<commit_before>//  Copyright (c) 2012-2013 Couchbase, Inc.
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
//  except in compliance with the License. You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//  Unless required by applicable law or agreed to in writing, software distributed under the
//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
//  either express or implied. See the License for the specific language governing permissions
//  and limitations under the License.

package base

import (
	"fmt"
	"log"
	"os"
	"runtime"
	"strings"
)

// 1 enables regular logs, 2 enables warnings, 3+ is nothing but panics.
// Default value is 1.
var LogLevel int = 1

// Set of LogTo() key strings that are enabled.
var LogKeys map[string]bool

var logger *log.Logger

func init() {
	logger = log.New(os.Stderr, "", log.Lmicroseconds)
	LogKeys = make(map[string]bool)
}

// Disables ANSI color in log output.
func LogNoColor() {
	reset, dim, fgRed, fgYellow = "", "", "", ""
}

// Parses a comma-separated list of log keys, probably coming from an argv flag.
// The key "bw" is interpreted as a call to LogNoColor, not a key.
func ParseLogFlag(flag string) {
	if flag != "" {
		ParseLogFlags(strings.Split(flag, ","))
	}
}

// Parses an array of log keys, probably coming from a argv flags.
// The key "bw" is interpreted as a call to LogNoColor, not a key.
func ParseLogFlags(flags []string) {
	for _, key := range flags {
		if key == "bw" {
			LogNoColor()
		} else {
			LogKeys[key] = true
			for strings.HasSuffix(key, "+") {
				key = key[0 : len(key)-1]
				LogKeys[key] = true
			}
		}
	}
	Log("Enabling logging: %s", flags)
}

// Returns a string identifying a function on the call stack.
// Use depth=1 for the caller of the function that calls GetCallersName, etc.
func GetCallersName(depth int) string {
	pc, file, line, ok := runtime.Caller(depth + 1)
	if !ok {
		return "???"
	}

	fnname := ""
	if fn := runtime.FuncForPC(pc); fn != nil {
		fnname = fn.Name()
	}

	return fmt.Sprintf("%s() at %s:%d", lastComponent(fnname), lastComponent(file), line)
}

// Logs a message to the console, but only if the corresponding key is true in LogKeys.
func LogTo(key string, format string, args ...interface{}) {
	if LogLevel <= 1 && LogKeys[key] {<commit_msg>Added "notime" logging flag, to disable timestamps in output<commit_after>//  Copyright (c) 2012-2013 Couchbase, Inc.
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
//  except in compliance with the License. You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//  Unless required by applicable law or agreed to in writing, software distributed under the
//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
//  either express or implied. See the License for the specific language governing permissions
//  and limitations under the License.

package base

import (
	"fmt"
	"log"
	"os"
	"runtime"
	"strings"
)

// 1 enables regular logs, 2 enables warnings, 3+ is nothing but panics.
// Default value is 1.
var LogLevel int = 1

// Set of LogTo() key strings that are enabled.
var LogKeys map[string]bool

var logger *log.Logger

func init() {
	logger = log.New(os.Stderr, "", log.Lmicroseconds)
	LogKeys = make(map[string]bool)
}

// Disables ANSI color in log output.
func LogNoColor() {
	reset, dim, fgRed, fgYellow = "", "", "", ""
}

func LogNoTime() {
	logger.SetFlags(logger.Flags() &^ (log.Ldate | log.Ltime | log.Lmicroseconds))
}

// Parses a comma-separated list of log keys, probably coming from an argv flag.
// The key "bw" is interpreted as a call to LogNoColor, not a key.
func ParseLogFlag(flag string) {
	if flag != "" {
		ParseLogFlags(strings.Split(flag, ","))
	}
}

// Parses an array of log keys, probably coming from a argv flags.
// The key "bw" is interpreted as a call to LogNoColor, not a key.
func ParseLogFlags(flags []string) {
	for _, key := range flags {
		switch key {
		case "bw":
			LogNoColor()
		case "notime":
			LogNoTime()
		default:
			LogKeys[key] = true
			for strings.HasSuffix(key, "+") {
				key = key[0 : len(key)-1]
				LogKeys[key] = true // "foo+" also enables "foo"
			}
		}
	}
	Log("Enabling logging: %s", flags)
}

// Returns a string identifying a function on the call stack.
// Use depth=1 for the caller of the function that calls GetCallersName, etc.
func GetCallersName(depth int) string {
	pc, file, line, ok := runtime.Caller(depth + 1)
	if !ok {
		return "???"
	}

	fnname := ""
	if fn := runtime.FuncForPC(pc); fn != nil {
		fnname = fn.Name()
	}

	return fmt.Sprintf("%s() at %s:%d", lastComponent(fnname), lastComponent(file), line)
}

// Logs a message to the console, but only if the corresponding key is true in LogKeys.
func LogTo(key string, format string, args ...interface{}) {
	if LogLevel <= 1 && LogKeys[key] {
{'fraction_non_alphanumeric': 0.12487492495497299, 'fraction_numerical': 0.008805283169901942, 'mean_word_length': 3.5108303249097474, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3136729', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1593, 'n_words': 630}