2018-05-03 15:04:00 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2017-06-27 17:33:06 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-30 14:41:08 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-06-27 17:33:06 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
LogLevelError = iota
|
|
|
|
LogLevelInfo
|
|
|
|
LogLevelDebug
|
|
|
|
)
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
Debug *log.Logger
|
|
|
|
Info *log.Logger
|
|
|
|
Error *log.Logger
|
|
|
|
}
|
|
|
|
|
2017-08-11 16:18:20 +02:00
|
|
|
func NewLogger(level int, prepend string) *Logger {
|
2017-06-30 14:41:08 +02:00
|
|
|
output := os.Stdout
|
2017-06-27 17:33:06 +02:00
|
|
|
logger := new(Logger)
|
2017-06-30 14:41:08 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return output, ioutil.Discard, ioutil.Discard
|
|
|
|
}()
|
|
|
|
|
|
|
|
logger.Debug = log.New(logDebug,
|
2017-08-11 16:18:20 +02:00
|
|
|
"DEBUG: "+prepend,
|
2018-05-13 19:33:41 +02:00
|
|
|
log.Ldate|log.Ltime,
|
2017-06-27 17:33:06 +02:00
|
|
|
)
|
2017-06-30 14:41:08 +02:00
|
|
|
|
|
|
|
logger.Info = log.New(logInfo,
|
2017-08-11 16:18:20 +02:00
|
|
|
"INFO: "+prepend,
|
2017-08-01 12:14:38 +02:00
|
|
|
log.Ldate|log.Ltime,
|
2017-06-27 17:33:06 +02:00
|
|
|
)
|
2017-06-30 14:41:08 +02:00
|
|
|
logger.Error = log.New(logErr,
|
2017-08-11 16:18:20 +02:00
|
|
|
"ERROR: "+prepend,
|
2017-08-01 12:14:38 +02:00
|
|
|
log.Ldate|log.Ltime,
|
2017-06-27 17:33:06 +02:00
|
|
|
)
|
|
|
|
return logger
|
|
|
|
}
|