clash/log/level.go

79 lines
1.4 KiB
Go
Raw Normal View History

2018-11-21 05:47:46 +00:00
package log
import (
"encoding/json"
"errors"
)
var (
// LogLevelMapping is a mapping for LogLevel enum
LogLevelMapping = map[string]LogLevel{
2018-12-05 13:13:29 +00:00
ERROR.String(): ERROR,
WARNING.String(): WARNING,
INFO.String(): INFO,
DEBUG.String(): DEBUG,
SILENT.String(): SILENT,
2018-11-21 05:47:46 +00:00
}
)
const (
DEBUG LogLevel = iota
INFO
WARNING
ERROR
2018-12-03 15:41:40 +00:00
SILENT
2018-11-21 05:47:46 +00:00
)
type LogLevel int
2018-12-05 13:13:29 +00:00
// UnmarshalYAML unserialize LogLevel with yaml
func (l *LogLevel) UnmarshalYAML(unmarshal func(interface{}) error) error {
2018-11-21 05:47:46 +00:00
var tp string
2018-12-05 13:13:29 +00:00
unmarshal(&tp)
2018-11-21 05:47:46 +00:00
level, exist := LogLevelMapping[tp]
if !exist {
return errors.New("invalid mode")
}
*l = level
return nil
}
2018-12-05 13:13:29 +00:00
// UnmarshalJSON unserialize LogLevel with json
2018-11-21 05:47:46 +00:00
func (l *LogLevel) UnmarshalJSON(data []byte) error {
var tp string
2018-11-28 02:38:30 +00:00
json.Unmarshal(data, &tp)
2018-11-21 05:47:46 +00:00
level, exist := LogLevelMapping[tp]
if !exist {
return errors.New("invalid mode")
}
*l = level
return nil
}
2018-12-05 13:13:29 +00:00
// MarshalJSON serialize LogLevel with json
2018-11-21 05:47:46 +00:00
func (l LogLevel) MarshalJSON() ([]byte, error) {
return json.Marshal(l.String())
}
// MarshalYAML serialize LogLevel with yaml
func (l LogLevel) MarshalYAML() (interface{}, error) {
return l.String(), nil
}
2018-11-21 05:47:46 +00:00
func (l LogLevel) String() string {
switch l {
case INFO:
return "info"
case WARNING:
return "warning"
case ERROR:
return "error"
case DEBUG:
return "debug"
2018-12-03 15:41:40 +00:00
case SILENT:
return "silent"
2018-11-21 05:47:46 +00:00
default:
2018-12-05 13:13:29 +00:00
return "unknown"
2018-11-21 05:47:46 +00:00
}
}