mihomo/tunnel/log.go

52 lines
864 B
Go
Raw Normal View History

2018-06-10 22:50:03 +08:00
package tunnel
import (
"fmt"
C "github.com/Dreamacro/clash/constant"
2018-06-10 22:50:03 +08:00
log "github.com/sirupsen/logrus"
2018-06-10 22:50:03 +08:00
)
type Log struct {
LogLevel C.LogLevel
2018-06-22 13:15:20 +08:00
Payload string
2018-06-10 22:50:03 +08:00
}
2018-06-18 11:31:49 +08:00
func (l *Log) Type() string {
return l.LogLevel.String()
2018-06-18 11:31:49 +08:00
}
2018-06-10 22:50:03 +08:00
func print(data Log) {
2018-06-22 13:15:20 +08:00
switch data.LogLevel {
case C.INFO:
2018-06-10 22:50:03 +08:00
log.Infoln(data.Payload)
case C.WARNING:
2018-06-10 22:50:03 +08:00
log.Warnln(data.Payload)
case C.ERROR:
2018-06-10 22:50:03 +08:00
log.Errorln(data.Payload)
case C.DEBUG:
2018-06-10 22:50:03 +08:00
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)
if data.LogLevel <= t.logLevel {
print(data)
}
2018-06-10 22:50:03 +08:00
}
}
func newLog(logLevel C.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
}
}