-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
124 lines (107 loc) · 2.8 KB
/
config.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
package L
import (
"io"
"os"
"runtime"
"strings"
)
// Config is the configuration of a logger.
type Config struct {
// Labels represents the set of labels associated with
// a logger.
Labels map[string]int `json:"labels,omitempty"`
// Pre is a sequence of Middlewares to pre-process
// loggable objects.
Pre []Middleware `json:"-"`
// Post is a sequence of Middlewares to post-process
// loggable objects. Post middlewares are called
// after .Log() and before .F.Fmt().
Post []Middleware `json:"-"`
// W is the writer for this logger.
W io.Writer `json:"-"`
// F is a Fmter for the logger.
F Fmter `json:"-"`
// E is a handler for any errors which occur during
// formatting.
E func(*Config, error) `json:"-"`
pkg string
}
// NewConfig returns a *Config with the associated labels. The labels are
// subject to the following expansion rules
//
// - a label starting with '.' is prefixed with package name.
func NewConfig(labels ...string) *Config {
pc, _, _, _ := runtime.Caller(1)
fn := runtime.FuncForPC(pc).Name()
i := strings.LastIndexByte(fn, byte('.'))
j := strings.IndexByte(fn, byte('('))
if j != -1 && j < i {
i = j - 1
}
pkg := fn
if i != -1 {
pkg = fn[:i]
}
c := &Config{
Labels: map[string]int{},
W: os.Stderr,
E: EPanic,
pkg: pkg,
}
for _, lbl := range labels {
c.Labels[c.Unlocalize(lbl)] = 0
}
return c
}
// Clone clones the configuration c.
func (c *Config) Clone() *Config {
res := &Config{}
*res = *c
res.Pre = append([]Middleware{}, c.Pre...)
res.Post = append([]Middleware{}, c.Post...)
res.Labels = make(map[string]int, len(c.Labels))
res.pkg = c.pkg
for k, v := range c.Labels {
res.Labels[k] = v
}
return res
}
// Unlocalize prefixes label with the c's package if
// label starts with '.'.
func (c *Config) Unlocalize(label string) string {
if label != "" && label[0] == '.' {
return c.pkg + label
}
return label
}
// localize strips the prefix '<pkg>.' if label
// starts with c's package.
func (c *Config) Localize(label string) string {
if strings.HasPrefix(label, c.pkg+".") {
return label[len(c.pkg):]
}
return label
}
// CurrentRootConfig retrieves a clone of the configuration
// from the last call to Apply, if any.
func CurrentRootConfig() *Config {
return root.ReadConfig()
}
func (c *Config) Package() string {
return c.pkg
}
// PackageConfig is a Config wrapper that exposes a Package Field.
// Config's have .Package() to make the package read-only, PackageConfig
// provides a json friendly wrapper.
type PackageConfig struct {
Config
Package string `json:"package"`
}
// ConfigNode represents a Config in a ConfigTree, assumed to take the
// form of []ConfigNode.
type ConfigNode struct {
PackageConfig
// The index of the parent in the the tree, or -1 if there is none
// (the root).
Parent int `json:"parent"`
}