From 9f6d732587b26f507916e26e2f5c57e331b1492c Mon Sep 17 00:00:00 2001 From: gudvinr Date: Tue, 6 Feb 2024 16:07:59 +0300 Subject: [PATCH] lib/logger: Split STTRACE into list of strings (#9402) Currently `IsTraced("xyz")` will return true for any inclusion of "xyz" in string. This change splits `STTRACE` using `','`, `' '` and `';'` as delimiters. That makes facilities separation more clear. --- lib/logger/logger.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/logger/logger.go b/lib/logger/logger.go index ecf20fd41..ac06f0c13 100644 --- a/lib/logger/logger.go +++ b/lib/logger/logger.go @@ -15,6 +15,8 @@ import ( "strings" "sync" "time" + + "golang.org/x/exp/slices" ) // This package uses stdlib sync as it may be used to debug syncthing/lib/sync @@ -63,7 +65,7 @@ type logger struct { handlers [NumLevels][]MessageHandler facilities map[string]string // facility name => description debug map[string]struct{} // only facility names with debugging enabled - traces string + traces []string mut sync.Mutex } @@ -80,9 +82,21 @@ func New() Logger { } func newLogger(w io.Writer) Logger { + traces := strings.FieldsFunc(os.Getenv("STTRACE"), func(r rune) bool { + return strings.ContainsRune(",; ", r) + }) + + if len(traces) > 0 { + if slices.Contains(traces, "all") { + traces = []string{"all"} + } else { + slices.Sort(traces) + } + } + return &logger{ logger: log.New(w, "", DefaultFlags), - traces: os.Getenv("STTRACE"), + traces: traces, facilities: make(map[string]string), debug: make(map[string]struct{}), } @@ -217,7 +231,16 @@ func (l *logger) SetDebug(facility string, enabled bool) { // IsTraced returns whether the facility name is contained in STTRACE. func (l *logger) IsTraced(facility string) bool { - return strings.Contains(l.traces, facility) || l.traces == "all" + if len(l.traces) > 0 { + if l.traces[0] == "all" { + return true + } + + _, found := slices.BinarySearch(l.traces, facility) + return found + } + + return false } // FacilityDebugging returns the set of facilities that have debugging