2017-05-27 14:30:11 -05:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2017-07-27 17:11:56 -05:00
|
|
|
"fmt"
|
2017-05-27 14:30:11 -05:00
|
|
|
"log"
|
|
|
|
"os"
|
2018-02-17 00:29:53 -05:00
|
|
|
"strings"
|
2017-05-27 14:30:11 -05:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LogLevel of quic-go
|
|
|
|
type LogLevel uint8
|
|
|
|
|
2017-07-27 17:11:56 -05:00
|
|
|
const (
|
|
|
|
// LogLevelNothing disables
|
|
|
|
LogLevelNothing LogLevel = iota
|
2017-05-27 14:30:11 -05:00
|
|
|
// LogLevelError enables err logs
|
|
|
|
LogLevelError
|
2017-07-27 17:11:56 -05:00
|
|
|
// LogLevelInfo enables info logs (e.g. packets)
|
|
|
|
LogLevelInfo
|
|
|
|
// LogLevelDebug enables debug logs (e.g. packet contents)
|
|
|
|
LogLevelDebug
|
2017-05-27 14:30:11 -05:00
|
|
|
)
|
|
|
|
|
2018-04-18 16:48:08 -05:00
|
|
|
const logEnv = "QUIC_GO_LOG_LEVEL"
|
|
|
|
|
|
|
|
// A Logger logs.
|
|
|
|
type Logger interface {
|
|
|
|
SetLogLevel(LogLevel)
|
|
|
|
SetLogTimeFormat(format string)
|
2018-09-02 16:18:54 -05:00
|
|
|
WithPrefix(prefix string) Logger
|
2018-04-18 16:48:08 -05:00
|
|
|
Debug() bool
|
|
|
|
|
|
|
|
Errorf(format string, args ...interface{})
|
|
|
|
Infof(format string, args ...interface{})
|
|
|
|
Debugf(format string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultLogger is used by quic-go for logging.
|
|
|
|
var DefaultLogger Logger
|
|
|
|
|
|
|
|
type defaultLogger struct {
|
2018-09-02 16:18:54 -05:00
|
|
|
prefix string
|
|
|
|
|
2018-04-18 16:48:08 -05:00
|
|
|
logLevel LogLevel
|
|
|
|
timeFormat string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Logger = &defaultLogger{}
|
2017-05-27 14:30:11 -05:00
|
|
|
|
|
|
|
// SetLogLevel sets the log level
|
2018-04-18 16:48:08 -05:00
|
|
|
func (l *defaultLogger) SetLogLevel(level LogLevel) {
|
|
|
|
l.logLevel = level
|
2017-05-27 14:30:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogTimeFormat sets the format of the timestamp
|
|
|
|
// an empty string disables the logging of timestamps
|
2018-04-18 16:48:08 -05:00
|
|
|
func (l *defaultLogger) SetLogTimeFormat(format string) {
|
2017-05-27 14:30:11 -05:00
|
|
|
log.SetFlags(0) // disable timestamp logging done by the log package
|
2018-04-18 16:48:08 -05:00
|
|
|
l.timeFormat = format
|
2017-05-27 14:30:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debugf logs something
|
2018-04-18 16:48:08 -05:00
|
|
|
func (l *defaultLogger) Debugf(format string, args ...interface{}) {
|
|
|
|
if l.logLevel == LogLevelDebug {
|
|
|
|
l.logMessage(format, args...)
|
2017-05-27 14:30:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Infof logs something
|
2018-04-18 16:48:08 -05:00
|
|
|
func (l *defaultLogger) Infof(format string, args ...interface{}) {
|
|
|
|
if l.logLevel >= LogLevelInfo {
|
|
|
|
l.logMessage(format, args...)
|
2017-05-27 14:30:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf logs something
|
2018-04-18 16:48:08 -05:00
|
|
|
func (l *defaultLogger) Errorf(format string, args ...interface{}) {
|
|
|
|
if l.logLevel >= LogLevelError {
|
|
|
|
l.logMessage(format, args...)
|
2017-05-27 14:30:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:48:08 -05:00
|
|
|
func (l *defaultLogger) logMessage(format string, args ...interface{}) {
|
2018-09-02 16:18:54 -05:00
|
|
|
var pre string
|
|
|
|
|
2018-04-18 16:48:08 -05:00
|
|
|
if len(l.timeFormat) > 0 {
|
2018-09-02 16:18:54 -05:00
|
|
|
pre = time.Now().Format(l.timeFormat) + " "
|
|
|
|
}
|
|
|
|
if len(l.prefix) > 0 {
|
|
|
|
pre += l.prefix + " "
|
|
|
|
}
|
|
|
|
log.Printf(pre+format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *defaultLogger) WithPrefix(prefix string) Logger {
|
|
|
|
if len(l.prefix) > 0 {
|
|
|
|
prefix = l.prefix + " " + prefix
|
|
|
|
}
|
|
|
|
return &defaultLogger{
|
|
|
|
logLevel: l.logLevel,
|
|
|
|
timeFormat: l.timeFormat,
|
|
|
|
prefix: prefix,
|
2017-05-27 14:30:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug returns true if the log level is LogLevelDebug
|
2018-04-18 16:48:08 -05:00
|
|
|
func (l *defaultLogger) Debug() bool {
|
|
|
|
return l.logLevel == LogLevelDebug
|
2017-05-27 14:30:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-04-18 16:48:08 -05:00
|
|
|
DefaultLogger = &defaultLogger{}
|
|
|
|
DefaultLogger.SetLogLevel(readLoggingEnv())
|
2017-05-27 14:30:11 -05:00
|
|
|
}
|
|
|
|
|
2018-04-18 16:48:08 -05:00
|
|
|
func readLoggingEnv() LogLevel {
|
2018-02-17 00:29:53 -05:00
|
|
|
switch strings.ToLower(os.Getenv(logEnv)) {
|
2017-07-27 17:11:56 -05:00
|
|
|
case "":
|
2018-04-18 16:48:08 -05:00
|
|
|
return LogLevelNothing
|
2018-02-17 00:29:53 -05:00
|
|
|
case "debug":
|
2018-04-18 16:48:08 -05:00
|
|
|
return LogLevelDebug
|
2018-02-17 00:29:53 -05:00
|
|
|
case "info":
|
2018-04-18 16:48:08 -05:00
|
|
|
return LogLevelInfo
|
2018-02-17 00:29:53 -05:00
|
|
|
case "error":
|
2018-04-18 16:48:08 -05:00
|
|
|
return LogLevelError
|
2017-07-27 17:11:56 -05:00
|
|
|
default:
|
|
|
|
fmt.Fprintln(os.Stderr, "invalid quic-go log level, see https://github.com/lucas-clemente/quic-go/wiki/Logging")
|
2018-04-18 16:48:08 -05:00
|
|
|
return LogLevelNothing
|
2017-05-27 14:30:11 -05:00
|
|
|
}
|
|
|
|
}
|