-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevels.go
136 lines (120 loc) · 2.82 KB
/
levels.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package golog
import "strconv"
var DefaultLevels = Levels{
Trace: -20,
Debug: -10,
Info: 0,
Warn: 10,
Error: 20,
Fatal: 30,
Names: map[Level]string{
-20: "TRACE",
-10: "DEBUG",
0: "INFO",
10: "WARN",
20: "ERROR",
30: "FATAL",
},
}
type Levels struct {
Trace Level
Debug Level
Info Level
Warn Level
Error Level
Fatal Level
Names map[Level]string
}
// Name returns the name of a level if available
// or the integer value of the level as string.
func (l *Levels) Name(level Level) string {
if name, ok := l.Names[level]; ok {
return name
}
return strconv.Itoa(int(level))
}
func (l *Levels) HasName(level Level) bool {
_, has := l.Names[level]
return has
}
func (l *Levels) FatalName() string {
return l.Name(l.Fatal)
}
func (l *Levels) ErrorName() string {
return l.Name(l.Error)
}
func (l *Levels) WarnName() string {
return l.Name(l.Warn)
}
func (l *Levels) InfoName() string {
return l.Name(l.Info)
}
func (l *Levels) DebugName() string {
return l.Name(l.Debug)
}
func (l *Levels) TraceName() string {
return l.Name(l.Trace)
}
// LevelOfName returns the level with a give name.
// If name is formatted as an integer and within [LevelMin..LevelMax]
// then a Level with that integer value will be returned.
// This is the inverse operation to what Levels.Name(unnamedLevel) returns.
// If there is no level with name or valid integer value
// then LevelInvalid will be returned.
func (l *Levels) LevelOfName(name string) Level {
for level, levelName := range l.Names {
if name == levelName {
return level
}
}
if i, err := strconv.Atoi(name); err == nil && i >= int(LevelMin) && i <= int(LevelMax) {
return Level(i)
}
return LevelInvalid
}
// LevelOfNameOrDefault returns the level with a given name,
// or defaultLevel if the name is not in Levels.Names.
func (l *Levels) LevelOfNameOrDefault(name string, defaultLevel Level) Level {
for level, levelName := range l.Names {
if name == levelName {
return level
}
}
return defaultLevel
}
func (l *Levels) NameLenRange() (min, max int) {
for _, name := range l.Names {
nameLen := len(name)
if nameLen < min {
min = nameLen
}
if nameLen > max {
max = nameLen
}
}
return min, max
}
func (l *Levels) CopyWithLeftPaddedNames() *Levels {
padded := *l
padded.Names = make(map[Level]string, len(l.Names))
_, maxLen := l.NameLenRange()
for level, name := range l.Names {
padded.Names[level] = name
for len(padded.Names[level]) < maxLen {
padded.Names[level] = " " + padded.Names[level]
}
}
return &padded
}
func (l *Levels) CopyWithRightPaddedNames() *Levels {
padded := *l
padded.Names = make(map[Level]string, len(l.Names))
_, maxLen := l.NameLenRange()
for level, name := range l.Names {
padded.Names[level] = name
for len(padded.Names[level]) < maxLen {
padded.Names[level] += " "
}
}
return &padded
}