-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.go
More file actions
107 lines (90 loc) · 2.5 KB
/
Copy pathlevel.go
File metadata and controls
107 lines (90 loc) · 2.5 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
101
102
103
104
105
106
107
package value
import (
"fmt"
"strconv"
"github.com/echocat/slf4g/level"
nlevel "github.com/echocat/slf4g/native/level"
)
// LevelTarget defines an object that receives the level.Level managed
// by the Level value facade.
type LevelTarget interface {
level.MutableAware
}
// Level is a value facade for transparent setting of level.Level for
// the slf4g/native implementation. This is quite handy for usage with
// flags package of the SDK or similar flag libraries. This might be
// usable, too in contexts where serialization might be required.
type Level struct {
// Target is the instance of LevelTarget which should be configured
// by this facade.
Target LevelTarget
// Names is used to transform provided plain data. If this is not defined
// the Target is assumed as nlevel.NamesAware or if this even does not work
// nlevel.DefaultNames is used.
Names level.Names
}
// NewLevel creates a new instance of Level with the given target.
func NewLevel(target LevelTarget, customizer ...func(*Level)) Level {
result := Level{
Target: target,
}
for _, c := range customizer {
c(&result)
}
return result
}
// Set implements flag.Value.
func (instance Level) Set(plain string) error {
if l, err := strconv.ParseUint(plain, 10, 16); err == nil {
instance.Target.SetLevel(level.Level(l))
return nil
}
l, err := instance.getNames().ToLevel(plain)
if err != nil {
return err
}
instance.Target.SetLevel(l)
return nil
}
// Get implements flag.Getter.
func (instance Level) Get() interface{} {
return instance.Target.GetLevel()
}
// String implements flag.Value.
func (instance Level) String() string {
b, err := instance.MarshalText()
if err != nil {
return fmt.Sprintf("ERR-%v", err)
}
return string(b)
}
// Type returns the type as a string.
func (instance Level) Type() string {
return "logLevel"
}
// MarshalText implements encoding.TextMarshaler
func (instance Level) MarshalText() (text []byte, err error) {
if instance.Target == nil {
return nil, nil
}
name, err := instance.getNames().ToName(instance.Target.GetLevel())
return []byte(name), err
}
// UnmarshalText implements encoding.TextUnmarshaler
func (instance Level) UnmarshalText(text []byte) error {
return instance.Set(string(text))
}
func (instance Level) getNames() level.Names {
if v := instance.Names; v != nil {
return v
}
if va, ok := instance.Target.(level.NamesAware); ok {
if v := va.GetLevelNames(); v != nil {
return v
}
}
if v := nlevel.DefaultNames; v != nil {
return v
}
return nlevel.NewNames()
}