mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 13:41:23 +08:00
86 lines
1.4 KiB
Go
86 lines
1.4 KiB
Go
package log
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/Dreamacro/clash/common/observable"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
logCh = make(chan interface{})
|
|
source = observable.NewObservable(logCh)
|
|
level = INFO
|
|
)
|
|
|
|
type Event struct {
|
|
LogLevel LogLevel
|
|
Payload string
|
|
}
|
|
|
|
func (e *Event) Type() string {
|
|
return e.LogLevel.String()
|
|
}
|
|
|
|
func Infoln(format string, v ...interface{}) {
|
|
event := newLog(INFO, format, v...)
|
|
logCh <- event
|
|
print(event)
|
|
}
|
|
|
|
func Warnln(format string, v ...interface{}) {
|
|
event := newLog(WARNING, format, v...)
|
|
logCh <- event
|
|
print(event)
|
|
}
|
|
|
|
func Errorln(format string, v ...interface{}) {
|
|
event := newLog(ERROR, format, v...)
|
|
logCh <- event
|
|
print(event)
|
|
}
|
|
|
|
func Debugln(format string, v ...interface{}) {
|
|
event := newLog(DEBUG, format, v...)
|
|
logCh <- event
|
|
print(event)
|
|
}
|
|
|
|
func Subscribe() observable.Subscription {
|
|
sub, _ := source.Subscribe()
|
|
return sub
|
|
}
|
|
|
|
func Level() LogLevel {
|
|
return level
|
|
}
|
|
|
|
func SetLevel(newLevel LogLevel) {
|
|
level = newLevel
|
|
}
|
|
|
|
func print(data *Event) {
|
|
if data.LogLevel < level {
|
|
return
|
|
}
|
|
|
|
switch data.LogLevel {
|
|
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 newLog(logLevel LogLevel, format string, v ...interface{}) *Event {
|
|
return &Event{
|
|
LogLevel: logLevel,
|
|
Payload: fmt.Sprintf(format, v...),
|
|
}
|
|
}
|