2018-06-10 22:50:03 +08:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-06-10 22:50:03 +08:00
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Log struct {
|
2018-07-26 00:04:59 +08:00
|
|
|
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 {
|
2018-07-26 00:04:59 +08:00
|
|
|
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 {
|
2018-07-26 00:04:59 +08:00
|
|
|
case C.INFO:
|
2018-06-10 22:50:03 +08:00
|
|
|
log.Infoln(data.Payload)
|
2018-07-26 00:04:59 +08:00
|
|
|
case C.WARNING:
|
2018-06-10 22:50:03 +08:00
|
|
|
log.Warnln(data.Payload)
|
2018-07-26 00:04:59 +08:00
|
|
|
case C.ERROR:
|
2018-06-10 22:50:03 +08:00
|
|
|
log.Errorln(data.Payload)
|
2018-07-26 00:04:59 +08:00
|
|
|
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)
|
2018-07-26 00:04:59 +08:00
|
|
|
if data.LogLevel <= t.logLevel {
|
|
|
|
print(data)
|
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +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
|
|
|
}
|
|
|
|
}
|