github.com/nozzle/golangci-lint@v1.49.0-nz3/pkg/logutils/logutils.go (about)

     1  package logutils
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  )
     7  
     8  // envDebug value: one or several debug keys.
     9  // examples:
    10  // - Remove output to `/dev/null`: `GL_DEBUG=linters_output ./golangci-lint run`
    11  // - Show linters configuration: `GL_DEBUG=enabled_linters golangci-lint run`
    12  // - Some analysis details: `GL_DEBUG=goanalysis/analyze,goanalysis/facts golangci-lint run`
    13  const envDebug = "GL_DEBUG"
    14  
    15  const (
    16  	DebugKeyAutogenExclude     = "autogen_exclude"
    17  	DebugKeyBinSalt            = "bin_salt"
    18  	DebugKeyConfigReader       = "config_reader"
    19  	DebugKeyEmpty              = ""
    20  	DebugKeyEnabledLinters     = "enabled_linters"
    21  	DebugKeyEnv                = "env"
    22  	DebugKeyExcludeRules       = "exclude_rules"
    23  	DebugKeyExec               = "exec"
    24  	DebugKeyFilenameUnadjuster = "filename_unadjuster"
    25  	DebugKeyGoEnv              = "goenv"
    26  	DebugKeyLinter             = "linter"
    27  	DebugKeyLintersContext     = "linters_context"
    28  	DebugKeyLintersDB          = "lintersdb"
    29  	DebugKeyLintersOutput      = "linters_output"
    30  	DebugKeyLoader             = "loader"
    31  	DebugKeyMaxFromLinter      = "max_from_linter"
    32  	DebugKeyMaxSameIssues      = "max_same_issues"
    33  	DebugKeyPkgCache           = "pkgcache"
    34  	DebugKeyRunner             = "runner"
    35  	DebugKeySeverityRules      = "severity_rules"
    36  	DebugKeySkipDirs           = "skip_dirs"
    37  	DebugKeySourceCode         = "source_code"
    38  	DebugKeyStopwatch          = "stopwatch"
    39  	DebugKeyTabPrinter         = "tab_printer"
    40  	DebugKeyTest               = "test"
    41  	DebugKeyTextPrinter        = "text_printer"
    42  )
    43  
    44  const (
    45  	DebugKeyGoAnalysis = "goanalysis"
    46  
    47  	DebugKeyGoAnalysisAnalyze     = DebugKeyGoAnalysis + "/analyze"
    48  	DebugKeyGoAnalysisIssuesCache = DebugKeyGoAnalysis + "/issues/cache"
    49  	DebugKeyGoAnalysisMemory      = DebugKeyGoAnalysis + "/memory"
    50  
    51  	DebugKeyGoAnalysisFacts        = DebugKeyGoAnalysis + "/facts"
    52  	DebugKeyGoAnalysisFactsCache   = DebugKeyGoAnalysisFacts + "/cache"
    53  	DebugKeyGoAnalysisFactsExport  = DebugKeyGoAnalysisFacts + "/export"
    54  	DebugKeyGoAnalysisFactsInherit = DebugKeyGoAnalysisFacts + "/inherit"
    55  )
    56  
    57  const (
    58  	DebugKeyGoCritic  = "gocritic"
    59  	DebugKeyMegacheck = "megacheck"
    60  	DebugKeyNolint    = "nolint"
    61  	DebugKeyRevive    = "revive"
    62  )
    63  
    64  func getEnabledDebugs() map[string]bool {
    65  	ret := map[string]bool{}
    66  	debugVar := os.Getenv(envDebug)
    67  	if debugVar == "" {
    68  		return ret
    69  	}
    70  
    71  	for _, tag := range strings.Split(debugVar, ",") {
    72  		ret[tag] = true
    73  	}
    74  
    75  	return ret
    76  }
    77  
    78  var enabledDebugs = getEnabledDebugs()
    79  
    80  type DebugFunc func(format string, args ...interface{})
    81  
    82  func nopDebugf(format string, args ...interface{}) {}
    83  
    84  func Debug(tag string) DebugFunc {
    85  	if !enabledDebugs[tag] {
    86  		return nopDebugf
    87  	}
    88  
    89  	logger := NewStderrLog(tag)
    90  	logger.SetLevel(LogLevelDebug)
    91  
    92  	return func(format string, args ...interface{}) {
    93  		logger.Debugf(format, args...)
    94  	}
    95  }
    96  
    97  func HaveDebugTag(tag string) bool {
    98  	return enabledDebugs[tag]
    99  }
   100  
   101  func SetupVerboseLog(log Log, isVerbose bool) {
   102  	if isVerbose {
   103  		log.SetLevel(LogLevelInfo)
   104  	}
   105  }