summaryrefslogtreecommitdiffhomepage
path: root/device/logger.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2021-01-26 23:05:48 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2021-01-26 23:05:48 +0100
commitd669c78c4306290963415568f4a64a1ae2b35b20 (patch)
tree189a833daf57ffded2e177bfe5a554fd1b886392 /device/logger.go
parent7139279cd0b08ebbd2c0322bc01d5678aa00cd0e (diff)
device: combine debug and info log levels into 'verbose'
There are very few cases, if any, in which a user only wants one of these levels, so combine it into a single level. While we're at it, reduce indirection on the loggers by using an empty function rather than a nil function pointer. It's not like we have retpolines anyway, and we were always calling through a function with a branch prior, so this seems like a net gain. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'device/logger.go')
-rw-r--r--device/logger.go38
1 files changed, 9 insertions, 29 deletions
diff --git a/device/logger.go b/device/logger.go
index 6869a24..7601929 100644
--- a/device/logger.go
+++ b/device/logger.go
@@ -16,53 +16,33 @@ import (
// 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{})
+ Verbosef func(format string, args ...interface{})
+ Errorf func(format string, args ...interface{})
}
// Log levels for use with NewLogger.
const (
LogLevelSilent = iota
LogLevelError
- LogLevelInfo
- LogLevelDebug
+ LogLevelVerbose
)
+// Function for use in Logger for discarding logged lines.
+func DiscardLogf(format string, args ...interface{}) {}
+
// 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 {
- logger := new(Logger)
+ logger := &Logger{DiscardLogf, DiscardLogf}
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 >= LogLevelVerbose {
+ logger.Verbosef = logf("DEBUG")
}
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...)
- }
-}