-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmn.go
149 lines (129 loc) · 4.35 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
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 could be:
// - "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 AddFlags(f Flags) { flags |= f } // adds some Flags (bitwise Or operation)
func RemoveFlags(f Flags) { flags &= ^f } // removes some Flags (bitwise And negative operation)
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
// SaveFlagsAndMod saves old flags, modify it, and restore the old at defer time
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