ooni-probe-cli/pkg/oonimkall/chanlogger.go
Simone Basso ee5be24900
[forwardport] refactor(oonimkall): merge internal/task into oonimkall (#617) (#618)
This diff forward ports bc4b9f1ea89158bfa7b7a80ae59a90b43c784ed2 to `master`.

See https://github.com/ooni/probe/issues/1903
2021-11-26 20:21:42 +01:00

86 lines
1.6 KiB
Go

package oonimkall
import "fmt"
// chanLogger is a logger targeting a channel
type chanLogger struct {
emitter *eventEmitter
hasdebug bool
hasinfo bool
haswarning bool
out chan<- *event
}
// Debug implements Logger.Debug
func (cl *chanLogger) Debug(msg string) {
if cl.hasdebug {
cl.emitter.Emit("log", eventLog{
LogLevel: "DEBUG",
Message: msg,
})
}
}
// Debugf implements Logger.Debugf
func (cl *chanLogger) Debugf(format string, v ...interface{}) {
if cl.hasdebug {
cl.Debug(fmt.Sprintf(format, v...))
}
}
// Info implements Logger.Info
func (cl *chanLogger) Info(msg string) {
if cl.hasinfo {
cl.emitter.Emit("log", eventLog{
LogLevel: "INFO",
Message: msg,
})
}
}
// Infof implements Logger.Infof
func (cl *chanLogger) Infof(format string, v ...interface{}) {
if cl.hasinfo {
cl.Info(fmt.Sprintf(format, v...))
}
}
// Warn implements Logger.Warn
func (cl *chanLogger) Warn(msg string) {
if cl.haswarning {
cl.emitter.Emit("log", eventLog{
LogLevel: "WARNING",
Message: msg,
})
}
}
// Warnf implements Logger.Warnf
func (cl *chanLogger) Warnf(format string, v ...interface{}) {
if cl.haswarning {
cl.Warn(fmt.Sprintf(format, v...))
}
}
// newChanLogger creates a new ChanLogger instance.
func newChanLogger(emitter *eventEmitter, logLevel string,
out chan<- *event) *chanLogger {
cl := &chanLogger{
emitter: emitter,
out: out,
}
switch logLevel {
case "DEBUG", "DEBUG2":
cl.hasdebug = true
fallthrough
case "INFO":
cl.hasinfo = true
fallthrough
case "ERR", "WARNING":
fallthrough
default:
cl.haswarning = true
}
return cl
}