-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformatter.go
123 lines (101 loc) · 3.01 KB
/
formatter.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
package value
import (
"fmt"
"github.com/echocat/slf4g/native/color"
"github.com/echocat/slf4g/native/formatter"
)
// FormatterTarget defines an object that receives the consumer.Consumer managed
// by the Formatter value facade.
type FormatterTarget interface {
formatter.MutableAware
}
// Formatter is a value facade for transparent setting of consumer.Consumer
// 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 Formatter struct {
// Target is the instance of consumer.MutableAware which should be
// configured by this facade.
Target FormatterTarget
// Codec is used to transform provided plain data. If this is not defined
// DefaultFormatterCodec is used.
Codec FormatterCodec
// ColorMode enables in cases where the Target is color.ModeMutableAware
// to modify the value of it.
// This will not always work. Not all targets supports it. In this case
// the value will be swallowed.
ColorMode *FormatterColorMode
}
// NewFormatter creates a new instance of Formatter with the given target.
func NewFormatter(target FormatterTarget, customizer ...func(*Formatter)) Formatter {
result := Formatter{
Target: target,
}
result.ColorMode = &FormatterColorMode{
result.getColorMode,
result.setColorMode,
nil,
}
for _, c := range customizer {
c(&result)
}
return result
}
// Set implements flag.Value.
func (instance Formatter) Set(plain string) error {
v, err := instance.getCodec().Parse(plain)
if err != nil {
return err
}
instance.Target.SetFormatter(v)
return instance.setColorMode(instance.ColorMode.get())
}
// Get implements flag.Getter.
func (instance Formatter) Get() interface{} {
return instance.Target.GetFormatter()
}
// String implements flag.Value.
func (instance Formatter) String() string {
b, err := instance.MarshalText()
if err != nil {
return fmt.Sprintf("ERR-%v", err)
}
return string(b)
}
func (instance Formatter) getColorMode() (color.Mode, bool) {
if t, ok := instance.Get().(color.ModeAware); ok {
return t.GetColorMode(), true
}
return 0, false
}
func (instance Formatter) setColorMode(v color.Mode) error {
if t, ok := instance.Get().(color.ModeMutableAware); ok {
t.SetColorMode(v)
}
return nil
}
// Type returns the type as a string.
func (instance Formatter) Type() string {
return "logFormatter"
}
// MarshalText implements encoding.TextMarshaler
func (instance Formatter) MarshalText() (text []byte, err error) {
if instance.Target == nil {
return nil, nil
}
name, err := instance.getCodec().Format(instance.Target.GetFormatter())
return []byte(name), err
}
// UnmarshalText implements encoding.TextUnmarshaler
func (instance Formatter) UnmarshalText(text []byte) error {
return instance.Set(string(text))
}
func (instance Formatter) getCodec() FormatterCodec {
if v := instance.Codec; v != nil {
return v
}
if v := DefaultFormatterCodec; v != nil {
return v
}
return NoopFormatterCodec()
}