-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseverity.go
More file actions
100 lines (86 loc) · 2.04 KB
/
Copy pathseverity.go
File metadata and controls
100 lines (86 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package logng
import (
"strings"
)
// Severity describes the severity level of Log.
type Severity int
const (
// SeverityNone is none or unspecified severity level.
SeverityNone Severity = iota
// SeverityFatal is the fatal severity level.
SeverityFatal
// SeverityError is the error severity level.
SeverityError
// SeverityWarning is the warning severity level.
SeverityWarning
// SeverityInfo is the info severity level.
SeverityInfo
// SeverityDebug is the debug severity level.
SeverityDebug
)
// IsValid returns whether s is valid.
func (s Severity) IsValid() bool {
return s.CheckValid() == nil
}
// CheckValid returns ErrInvalidSeverity for invalid s.
func (s Severity) CheckValid() error {
if !(SeverityNone <= s && s <= SeverityDebug) {
return ErrInvalidSeverity
}
return nil
}
// String is the implementation of fmt.Stringer.
func (s Severity) String() string {
text, _ := s.MarshalText()
return string(text)
}
// MarshalText is the implementation of encoding.TextMarshaler.
// If s is invalid, it returns the error from Severity.CheckValid.
func (s Severity) MarshalText() (text []byte, err error) {
if e := s.CheckValid(); e != nil {
return nil, e
}
var str string
switch s {
case SeverityNone:
str = "NONE"
case SeverityFatal:
str = "FATAL"
case SeverityError:
str = "ERROR"
case SeverityWarning:
str = "WARNING"
case SeverityInfo:
str = "INFO"
case SeverityDebug:
str = "DEBUG"
default:
panic("invalid severity")
}
return []byte(str), nil
}
// UnmarshalText is the implementation of encoding.TextUnmarshaler.
// If text is unknown, it returns ErrUnknownSeverity.
func (s *Severity) UnmarshalText(text []byte) error {
switch str := strings.ToUpper(string(text)); str {
case "NONE":
*s = SeverityNone
case "FATAL":
*s = SeverityFatal
case "ERROR":
*s = SeverityError
case "WARNING":
*s = SeverityWarning
case "INFO":
*s = SeverityInfo
case "DEBUG":
*s = SeverityDebug
default:
return ErrUnknownSeverity
}
return nil
}
// custom severities
const (
severityPrint Severity = -iota - 1
)