2018-06-08 08:43:43 +03:00
|
|
|
package logutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2018-06-12 15:32:34 +03:00
|
|
|
"strings"
|
2018-06-08 08:43:43 +03:00
|
|
|
)
|
|
|
|
|
2024-03-16 17:07:42 +01:00
|
|
|
// EnvTestRun value: "1"
|
|
|
|
const EnvTestRun = "GL_TEST_RUN"
|
|
|
|
|
2022-09-06 13:48:25 +02:00
|
|
|
// envDebug value: one or several debug keys.
|
|
|
|
// examples:
|
|
|
|
// - Remove output to `/dev/null`: `GL_DEBUG=linters_output ./golangci-lint run`
|
|
|
|
// - Show linters configuration: `GL_DEBUG=enabled_linters golangci-lint run`
|
|
|
|
// - Some analysis details: `GL_DEBUG=goanalysis/analyze,goanalysis/facts golangci-lint run`
|
|
|
|
const envDebug = "GL_DEBUG"
|
|
|
|
|
|
|
|
const (
|
2022-12-27 10:48:48 +01:00
|
|
|
DebugKeyAutogenExclude = "autogen_exclude" // Debugs a filter excluding autogenerated source code.
|
2022-09-06 13:48:25 +02:00
|
|
|
DebugKeyBinSalt = "bin_salt"
|
|
|
|
DebugKeyConfigReader = "config_reader"
|
|
|
|
DebugKeyEmpty = ""
|
|
|
|
DebugKeyEnabledLinters = "enabled_linters"
|
2022-12-27 10:48:48 +01:00
|
|
|
DebugKeyEnv = "env" // Debugs `go env` command.
|
2022-09-06 13:48:25 +02:00
|
|
|
DebugKeyExcludeRules = "exclude_rules"
|
|
|
|
DebugKeyExec = "exec"
|
|
|
|
DebugKeyFilenameUnadjuster = "filename_unadjuster"
|
2024-03-20 17:25:22 +01:00
|
|
|
DebugKeyInvalidIssue = "invalid_issue"
|
2023-05-31 22:33:20 +02:00
|
|
|
DebugKeyForbidigo = "forbidigo"
|
2022-09-06 13:48:25 +02:00
|
|
|
DebugKeyGoEnv = "goenv"
|
|
|
|
DebugKeyLinter = "linter"
|
|
|
|
DebugKeyLintersContext = "linters_context"
|
|
|
|
DebugKeyLintersDB = "lintersdb"
|
|
|
|
DebugKeyLintersOutput = "linters_output"
|
2022-12-27 10:48:48 +01:00
|
|
|
DebugKeyLoader = "loader" // Debugs packages loading (including `go/packages` internal debugging).
|
2022-09-06 13:48:25 +02:00
|
|
|
DebugKeyMaxFromLinter = "max_from_linter"
|
|
|
|
DebugKeyMaxSameIssues = "max_same_issues"
|
|
|
|
DebugKeyPkgCache = "pkgcache"
|
|
|
|
DebugKeyRunner = "runner"
|
|
|
|
DebugKeySeverityRules = "severity_rules"
|
|
|
|
DebugKeySkipDirs = "skip_dirs"
|
|
|
|
DebugKeySourceCode = "source_code"
|
|
|
|
DebugKeyStopwatch = "stopwatch"
|
|
|
|
DebugKeyTabPrinter = "tab_printer"
|
|
|
|
DebugKeyTest = "test"
|
|
|
|
DebugKeyTextPrinter = "text_printer"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DebugKeyGoAnalysis = "goanalysis"
|
|
|
|
|
|
|
|
DebugKeyGoAnalysisAnalyze = DebugKeyGoAnalysis + "/analyze"
|
|
|
|
DebugKeyGoAnalysisIssuesCache = DebugKeyGoAnalysis + "/issues/cache"
|
|
|
|
DebugKeyGoAnalysisMemory = DebugKeyGoAnalysis + "/memory"
|
|
|
|
|
|
|
|
DebugKeyGoAnalysisFacts = DebugKeyGoAnalysis + "/facts"
|
|
|
|
DebugKeyGoAnalysisFactsCache = DebugKeyGoAnalysisFacts + "/cache"
|
|
|
|
DebugKeyGoAnalysisFactsExport = DebugKeyGoAnalysisFacts + "/export"
|
|
|
|
DebugKeyGoAnalysisFactsInherit = DebugKeyGoAnalysisFacts + "/inherit"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-12-27 10:48:48 +01:00
|
|
|
DebugKeyGoCritic = "gocritic" // Debugs `go-critic` linter.
|
2024-03-07 16:23:42 +02:00
|
|
|
DebugKeyGovet = "govet" // Debugs `govet` linter.
|
2022-12-27 10:48:48 +01:00
|
|
|
DebugKeyMegacheck = "megacheck" // Debugs `staticcheck` related linters.
|
|
|
|
DebugKeyNolint = "nolint" // Debugs a filter excluding issues by `//nolint` comments.
|
2023-08-29 22:25:39 +03:00
|
|
|
DebugKeyRevive = "revive" // Debugs `revive` linter.
|
2022-09-06 13:48:25 +02:00
|
|
|
)
|
|
|
|
|
2018-06-12 15:32:34 +03:00
|
|
|
func getEnabledDebugs() map[string]bool {
|
|
|
|
ret := map[string]bool{}
|
2022-09-06 13:48:25 +02:00
|
|
|
debugVar := os.Getenv(envDebug)
|
2018-06-12 15:32:34 +03:00
|
|
|
if debugVar == "" {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range strings.Split(debugVar, ",") {
|
|
|
|
ret[tag] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
var enabledDebugs = getEnabledDebugs()
|
|
|
|
|
2023-03-28 17:22:55 +03:00
|
|
|
type DebugFunc func(format string, args ...any)
|
2018-06-12 15:32:34 +03:00
|
|
|
|
2023-04-16 19:18:53 +03:00
|
|
|
func nopDebugf(_ string, _ ...any) {}
|
2018-06-12 15:32:34 +03:00
|
|
|
|
|
|
|
func Debug(tag string) DebugFunc {
|
|
|
|
if !enabledDebugs[tag] {
|
|
|
|
return nopDebugf
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:48:18 +03:00
|
|
|
logger := NewStderrLog(tag)
|
|
|
|
logger.SetLevel(LogLevelDebug)
|
|
|
|
|
2023-03-28 17:22:55 +03:00
|
|
|
return func(format string, args ...any) {
|
2018-06-13 22:37:48 +03:00
|
|
|
logger.Debugf(format, args...)
|
2018-06-12 15:32:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 19:20:43 +03:00
|
|
|
func HaveDebugTag(tag string) bool {
|
|
|
|
return enabledDebugs[tag]
|
|
|
|
}
|
2018-06-13 22:37:48 +03:00
|
|
|
|
2024-02-27 00:03:48 +01:00
|
|
|
var verbose bool
|
|
|
|
|
2018-06-13 22:37:48 +03:00
|
|
|
func SetupVerboseLog(log Log, isVerbose bool) {
|
|
|
|
if isVerbose {
|
2024-02-27 00:03:48 +01:00
|
|
|
verbose = isVerbose
|
2018-06-13 22:37:48 +03:00
|
|
|
log.SetLevel(LogLevelInfo)
|
|
|
|
}
|
|
|
|
}
|
2024-02-27 00:03:48 +01:00
|
|
|
|
|
|
|
func IsVerbose() bool {
|
|
|
|
return verbose
|
|
|
|
}
|