mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-20 00:50:06 +08:00
Some checks failed
Test / test (1.20, ubuntu-latest) (push) Failing after 1s
Test / test (1.21, ubuntu-latest) (push) Failing after 1s
Test / test (1.22, ubuntu-latest) (push) Failing after 1s
Test / test (1.23, ubuntu-latest) (push) Failing after 0s
Test / test (1.24, ubuntu-latest) (push) Failing after 1s
Trigger CMFA Update / trigger-CMFA-update (push) Failing after 1s
Test / test (1.20, macos-13) (push) Has been cancelled
Test / test (1.20, macos-latest) (push) Has been cancelled
Test / test (1.20, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.20, windows-latest) (push) Has been cancelled
Test / test (1.21, macos-13) (push) Has been cancelled
Test / test (1.21, macos-latest) (push) Has been cancelled
Test / test (1.21, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.21, windows-latest) (push) Has been cancelled
Test / test (1.22, macos-13) (push) Has been cancelled
Test / test (1.22, macos-latest) (push) Has been cancelled
Test / test (1.22, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.22, windows-latest) (push) Has been cancelled
Test / test (1.23, macos-13) (push) Has been cancelled
Test / test (1.23, macos-latest) (push) Has been cancelled
Test / test (1.23, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.23, windows-latest) (push) Has been cancelled
Test / test (1.24, macos-13) (push) Has been cancelled
Test / test (1.24, macos-latest) (push) Has been cancelled
Test / test (1.24, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.24, windows-latest) (push) Has been cancelled
93 lines
1.7 KiB
Go
93 lines
1.7 KiB
Go
package log
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
// LogLevelMapping is a mapping for LogLevel enum
|
|
var LogLevelMapping = map[string]LogLevel{
|
|
ERROR.String(): ERROR,
|
|
WARNING.String(): WARNING,
|
|
INFO.String(): INFO,
|
|
DEBUG.String(): DEBUG,
|
|
SILENT.String(): SILENT,
|
|
}
|
|
|
|
const (
|
|
DEBUG LogLevel = iota
|
|
INFO
|
|
WARNING
|
|
ERROR
|
|
SILENT
|
|
)
|
|
|
|
type LogLevel int
|
|
|
|
// UnmarshalYAML unserialize LogLevel with yaml
|
|
func (l *LogLevel) UnmarshalYAML(unmarshal func(any) error) error {
|
|
var tp string
|
|
unmarshal(&tp)
|
|
level, exist := LogLevelMapping[strings.ToLower(tp)]
|
|
if !exist {
|
|
return errors.New("invalid log-level")
|
|
}
|
|
*l = level
|
|
return nil
|
|
}
|
|
|
|
// UnmarshalJSON unserialize LogLevel with json
|
|
func (l *LogLevel) UnmarshalJSON(data []byte) error {
|
|
var tp string
|
|
json.Unmarshal(data, &tp)
|
|
level, exist := LogLevelMapping[strings.ToLower(tp)]
|
|
if !exist {
|
|
return errors.New("invalid log-level")
|
|
}
|
|
*l = level
|
|
return nil
|
|
}
|
|
|
|
// UnmarshalText unserialize LogLevel
|
|
func (l *LogLevel) UnmarshalText(data []byte) error {
|
|
level, exist := LogLevelMapping[strings.ToLower(string(data))]
|
|
if !exist {
|
|
return errors.New("invalid log-level")
|
|
}
|
|
*l = level
|
|
return nil
|
|
}
|
|
|
|
// MarshalYAML serialize LogLevel with yaml
|
|
func (l LogLevel) MarshalYAML() (any, error) {
|
|
return l.String(), nil
|
|
}
|
|
|
|
// MarshalJSON serialize LogLevel with json
|
|
func (l LogLevel) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(l.String())
|
|
}
|
|
|
|
// MarshalText serialize LogLevel
|
|
func (l LogLevel) MarshalText() ([]byte, error) {
|
|
return []byte(l.String()), nil
|
|
}
|
|
|
|
func (l LogLevel) String() string {
|
|
switch l {
|
|
case INFO:
|
|
return "info"
|
|
case WARNING:
|
|
return "warning"
|
|
case ERROR:
|
|
return "error"
|
|
case DEBUG:
|
|
return "debug"
|
|
case SILENT:
|
|
return "silent"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|