2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 13:04:00 +00:00
|
|
|
*
|
2020-05-02 08:08:26 +00:00
|
|
|
* Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
|
2018-05-03 13:04:00 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-03 03:04:41 +00:00
|
|
|
package device
|
2017-06-27 15:33:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2021-01-22 22:11:17 +00:00
|
|
|
// 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 {
|
2021-01-26 22:05:48 +00:00
|
|
|
Verbosef func(format string, args ...interface{})
|
|
|
|
Errorf func(format string, args ...interface{})
|
2021-01-22 22:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log levels for use with NewLogger.
|
2017-06-27 15:33:06 +00:00
|
|
|
const (
|
2018-05-14 01:38:06 +00:00
|
|
|
LogLevelSilent = iota
|
|
|
|
LogLevelError
|
2021-01-26 22:05:48 +00:00
|
|
|
LogLevelVerbose
|
2017-06-27 15:33:06 +00:00
|
|
|
)
|
|
|
|
|
2021-01-26 22:05:48 +00:00
|
|
|
// Function for use in Logger for discarding logged lines.
|
|
|
|
func DiscardLogf(format string, args ...interface{}) {}
|
|
|
|
|
2021-01-22 22:11:17 +00:00
|
|
|
// 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.
|
2017-08-11 14:18:20 +00:00
|
|
|
func NewLogger(level int, prepend string) *Logger {
|
2021-01-26 22:05:48 +00:00
|
|
|
logger := &Logger{DiscardLogf, DiscardLogf}
|
2021-01-22 22:11:17 +00:00
|
|
|
logf := func(prefix string) func(string, ...interface{}) {
|
|
|
|
return log.New(os.Stdout, prefix+": "+prepend, log.Ldate|log.Ltime).Printf
|
|
|
|
}
|
2021-01-26 22:05:48 +00:00
|
|
|
if level >= LogLevelVerbose {
|
|
|
|
logger.Verbosef = logf("DEBUG")
|
2021-01-22 22:11:17 +00:00
|
|
|
}
|
|
|
|
if level >= LogLevelError {
|
|
|
|
logger.Errorf = logf("ERROR")
|
|
|
|
}
|
2017-06-27 15:33:06 +00:00
|
|
|
return logger
|
|
|
|
}
|