forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogutils.go
135 lines (112 loc) · 3.8 KB
/
logutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package logutils
import (
"os"
"strings"
)
// EnvTestRun value: "1"
const EnvTestRun = "GL_TEST_RUN"
// 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 (
DebugKeyBinSalt = "bin_salt"
DebugKeyConfigReader = "config_reader"
DebugKeyEmpty = ""
DebugKeyEnabledLinters = "enabled_linters"
DebugKeyExec = "exec"
DebugKeyFormatter = "formatter"
DebugKeyGoEnv = "goenv"
DebugKeyLinter = "linter"
DebugKeyLintersContext = "linters_context"
DebugKeyLintersDB = "lintersdb"
DebugKeyLintersOutput = "linters_output"
DebugKeyLoader = "loader" // Debugs packages loading (including `go/packages` internal debugging).
DebugKeyPkgCache = "pkgcache"
DebugKeyRunner = "runner"
DebugKeyStopwatch = "stopwatch"
DebugKeyTest = "test"
)
// Printers.
const (
DebugKeyCheckstylePrinter = "checkstyle_printer"
DebugKeyCodeClimatePrinter = "codeclimate_printer"
DebugKeySarifPrinter = "sarif_printer"
DebugKeyTabPrinter = "tab_printer"
DebugKeyTeamCityPrinter = "teamcity_printer"
DebugKeyTextPrinter = "text_printer"
)
// Processors.
const (
DebugKeyExclusionPaths = "exclusion_paths"
DebugKeyExclusionRules = "exclusion_rules"
DebugKeyFilenameUnadjuster = "filename_unadjuster"
DebugKeyGeneratedFileFilter = "generated_file_filter" // Debugs a filter excluding autogenerated source code.
DebugKeyInvalidIssue = "invalid_issue"
DebugKeyMaxFromLinter = "max_from_linter"
DebugKeyMaxSameIssues = "max_same_issues"
DebugKeyNolintFilter = "nolint_filter" // Debugs a filter excluding issues by `//nolint` comments.
DebugKeyPathAbsoluter = "path_absoluter"
DebugKeyPathPrettifier = "path_prettifier"
DebugKeyPathRelativity = "path_relativity"
DebugKeySeverityRules = "severity_rules"
DebugKeySkipDirs = "skip_dirs"
DebugKeySourceCode = "source_code"
)
// Analysis.
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"
)
// Linters.
const (
DebugKeyForbidigo = "forbidigo" // Debugs `forbidigo` linter.
DebugKeyGoCritic = "gocritic" // Debugs `gocritic` linter.
DebugKeyGovet = "govet" // Debugs `govet` linter.
DebugKeyRevive = "revive" // Debugs `revive` linter.
)
func getEnabledDebugs() map[string]bool {
ret := map[string]bool{}
debugVar := os.Getenv(envDebug)
if debugVar == "" {
return ret
}
for _, tag := range strings.Split(debugVar, ",") {
ret[tag] = true
}
return ret
}
var enabledDebugs = getEnabledDebugs()
type DebugFunc func(format string, args ...any)
func nopDebugf(_ string, _ ...any) {}
func Debug(tag string) DebugFunc {
if !enabledDebugs[tag] {
return nopDebugf
}
logger := NewStderrLog(tag)
logger.SetLevel(LogLevelDebug)
return func(format string, args ...any) {
logger.Debugf(format, args...)
}
}
func HaveDebugTag(tag string) bool {
return enabledDebugs[tag]
}
var verbose bool
func SetupVerboseLog(log Log, isVerbose bool) {
if isVerbose {
verbose = isVerbose
log.SetLevel(LogLevelInfo)
}
}
func IsVerbose() bool {
return verbose
}