-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmn.go
180 lines (158 loc) · 5.07 KB
/
cmn.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package slog
import (
"regexp"
"sync"
"github.com/hedzr/is/term/color"
)
// AddCodeHostingProviders appends more provider, repl pair to reduce the caller text width.
//
// The builtin providers are:
// - "github.com" -> "GH"
// - "gitlab.com" -> "GL"
// - "gitee.com" -> "GT"
// - "bitbucket.com" -> "BB"
// - ...
func AddCodeHostingProviders(provider, repl string) { codeHostingProvidersMap[provider] = repl }
// AddKnownPathMapping appends more (pathname, repl) pair to reduce the caller filepath width.
//
// Such as:
// - "$HOME" -> "~"
// - pwd -> "." (current directory -> '.', that means any abs-path will be converted to rel-path)
func AddKnownPathMapping(pathname, repl string) { knownPathMap[pathname] = repl }
// AddKnownPathRegexpMapping adds regexp pattern, repl pair to reduce the called filepath width.
func AddKnownPathRegexpMapping(pathnameRegexpExpr, repl string) {
knownPathRegexpMap = append(knownPathRegexpMap, regRepl{
expr: regexp.MustCompile(pathnameRegexpExpr),
repl: repl,
})
}
// SetLevelOutputWidth sets how many characters of level string should
// be formatted and output to logging lines.
//
// While you are customizing your level, a 1..5 characters array is
// required for formatting purpose.
//
// For example:
//
// const NoticeLevel = slog.Level(17) // A custom level must have a value greater than slog.MaxLevel
// slog.RegisterLevel(NoticeLevel, "NOTICE",
// slog.RegWithShortTags([6]string{"", "N", "NT", "NTC", "NOTC", "NOTIC"}),
// slog.RegWithColor(color.FgWhite, color.BgUnderline),
// slog.RegWithTreatedAsLevel(slog.InfoLevel),
// ))
func SetLevelOutputWidth(width int) {
if width >= 0 && width <= 5 {
levelOutputWidth = width
}
}
// SetMessageMinimalWidth modify the minimal width between
// message and attributes. It works for only colorful mode.
//
// The default width is 36, that means a message will be left
// padding to take 36 columns, filled by space char (' ').
func SetMessageMinimalWidth(w int) {
if w >= 16 {
minimalMessageWidth = w
}
}
func GetFlags() Flags { return flags } // returns logg/slog Flags
func SetFlags(f Flags) { flags = f } // sets logg/slog Flags
func ResetFlags() { flags = LstdFlags } // resets logg/slog Flags to factory settings
func IsAnyBitsSet(f Flags) bool { return flags&f != 0 } // detects if any of some Flags are set
func IsAllBitsSet(f Flags) bool { return flags&f == f } // detects if all of given Flags are both set
// AddFlags adds some Flags (bitwise Or operation).
//
// These ways can merge flags into internal settings:
//
// AddFlags(Lprivacypathregexp | Lprivacypath)
// AddFlags(Lprivacypathregexp, Lprivacypath)
func AddFlags(flagsToAdd ...Flags) {
for _, f := range flagsToAdd {
flags |= f
}
}
// RemoveFlags removes some Flags (bitwise And negative operation).
//
// These ways can strip flags off from internal settings:
//
// RemoveFlags(Lprivacypathregexp | Lprivacypath)
// RemoveFlags(Lprivacypathregexp, Lprivacypath)
func RemoveFlags(flagsToRemove ...Flags) {
for _, f := range flagsToRemove {
flags &= ^f
}
}
// SaveFlagsAndMod saves old flags, modify it, and restore the old at defer time.
//
// A typical usage might be:
//
// // Inside a test case, you wanna add date part to timestamp output,
// // and disable panic (by Panic or Fatal) breaking the testing flow.
// // So this line will make those temporary modifications:
// defer SaveFlagsAndMod(Ldate | LnoInterrupt)()
// // ...
// // concrete testing codes here
func SaveFlagsAndMod(addingFlags Flags, removingFlags ...Flags) (deferFn func()) {
save := flags
AddFlags(addingFlags)
for _, f := range removingFlags {
RemoveFlags(f)
}
return func() {
flags = save
}
}
// func SaveFlagsAndMod(actions ...func()) (deferFn func()) {
// save := flags
// for _, action := range actions {
// action()
// }
// return func() {
// flags = save
// }
// }
// Reset clear user settings and restore Default to default.
func Reset() {
ResetLevel()
ResetFlags()
}
func SetDefault(l Logger) { defaultLog = l } // sets user-defined logger as Default
func Default() Logger { return defaultLog } // return native default logger
var (
defaultWriter *dualWriter
defaultLog Logger // builtin logger as default device, see func init()
onceInit sync.Once
)
// const (
// STDLOG = "std"
// GOLOG = "go/log"
// )
var (
lvlCurrent Level
flags = LstdFlags
minimalMessageWidth = 36
levelOutputWidth = 3
)
const (
clrNone = color.NoColor
clrBasic = color.FgLightMagenta
darkGray = color.FgDarkGray
lightGray = color.FgLightGray
cyan = color.FgCyan
hiRed = color.FgLightRed
red = color.FgRed
yellow = color.FgYellow
clrTimestamp = color.FgGreen
clrFuncName = darkGray
clrAttrKey = darkGray
clrAttrKeyBg = clrNone
clrLoggerName = color.FgLightGray
clrLoggerNameBg = clrNone
)
const (
timestampFieldName = "time"
levelFieldName = "level"
callerFieldName = "caller"
messageFieldName = "msg"
)
// var errNotReady = errors.New("not ready") // here we just need a very simple error message object