-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
89 lines (70 loc) · 1.51 KB
/
settings.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
package log
import (
"io"
"os"
"time"
"github.com/qdm12/log/internal/caller"
)
type settings struct {
writers []io.Writer
level *Level
timeFormat *string
component string
caller caller.Settings
}
// newSettings returns settings using the options given
// and with defaults set for any unset setting field.
func newSettings(options []Option) (s settings) {
for _, option := range options {
option(&s)
}
return s
}
func (s *settings) setDefaults() {
if len(s.writers) == 0 {
s.writers = []io.Writer{os.Stdout}
}
if s.level == nil {
value := LevelInfo
s.level = &value
}
if s.timeFormat == nil {
value := time.RFC3339
s.timeFormat = &value
}
s.caller.SetDefaults()
}
func (s *settings) copy() (settingsCopy settings) {
if s.writers != nil {
settingsCopy.writers = make([]io.Writer, len(s.writers))
copy(settingsCopy.writers, s.writers)
}
if s.level != nil {
level := *s.level
settingsCopy.level = &level
}
if s.timeFormat != nil {
timeFormat := *s.timeFormat
settingsCopy.timeFormat = &timeFormat
}
settingsCopy.component = s.component
settingsCopy.caller = s.caller.Copy()
return settingsCopy
}
func (s *settings) overrideWith(other settings) {
if len(other.writers) > 0 {
s.writers = other.writers
}
if other.level != nil {
value := *other.level
s.level = &value
}
if other.timeFormat != nil {
value := *other.timeFormat
s.timeFormat = &value
}
if other.component != "" {
s.component = other.component
}
s.caller.OverrideWith(other.caller)
}