Files
wireguard-go/device/logger.go
T

49 lines
1.2 KiB
Go
Raw Normal View History

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