-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verbosity.go
105 lines (93 loc) · 2.94 KB
/
verbosity.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
// Copyright (c) 2020-present Sven Greb <[email protected]>
// This source code is licensed under the MIT license found in the LICENSE file.
package nib
import (
"fmt"
"strings"
)
// Verbosity level names.
const (
VerbosityNameDebug = "debug"
VerbosityNameError = "error"
VerbosityNameFatal = "fatal"
VerbosityNameInfo = "info"
VerbosityNameSuccess = "success"
VerbosityNameSuppress = "suppress"
VerbosityNameUnknown = "unknown"
VerbosityNameWarn = "warn"
)
const (
// FatalVerbosity is the verbosity level of the line printer for fatal messages.
FatalVerbosity Verbosity = iota
// ErrorVerbosity is the verbosity level of the line printer for error messages.
ErrorVerbosity
// WarnVerbosity is the verbosity level of the line printer for warning messages.
WarnVerbosity
// SuccessVerbosity is verbosity the level of the line printer for success messages.
SuccessVerbosity
// InfoVerbosity is verbosity the level of the line printer for information messages.
InfoVerbosity
// DebugVerbosity is verbosity the level of the line printer for debug messages.
DebugVerbosity
// SuppressVerbosity is the verbosity level of the line printer to suppress messages.
SuppressVerbosity
)
// Verbosity defines the verbosity level of the line printer.
type Verbosity uint32
// ParseVerbosity takes a verbosity level name and returns the Verbosity level constant.
func ParseVerbosity(lvl string) (Verbosity, error) {
switch strings.ToLower(lvl) {
case VerbosityNameFatal:
return FatalVerbosity, nil
case VerbosityNameError:
return ErrorVerbosity, nil
case VerbosityNameWarn:
return WarnVerbosity, nil
case VerbosityNameSuccess:
return SuccessVerbosity, nil
case VerbosityNameInfo:
return InfoVerbosity, nil
case VerbosityNameDebug:
return DebugVerbosity, nil
case VerbosityNameSuppress:
return SuppressVerbosity, nil
}
var v Verbosity
return v, fmt.Errorf("not a valid level: %q", lvl)
}
// MarshalText returns the textual representation of itself.
func (v Verbosity) MarshalText() ([]byte, error) {
switch v {
case FatalVerbosity:
return []byte(VerbosityNameFatal), nil
case ErrorVerbosity:
return []byte(VerbosityNameError), nil
case WarnVerbosity:
return []byte(VerbosityNameWarn), nil
case SuccessVerbosity:
return []byte(VerbosityNameSuccess), nil
case InfoVerbosity:
return []byte(VerbosityNameInfo), nil
case DebugVerbosity:
return []byte(VerbosityNameDebug), nil
case SuppressVerbosity:
return []byte(VerbosityNameSuppress), nil
}
return nil, fmt.Errorf("not a valid level %d", v)
}
// Convert the Verbosity to a string.
func (v Verbosity) String() string {
if b, err := v.MarshalText(); err == nil {
return string(b)
}
return VerbosityNameUnknown
}
// UnmarshalText implements encoding.TextUnmarshaler to unmarshal a textual representation of itself.
func (v *Verbosity) UnmarshalText(text []byte) error {
l, err := ParseVerbosity(string(text))
if err != nil {
return err
}
*v = l
return nil
}