-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.go
316 lines (272 loc) · 10.2 KB
/
funcs.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package slog
import (
"context"
"errors"
"io"
"log"
"time"
"github.com/hedzr/is"
)
// IsTty detects a writer if it is abstracting from a tty (console, terminal) device.
func IsTty(w io.Writer) bool { return is.Tty(w) }
// IsColoredTty detects a writer if it is a colorful tty device.
//
// A colorful tty device can receive ANSI escaped sequences and draw its.
func IsColoredTty(w io.Writer) bool { return is.ColoredTty(w) }
// IsTtyEscaped detects a string if it contains ansi color escaped sequences
func IsTtyEscaped(s string) bool { return is.TtyEscaped(s) }
// StripEscapes removes any ansi color escaped sequences from a string
func StripEscapes(str string) (strCleaned string) { return is.StripEscapes(str) }
// ReadPassword reads the password from stdin with safe protection
func ReadPassword() (text string, err error) { return is.ReadPassword() }
// GetTtySize returns the window size in columns and rows in the active console window.
// The return value of this function is in the order of cols, rows.
func GetTtySize() (cols, rows int) { return is.GetTtySize() }
//
//
//
func Panic(msg string, args ...any) { logctx(PanicLevel, msg, args...) } // Panic with Default Logger.
func Fatal(msg string, args ...any) { logctx(FatalLevel, msg, args...) } // Fatal with Default Logger.
func Error(msg string, args ...any) { logctx(ErrorLevel, msg, args...) } // Error with Default Logger.
func Warn(msg string, args ...any) { logctx(WarnLevel, msg, args...) } // Warn with Default Logger.
func Info(msg string, args ...any) { logctx(InfoLevel, msg, args...) } // Info with Default Logger.
func Debug(msg string, args ...any) { logctx(DebugLevel, msg, args...) } // Debug with Default Logger.
func Trace(msg string, args ...any) { logctx(TraceLevel, msg, args...) } // Trace with Default Logger.
func Print(msg string, args ...any) { logctx(AlwaysLevel, msg, args...) } // Print with Default Logger.
func OK(msg string, args ...any) { logctx(OKLevel, msg, args...) } // OK with Default Logger.
func Success(msg string, args ...any) { logctx(SuccessLevel, msg, args...) } // Success with Default Logger.
func Fail(msg string, args ...any) { logctx(FailLevel, msg, args...) } // Fail with Default Logger.
func Verbose(msg string, args ...any) { vlogctx(context.TODO(), msg, args...) } // Verbose with Default Logger.
func VerboseContext(ctx context.Context, msg string, args ...any) { vlogctx(ctx, msg, args...) } // Verbose with Default Logger.
// Println with Default Logger.
func Println(args ...any) {
if len(args) == 0 {
logctx(AlwaysLevel, "")
return
}
var msg string
msg, args = args[0].(string), args[1:]
logctx(AlwaysLevel, msg, args...)
}
func logctx(lvl Level, msg string, args ...any) {
ctx := context.Background()
switch s := defaultLog.(type) {
case *logimp:
if s.EnabledContext(ctx, lvl) {
pc := getpc(3, s.extraFrames) // caller -> slog.Info -> logctx (this func)
s.logContext(ctx, lvl, pc, msg, args...)
}
case *entry:
if s.EnabledContext(ctx, lvl) {
pc := getpc(3, s.extraFrames) // caller -> slog.Info -> logctx (this func)
s.logContext(ctx, lvl, pc, msg, args...)
}
}
}
func logctxctx(ctx context.Context, lvl Level, msg string, args ...any) {
switch s := defaultLog.(type) {
case *logimp:
if s.EnabledContext(ctx, lvl) {
pc := getpc(3, s.extraFrames) // caller -> slog.Info -> logctx (this func)
s.logContext(ctx, lvl, pc, msg, args...)
}
case *entry:
if s.EnabledContext(ctx, lvl) {
pc := getpc(3, s.extraFrames) // caller -> slog.Info -> logctx (this func)
s.logContext(ctx, lvl, pc, msg, args...)
}
}
}
//
// PanicContext with Default Logger.
func PanicContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, PanicLevel, msg, args...)
}
// FatalContext with Default Logger.
func FatalContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, FatalLevel, msg, args...)
}
// ErrorContext with Default Logger.
func ErrorContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, ErrorLevel, msg, args...)
}
// WarnContext with Default Logger.
func WarnContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, WarnLevel, msg, args...)
}
// InfoContext with Default Logger.
func InfoContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, InfoLevel, msg, args...)
}
// DebugContext with Default Logger.
func DebugContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, DebugLevel, msg, args...)
}
// TraceContext with Default Logger.
func TraceContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, TraceLevel, msg, args...)
}
// PrintContext with Default Logger.
func PrintContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, AlwaysLevel, msg, args...)
}
// PrintlnContext with Default Logger.
func PrintlnContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, AlwaysLevel, msg, args...)
}
// OKContext with Default Logger.
func OKContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, OKLevel, msg, args...)
}
// SuccessContext with Default Logger.
func SuccessContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, SuccessLevel, msg, args...)
}
// FailContext with Default Logger.
func FailContext(ctx context.Context, msg string, args ...any) {
logctxctx(ctx, FailLevel, msg, args...)
}
//
//
//
func String(key string, val string) Attr { return &kvp{key, val} }
func Bool(key string, val bool) Attr { return &kvp{key, val} }
func Int(key string, val int) Attr { return &kvp{key, val} }
func Int8(key string, val int8) Attr { return &kvp{key, val} }
func Int16(key string, val int16) Attr { return &kvp{key, val} }
func Int32(key string, val int32) Attr { return &kvp{key, val} }
func Int64(key string, val int64) Attr { return &kvp{key, val} }
func Uint(key string, val uint) Attr { return &kvp{key, val} }
func Uint8(key string, val uint8) Attr { return &kvp{key, val} }
func Uint16(key string, val uint16) Attr { return &kvp{key, val} }
func Uint32(key string, val uint32) Attr { return &kvp{key, val} }
func Uint64(key string, val uint64) Attr { return &kvp{key, val} }
func Float32(key string, val float32) Attr { return &kvp{key, val} }
func Float64(key string, val float64) Attr { return &kvp{key, val} }
func Complex64(key string, val complex64) Attr { return &kvp{key, val} }
func Complex128(key string, val complex128) Attr { return &kvp{key, val} }
func Time(key string, val time.Time) Attr { return &kvp{key, val} }
func Duration(key string, val time.Duration) Attr { return &kvp{key, val} }
func Any(key string, val any) Attr { return &kvp{key, val} }
func Numeric[T Numerics](key string, val T) Attr { return &kvp{key, val} }
func Group(key string, args ...any) Attr {
var g = &gkvp{key: key, items: argsToAttrs(nil, args...)}
return g
}
func buildAttrs(as ...any) (kvps Attrs) { return argsToAttrs(nil, as...) }
func buildUniqueAttrs(keys map[string]bool, as ...any) (kvps Attrs) { return argsToAttrs(keys, as...) }
func argsToAttrs(keysKnown map[string]bool, args ...any) (kvps Attrs) {
var key string
if keysKnown == nil {
// keysKnown = make(map[string]bool)
for _, it := range args {
if key == "" {
switch k := it.(type) {
case string:
key = k
case Attr:
kvps = append(kvps, k)
key = ""
case []Attr:
for _, el := range k {
kvps = append(kvps, el)
}
key = ""
case Attrs:
for _, el := range k {
kvps = append(kvps, el)
}
key = ""
default:
// raiseerror(`bad sequences. The right list should be:
// NewGroupedAttrEasy("key", "attr1", 1, "attr2", false)`)
hintInternal(errUnmatchedPair, "expecting 'key' and 'value' pair in 'args' list, but unmatched 'key' found") // args must be key and value pair, key should be a string
}
} else {
kvps = append(kvps, NewAttr(key, it))
key = ""
}
}
return
}
for _, it := range args {
if key == "" {
switch k := it.(type) {
case string:
key = k
case Attr:
if _, ok := keysKnown[k.Key()]; !ok {
kvps = append(kvps, k)
keysKnown[k.Key()] = true
}
key = ""
case []Attr:
for _, el := range k {
if _, ok := keysKnown[el.Key()]; !ok {
kvps = append(kvps, el)
keysKnown[el.Key()] = true
}
}
key = ""
case Attrs:
for _, el := range k {
if _, ok := keysKnown[el.Key()]; !ok {
kvps = append(kvps, el)
keysKnown[el.Key()] = true
}
}
key = ""
default:
// raiseerror(`bad sequences. The right list should be:
// NewGroupedAttrEasy("key", "attr1", 1, "attr2", false)`)
hintInternal(errUnmatchedPair, "expecting 'key' and 'value' pair in 'args' list, but unmatched 'key' found") // args must be key and value pair, key should be a string
}
} else {
kvps = setUniqueKvp(keysKnown, kvps, key, it)
keysKnown[key] = true
key = ""
}
}
return
}
func setUniqueKvp(keys map[string]bool, kvps []Attr, key string, val any) []Attr {
if _, ok := keys[key]; ok {
for ix, iv := range kvps {
if iv.Key() == key {
kvps[ix].SetValue(val)
break
}
}
} else {
kvps = append(kvps, NewAttr(key, val))
keys[key] = true
}
return kvps
}
// NewLogLogger returns a new log.Logger such that each call to its Output method
// dispatches a Record to the specified handler. The logger acts as a bridge from
// the older log API to newer structured logging handlers.
func NewLogLogger(h Logger, lvl Level) *log.Logger {
return log.New(&handlerWriter{h, lvl, true, 0}, "", 0)
}
type handlerWriter struct {
l Logger
lvl Level
capturePC bool
extraFrames int
}
func (s *handlerWriter) Write(buf []byte) (n int, err error) {
if s.lvl >= s.l.Level() {
var pc uintptr
if s.capturePC {
// skip [runtime.Callers, s.Write, Logger.Output, log.Print]
pc = getpc(4, s.extraFrames)
}
if h, ok := s.l.(LogLoggerAware); ok {
n, err = h.WriteInternal(context.Background(), s.lvl, pc, buf)
}
}
return
}
var errUnmatchedPair = errors.New("unmatched (key,value) pair.")
// var err // "args must be key and value pair, key should be a string"