diff options
author | Josh Bleecher Snyder <josh@tailscale.com> | 2021-01-22 14:11:17 -0800 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-01-26 22:40:20 +0100 |
commit | 7139279cd0b08ebbd2c0322bc01d5678aa00cd0e (patch) | |
tree | b49339aabb016e640f2f6088bc5753395d4239b8 /device/logger.go | |
parent | 37efdcaccfb16e47137728b5462c90d0b2ae8460 (diff) |
device: change logging interface to use functions
This commit overhauls wireguard-go's logging.
The primary, motivating change is to use a function instead
of a *log.Logger as the basic unit of logging.
Using functions provides a lot more flexibility for
people to bring their own logging system.
It also introduces logging helper methods on Device.
These reduce line noise at the call site.
They also allow for log functions to be nil;
when nil, instead of generating a log line and throwing it away,
we don't bother generating it at all.
This spares allocation and pointless work.
This is a breaking change, although the fix required
of clients is fairly straightforward.
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
Diffstat (limited to 'device/logger.go')
-rw-r--r-- | device/logger.go | 81 |
1 files changed, 45 insertions, 36 deletions
diff --git a/device/logger.go b/device/logger.go index 3c4d744..6869a24 100644 --- a/device/logger.go +++ b/device/logger.go @@ -6,12 +6,22 @@ package device import ( - "io" - "io/ioutil" "log" "os" ) +// A Logger provides logging for a Device. +// The functions are Printf-style functions. +// They must be safe for concurrent use. +// They do not require a trailing newline in the format. +// If nil, that level of logging will be silent. +type Logger struct { + Debugf func(format string, args ...interface{}) + Infof func(format string, args ...interface{}) + Errorf func(format string, args ...interface{}) +} + +// Log levels for use with NewLogger. const ( LogLevelSilent = iota LogLevelError @@ -19,41 +29,40 @@ const ( LogLevelDebug ) -type Logger struct { - Debug *log.Logger - Info *log.Logger - Error *log.Logger -} - +// NewLogger constructs a Logger that writes to stdout. +// It logs at the specified log level and above. +// It decorates log lines with the log level, date, time, and prepend. func NewLogger(level int, prepend string) *Logger { - output := os.Stdout logger := new(Logger) - - logErr, logInfo, logDebug := func() (io.Writer, io.Writer, io.Writer) { - if level >= LogLevelDebug { - return output, output, output - } - if level >= LogLevelInfo { - return output, output, ioutil.Discard - } - if level >= LogLevelError { - return output, ioutil.Discard, ioutil.Discard - } - return ioutil.Discard, ioutil.Discard, ioutil.Discard - }() - - logger.Debug = log.New(logDebug, - "DEBUG: "+prepend, - log.Ldate|log.Ltime, - ) - - logger.Info = log.New(logInfo, - "INFO: "+prepend, - log.Ldate|log.Ltime, - ) - logger.Error = log.New(logErr, - "ERROR: "+prepend, - log.Ldate|log.Ltime, - ) + logf := func(prefix string) func(string, ...interface{}) { + return log.New(os.Stdout, prefix+": "+prepend, log.Ldate|log.Ltime).Printf + } + if level >= LogLevelDebug { + logger.Debugf = logf("DEBUG") + } + if level >= LogLevelInfo { + logger.Infof = logf("INFO") + } + if level >= LogLevelError { + logger.Errorf = logf("ERROR") + } return logger } + +func (device *Device) debugf(format string, args ...interface{}) { + if device.log.Debugf != nil { + device.log.Debugf(format, args...) + } +} + +func (device *Device) infof(format string, args ...interface{}) { + if device.log.Infof != nil { + device.log.Infof(format, args...) + } +} + +func (device *Device) errorf(format string, args ...interface{}) { + if device.log.Errorf != nil { + device.log.Errorf(format, args...) + } +} |