all, lib/logger: Refactor SetDebug calls (#6054)

This commit is contained in:
Lukas Lihotzki 2019-10-04 13:03:34 +02:00 committed by Simon Frei
parent 8fb576ed54
commit 96bb1c8e29
27 changed files with 16 additions and 176 deletions

View File

@ -7,16 +7,9 @@
package main package main
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("main", "Main package") l = logger.DefaultLogger.NewFacility("main", "Main package")
) )
func init() {
l.SetDebug("main", strings.Contains(os.Getenv("STTRACE"), "main") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,9 +7,6 @@
package api package api
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
@ -24,5 +21,7 @@ func shouldDebugHTTP() bool {
func init() { func init() {
// The debug facility was originally named "http", changed in: // The debug facility was originally named "http", changed in:
// https://github.com/syncthing/syncthing/pull/5548 // https://github.com/syncthing/syncthing/pull/5548
l.SetDebug("api", strings.Contains(os.Getenv("STTRACE"), "api") || strings.Contains(os.Getenv("STTRACE"), "http") || os.Getenv("STTRACE") == "all") if l.IsTraced("http") {
l.SetDebug("api", true)
}
} }

View File

@ -7,16 +7,9 @@
package beacon package beacon
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("beacon", "Multicast and broadcast discovery") l = logger.DefaultLogger.NewFacility("beacon", "Multicast and broadcast discovery")
) )
func init() {
l.SetDebug("beacon", strings.Contains(os.Getenv("STTRACE"), "beacon") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package config package config
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("config", "Configuration loading and saving") l = logger.DefaultLogger.NewFacility("config", "Configuration loading and saving")
) )
func init() {
l.SetDebug("config", strings.Contains(os.Getenv("STTRACE"), "config") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package connections package connections
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("connections", "Connection handling") l = logger.DefaultLogger.NewFacility("connections", "Connection handling")
) )
func init() {
l.SetDebug("connections", strings.Contains(os.Getenv("STTRACE"), "connections") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,9 +7,6 @@
package db package db
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
@ -17,10 +14,6 @@ var (
l = logger.DefaultLogger.NewFacility("db", "The database layer") l = logger.DefaultLogger.NewFacility("db", "The database layer")
) )
func init() {
l.SetDebug("db", strings.Contains(os.Getenv("STTRACE"), "db") || os.Getenv("STTRACE") == "all")
}
func shouldDebug() bool { func shouldDebug() bool {
return l.ShouldDebug("db") return l.ShouldDebug("db")
} }

View File

@ -11,7 +11,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings"
"time" "time"
"golang.org/x/net/proxy" "golang.org/x/net/proxy"
@ -29,8 +28,6 @@ var (
type dialFunc func(network, addr string) (net.Conn, error) type dialFunc func(network, addr string) (net.Conn, error)
func init() { func init() {
l.SetDebug("dialer", strings.Contains(os.Getenv("STTRACE"), "dialer") || os.Getenv("STTRACE") == "all")
proxy.RegisterDialerType("socks", socksDialerFunction) proxy.RegisterDialerType("socks", socksDialerFunction)
proxyDialer = getDialer(proxy.Direct) proxyDialer = getDialer(proxy.Direct)
usingProxy = proxyDialer != proxy.Direct usingProxy = proxyDialer != proxy.Direct

View File

@ -7,16 +7,9 @@
package discover package discover
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("discover", "Remote device discovery") l = logger.DefaultLogger.NewFacility("discover", "Remote device discovery")
) )
func init() {
l.SetDebug("discover", strings.Contains(os.Getenv("STTRACE"), "discover") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package events package events
import ( import (
"os"
"strings"
liblogger "github.com/syncthing/syncthing/lib/logger" liblogger "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
dl = liblogger.DefaultLogger.NewFacility("events", "Event generation and logging") dl = liblogger.DefaultLogger.NewFacility("events", "Event generation and logging")
) )
func init() {
dl.SetDebug("events", strings.Contains(os.Getenv("STTRACE"), "events") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,9 +7,6 @@
package fs package fs
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
@ -19,11 +16,7 @@ var (
func init() { func init() {
logger.DefaultLogger.NewFacility("walkfs", "Filesystem access while walking") logger.DefaultLogger.NewFacility("walkfs", "Filesystem access while walking")
switch { if logger.DefaultLogger.ShouldDebug("walkfs") {
case strings.Contains(os.Getenv("STTRACE"), "walkfs") || os.Getenv("STTRACE") == "all":
l.SetDebug("walkfs", true)
fallthrough
case strings.Contains(os.Getenv("STTRACE"), "fs"):
l.SetDebug("fs", true) l.SetDebug("fs", true)
} }
} }

View File

@ -50,6 +50,7 @@ type Logger interface {
Warnf(format string, vals ...interface{}) Warnf(format string, vals ...interface{})
ShouldDebug(facility string) bool ShouldDebug(facility string) bool
SetDebug(facility string, enabled bool) SetDebug(facility string, enabled bool)
IsTraced(facility string) bool
Facilities() map[string]string Facilities() map[string]string
FacilityDebugging() []string FacilityDebugging() []string
NewFacility(facility, description string) Logger NewFacility(facility, description string) Logger
@ -60,6 +61,7 @@ type logger struct {
handlers [NumLevels][]MessageHandler handlers [NumLevels][]MessageHandler
facilities map[string]string // facility name => description facilities map[string]string // facility name => description
debug map[string]struct{} // only facility names with debugging enabled debug map[string]struct{} // only facility names with debugging enabled
traces string
mut sync.Mutex mut sync.Mutex
} }
@ -78,6 +80,7 @@ func New() Logger {
func newLogger(w io.Writer) Logger { func newLogger(w io.Writer) Logger {
return &logger{ return &logger{
logger: log.New(w, "", DefaultFlags), logger: log.New(w, "", DefaultFlags),
traces: os.Getenv("STTRACE"),
facilities: make(map[string]string), facilities: make(map[string]string),
debug: make(map[string]struct{}), debug: make(map[string]struct{}),
} }
@ -210,6 +213,11 @@ 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"
}
// FacilityDebugging returns the set of facilities that have debugging // FacilityDebugging returns the set of facilities that have debugging
// enabled. // enabled.
func (l *logger) FacilityDebugging() []string { func (l *logger) FacilityDebugging() []string {
@ -236,6 +244,8 @@ func (l *logger) Facilities() map[string]string {
// NewFacility returns a new logger bound to the named facility. // NewFacility returns a new logger bound to the named facility.
func (l *logger) NewFacility(facility, description string) Logger { func (l *logger) NewFacility(facility, description string) Logger {
l.SetDebug(facility, l.IsTraced(facility))
l.mut.Lock() l.mut.Lock()
l.facilities[facility] = description l.facilities[facility] = description
l.mut.Unlock() l.mut.Unlock()

View File

@ -7,9 +7,6 @@
package model package model
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
@ -17,10 +14,6 @@ var (
l = logger.DefaultLogger.NewFacility("model", "The root hub") l = logger.DefaultLogger.NewFacility("model", "The root hub")
) )
func init() {
l.SetDebug("model", strings.Contains(os.Getenv("STTRACE"), "model") || os.Getenv("STTRACE") == "all")
}
func shouldDebug() bool { func shouldDebug() bool {
return l.ShouldDebug("model") return l.ShouldDebug("model")
} }

View File

@ -7,16 +7,9 @@
package nat package nat
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("nat", "NAT discovery and port mapping") l = logger.DefaultLogger.NewFacility("nat", "NAT discovery and port mapping")
) )
func init() {
l.SetDebug("nat", strings.Contains(os.Getenv("STTRACE"), "nat") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package pmp package pmp
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("pmp", "NAT-PMP discovery and port mapping") l = logger.DefaultLogger.NewFacility("pmp", "NAT-PMP discovery and port mapping")
) )
func init() {
l.SetDebug("pmp", strings.Contains(os.Getenv("STTRACE"), "pmp") || os.Getenv("STTRACE") == "all")
}

View File

@ -3,16 +3,9 @@
package protocol package protocol
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("protocol", "The BEP protocol") l = logger.DefaultLogger.NewFacility("protocol", "The BEP protocol")
) )
func init() {
l.SetDebug("protocol", strings.Contains(os.Getenv("STTRACE"), "protocol") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package rc package rc
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("rc", "Remote control package") l = logger.DefaultLogger.NewFacility("rc", "Remote control package")
) )
func init() {
l.SetDebug("rc", strings.Contains(os.Getenv("STTRACE"), "rc") || os.Getenv("STTRACE") == "all")
}

View File

@ -3,16 +3,9 @@
package client package client
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("relay", "") l = logger.DefaultLogger.NewFacility("relay", "")
) )
func init() {
l.SetDebug("relay", strings.Contains(os.Getenv("STTRACE"), "relay") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package scanner package scanner
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("scanner", "File change detection and hashing") l = logger.DefaultLogger.NewFacility("scanner", "File change detection and hashing")
) )
func init() {
l.SetDebug("scanner", strings.Contains(os.Getenv("STTRACE"), "scanner") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package stats package stats
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("stats", "Persistent device and folder statistics") l = logger.DefaultLogger.NewFacility("stats", "Persistent device and folder statistics")
) )
func init() {
l.SetDebug("stats", strings.Contains(os.Getenv("STTRACE"), "stats") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package stun package stun
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("stun", "STUN functionality") l = logger.DefaultLogger.NewFacility("stun", "STUN functionality")
) )
func init() {
l.SetDebug("stun", strings.Contains(os.Getenv("STTRACE"), "stun") || os.Getenv("STTRACE") == "all")
}

View File

@ -9,7 +9,6 @@ package sync
import ( import (
"os" "os"
"strconv" "strconv"
"strings"
"time" "time"
deadlock "github.com/sasha-s/go-deadlock" deadlock "github.com/sasha-s/go-deadlock"
@ -23,13 +22,11 @@ var (
// We make an exception in this package and have an actual "if debug { ... // We make an exception in this package and have an actual "if debug { ...
// }" variable, as it may be rather performance critical and does // }" variable, as it may be rather performance critical and does
// nonstandard things (from a debug logging PoV). // nonstandard things (from a debug logging PoV).
debug = strings.Contains(os.Getenv("STTRACE"), "sync") || os.Getenv("STTRACE") == "all" debug = logger.DefaultLogger.ShouldDebug("sync")
useDeadlock = false useDeadlock = false
) )
func init() { func init() {
l.SetDebug("sync", strings.Contains(os.Getenv("STTRACE"), "sync") || os.Getenv("STTRACE") == "all")
if n, _ := strconv.Atoi(os.Getenv("STLOCKTHRESHOLD")); n > 0 { if n, _ := strconv.Atoi(os.Getenv("STLOCKTHRESHOLD")); n > 0 {
threshold = time.Duration(n) * time.Millisecond threshold = time.Duration(n) * time.Millisecond
} }

View File

@ -7,16 +7,9 @@
package syncthing package syncthing
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("app", "Main run facility") l = logger.DefaultLogger.NewFacility("app", "Main run facility")
) )
func init() {
l.SetDebug("app", strings.Contains(os.Getenv("STTRACE"), "app") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package upgrade package upgrade
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("upgrade", "Binary upgrades") l = logger.DefaultLogger.NewFacility("upgrade", "Binary upgrades")
) )
func init() {
l.SetDebug("upgrade", strings.Contains(os.Getenv("STTRACE"), "upgrade") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package upnp package upnp
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("upnp", "UPnP discovery and port mapping") l = logger.DefaultLogger.NewFacility("upnp", "UPnP discovery and port mapping")
) )
func init() {
l.SetDebug("upnp", strings.Contains(os.Getenv("STTRACE"), "upnp") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package ur package ur
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("ur", "Usage reporting") l = logger.DefaultLogger.NewFacility("ur", "Usage reporting")
) )
func init() {
l.SetDebug("ur", strings.Contains(os.Getenv("STTRACE"), "ur") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,16 +7,9 @@
package versioner package versioner
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var ( var (
l = logger.DefaultLogger.NewFacility("versioner", "File versioning") l = logger.DefaultLogger.NewFacility("versioner", "File versioning")
) )
func init() {
l.SetDebug("versioner", strings.Contains(os.Getenv("STTRACE"), "versioner") || os.Getenv("STTRACE") == "all")
}

View File

@ -7,18 +7,9 @@
package watchaggregator package watchaggregator
import ( import (
"os"
"strings"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
) )
var facilityName = "watchaggregator"
var ( var (
l = logger.DefaultLogger.NewFacility(facilityName, "Filesystem event watcher") l = logger.DefaultLogger.NewFacility("watchaggregator", "Filesystem event watcher")
) )
func init() {
l.SetDebug(facilityName, strings.Contains(os.Getenv("STTRACE"), facilityName) || os.Getenv("STTRACE") == "all")
}