github.com/igoogolx/clash@v1.19.8/log/level.go (about)

     1  package log
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  )
     7  
     8  // LogLevelMapping is a mapping for LogLevel enum
     9  var LogLevelMapping = map[string]LogLevel{
    10  	ERROR.String():   ERROR,
    11  	WARNING.String(): WARNING,
    12  	INFO.String():    INFO,
    13  	DEBUG.String():   DEBUG,
    14  	SILENT.String():  SILENT,
    15  }
    16  
    17  const (
    18  	DEBUG LogLevel = iota
    19  	INFO
    20  	WARNING
    21  	ERROR
    22  	SILENT
    23  )
    24  
    25  type LogLevel int
    26  
    27  // UnmarshalYAML unserialize LogLevel with yaml
    28  func (l *LogLevel) UnmarshalYAML(unmarshal func(any) error) error {
    29  	var tp string
    30  	unmarshal(&tp)
    31  	level, exist := LogLevelMapping[tp]
    32  	if !exist {
    33  		return errors.New("invalid mode")
    34  	}
    35  	*l = level
    36  	return nil
    37  }
    38  
    39  // UnmarshalJSON unserialize LogLevel with json
    40  func (l *LogLevel) UnmarshalJSON(data []byte) error {
    41  	var tp string
    42  	json.Unmarshal(data, &tp)
    43  	level, exist := LogLevelMapping[tp]
    44  	if !exist {
    45  		return errors.New("invalid mode")
    46  	}
    47  	*l = level
    48  	return nil
    49  }
    50  
    51  // MarshalJSON serialize LogLevel with json
    52  func (l LogLevel) MarshalJSON() ([]byte, error) {
    53  	return json.Marshal(l.String())
    54  }
    55  
    56  // MarshalYAML serialize LogLevel with yaml
    57  func (l LogLevel) MarshalYAML() (any, error) {
    58  	return l.String(), nil
    59  }
    60  
    61  func (l LogLevel) String() string {
    62  	switch l {
    63  	case INFO:
    64  		return "info"
    65  	case WARNING:
    66  		return "warning"
    67  	case ERROR:
    68  		return "error"
    69  	case DEBUG:
    70  		return "debug"
    71  	case SILENT:
    72  		return "silent"
    73  	default:
    74  		return "unknown"
    75  	}
    76  }