mihomo/tunnel/log.go

68 lines
991 B
Go
Raw Normal View History

2018-06-10 22:50:03 +08:00
package tunnel
import (
"fmt"
log "github.com/sirupsen/logrus"
)
const (
2018-06-22 13:15:20 +08:00
ERROR LogLevel = iota
2018-06-10 22:50:03 +08:00
WARNING
2018-06-22 13:15:20 +08:00
INFO
2018-06-10 22:50:03 +08:00
DEBUG
)
2018-06-22 13:15:20 +08:00
type LogLevel int
2018-06-10 22:50:03 +08:00
type Log struct {
2018-06-22 13:15:20 +08:00
LogLevel LogLevel
Payload string
2018-06-10 22:50:03 +08:00
}
2018-06-18 11:31:49 +08:00
func (l *Log) Type() string {
2018-06-22 13:15:20 +08:00
switch l.LogLevel {
2018-06-18 11:31:49 +08:00
case INFO:
return "Info"
case WARNING:
return "Warning"
case ERROR:
return "Error"
case DEBUG:
return "Debug"
default:
return "Unknow"
}
}
2018-06-10 22:50:03 +08:00
func print(data Log) {
2018-06-22 13:15:20 +08:00
switch data.LogLevel {
2018-06-10 22:50:03 +08:00
case INFO:
log.Infoln(data.Payload)
case WARNING:
log.Warnln(data.Payload)
case ERROR:
log.Errorln(data.Payload)
case DEBUG:
log.Debugln(data.Payload)
}
}
func (t *Tunnel) subscribeLogs() {
sub, err := t.observable.Subscribe()
if err != nil {
log.Fatalf("Can't subscribe tunnel log: %s", err.Error())
}
for elm := range sub {
data := elm.(Log)
print(data)
}
}
2018-06-22 13:15:20 +08:00
func newLog(logLevel LogLevel, format string, v ...interface{}) Log {
2018-06-10 22:50:03 +08:00
return Log{
2018-06-22 13:15:20 +08:00
LogLevel: logLevel,
Payload: fmt.Sprintf(format, v...),
2018-06-10 22:50:03 +08:00
}
}