-
Notifications
You must be signed in to change notification settings - Fork 0
/
emo.go
511 lines (441 loc) · 10.9 KB
/
emo.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package emo
import (
"fmt"
"log"
"os"
"runtime"
"strconv"
"strings"
color "github.com/acmacalister/skittles"
)
//go:generate go run ./codegen
// ObjectInfo prints elements list for debugging purpose.
func ObjectInfo(args ...any) {
msg := "[" + color.Yellow("object info") + "] "
for _, a := range args {
fmt.Println(msg+"Type: %T Value: %#v", a, a)
}
}
// Zone : base emo zone.
type Zone struct {
Name string
Verbose ParamType // true => always print ; false => print only when isError
StackInfo ParamType // StackNo => never print the call stack info, StackAuto => only when Verbose=true and isError
Hook func(Event)
}
var (
maxNameLen = 1
fmtName = "%-1s"
fmtNameNC = "%-3s" // NC = non-color
)
// NewZone : create a zone.
func NewZone(name string) Zone {
if maxNameLen < len(name) {
maxNameLen = len(name)
fmtName = "%-" + strconv.Itoa(maxNameLen) + "s"
fmtNameNC = "%-" + strconv.Itoa(maxNameLen+2) + "s"
}
return Zone{
Name: name,
Verbose: Auto,
StackInfo: Auto,
Hook: nil,
}
}
// DefaultZone allows to use emo without having to create a new zone.
// DefaultZone defines the default values.
var DefaultZone = Zone{
Name: "",
Verbose: Auto,
StackInfo: Auto,
Hook: nil,
}
type ParamType int
const (
Auto ParamType = 0
No ParamType = -1
Yes ParamType = 1
)
// GlobalTimestamp enables the insertion of a date/time timestamp for each event print.
// To disable use:
//
// emo.GlobalTimestamp(false)
func GlobalTimestamp(enable bool) {
timestampPrefixed = enable
}
// GlobalColoring enables/disables the coloring of the event print.
func GlobalColoring(enable bool) {
outputColored = enable
}
// GlobalVerbosity controls the verbose mode for all zones having Verbose=Auto.
func GlobalVerbosity(enable bool) {
if enable {
DefaultZone = DefaultZone.V()
} else {
DefaultZone = DefaultZone.V(false)
}
}
// GlobalStackInfo enables/disables the call stack information for all zones having StackInfo=Auto.
func GlobalStackInfo(enable bool) {
if enable {
DefaultZone = DefaultZone.S()
} else {
DefaultZone = DefaultZone.S(-1)
}
}
// SetHook sets the callback function to be triggered by all zones.
func GlobalHook(hook func(Event)) {
DefaultZone = DefaultZone.SetHook(hook)
}
// SetVerbosity controls the verbose mode.
// When used with DefaultZone, it impacts all zones having Verbose=Auto.
// V forces the event to be always printed, even when zone.Verbose=false.
//
// V can also be used to never print the log. Example:
//
// zone.V(false).Error("my error")
func (zone Zone) V(auto ...bool) Zone {
zone.Verbose = Yes
if len(auto) > 0 {
if auto[0] {
zone.Verbose = Auto
} else {
zone.Verbose = No
}
}
return zone
}
// S controls the printing of the call stack information.
// Without arguments, S forces the retrieval
// of the call stack info (i.e. caller function and file:line).
//
// The call stack depth can be shifted with the optional parameter:
//
// func myLoggerFunction(s string) { zone.S(2).Error(s) }
//
// S can also be used to disable the call stack info using a negative value:
//
// zone.S(-1).Error("msg")
//
// The zero value is to use the global settings:
//
// zone.S(0).Error("msg")
func (zone Zone) S(callDepth ...int) Zone {
// if StackInfo already shifts the call stack depth
if zone.StackInfo > Yes {
if len(callDepth) > 0 {
if callDepth[0] < 0 {
zone.StackInfo = No
} else if callDepth[0] > 0 {
// increment the call stack depth
zone.StackInfo += ParamType(callDepth[0])
}
// callDepth[0]==0 is ignored because it may use a bad call stack depth
}
} else {
zone.StackInfo = Yes
if len(callDepth) > 0 {
if callDepth[0] >= 1 {
callDepth[0]++ // shift the call stack depth one step more
}
zone.StackInfo = ParamType(callDepth[0])
}
}
return zone
}
// SetHook sets the callback function to be triggered when an Event occurs.
func (zone Zone) SetHook(hook func(Event)) Zone {
zone.Hook = hook
return zone
}
// timestampPrefixed = true => prints are prefixed with the current timestamp
var timestampPrefixed bool = false
// outputColored = true => print colors (use color=false for testing)
var outputColored bool = true
// GlobalTracing enables the print of the Trace() events.
func GlobalTracing(enable bool) {
tracePrinted = enable
}
var tracePrinted bool = false
// N changes zone name and can be used, for example,
// to temporary change the current event name:
//
// zone.N("foo").Info("bar")
func (zone Zone) N(name string) Zone {
zone.Name = name
return zone
}
func (zone Zone) NewEvent(emoji string, isError bool, args ...any) Event {
e := Event{
Zone: zone,
Emoji: emoji,
IsError: isError,
Args: args,
From: "",
File: "",
Line: 0,
}
if e.stackInfoEnabled() {
e = e.Stack()
}
return e
}
// Event : base emo event.
type Event struct {
Emoji string
Zone Zone
IsError bool
Args []any
From string
File string
Line int
cache string
}
func (e Event) Print() Event {
if e.toBePrinted() {
m := e.Message()
if timestampPrefixed {
log.Print(m)
} else {
fmt.Println(m)
}
}
return e
}
func (e Event) Message() string {
msg := e.Zone.highlightName() + e.Emoji
if e.IsError {
msg += " " + red("ERROR")
}
return msg + " " + e.Error()
}
// Err converts an Event to a standard Go error.
func (e Event) Err() error {
return &e
}
// Error is the implementation of the error interface.
func (e *Event) Error() string {
if e.cache == "" {
text := make([]string, 0, len(e.Args))
for _, a := range e.Args {
text = append(text, fmt.Sprintf("%v", a))
}
e.cache = strings.Join(text, " ")
if e.From != "" {
e.cache += " from " + bold(e.From) +
" in " + e.File + ":" +
white(strconv.Itoa(e.Line))
}
}
return e.cache
}
func (zone Zone) hookPresent() bool {
return (zone.Hook != nil) || (DefaultZone.Hook != nil)
}
func (e Event) CallHook() Event {
if e.Zone.hookPresent() {
e = e.Stack()
if e.Zone.Hook != nil {
e.Zone.Hook(e)
}
if DefaultZone.Hook != nil {
DefaultZone.Hook(e)
}
}
return e
}
// Stack extracts info from the the call stack info
// (caller function, file and line number).
// Then the event may be printed later.
// The optional parameter allows to change the call stack depth within the call stack.
func (e Event) Stack(optionalPosition ...int) Event {
// do not extract the call stack info if already done
if e.From == "" {
callDepth := 4
if e.Zone.StackInfo > Yes {
callDepth = 3 + int(e.Zone.StackInfo)
}
if len(optionalPosition) > 0 {
callDepth += optionalPosition[0]
}
pc := make([]uintptr, 1)
runtime.Callers(callDepth, pc)
f := runtime.FuncForPC(pc[0])
e.File, e.Line = f.FileLine(pc[0])
e.From = f.Name()
}
return e
}
func (zone Zone) enabled(isError bool) bool {
return zone.toBePrinted(isError) || zone.hookPresent()
}
func (e Event) toBePrinted() bool {
return e.Zone.toBePrinted(e.IsError)
}
func (e Event) stackInfoEnabled() bool {
return e.Zone.stackInfoEnabled(e.IsError)
}
func (zone Zone) toBePrinted(isError bool) bool {
if zone.Verbose >= Yes {
return true
}
if zone.Verbose <= No {
return false
}
if DefaultZone.Verbose >= Yes {
return true
}
if DefaultZone.Verbose <= No {
return isError
}
return true
}
func (zone Zone) stackInfoEnabled(isError bool) bool {
if zone.StackInfo >= Yes {
return true
}
if zone.StackInfo <= No {
return false
}
if DefaultZone.StackInfo >= Yes {
return true
}
if DefaultZone.StackInfo <= No {
return false
}
return isError && zone.toBePrinted(isError)
}
// --- following functions for compatibility with logrus and Go standard logger ---
func (zone Zone) Default() *log.Logger {
return log.Default()
}
func Trace(args ...any) Event {
if tracePrinted {
return DefaultZone.NewEvent("👣", false, args...).Print()
}
var evt Event
return evt
}
func Tracef(format string, v ...any) Event {
if tracePrinted {
s := fmt.Sprintf(format, v...)
return DefaultZone.Trace(s)
}
var evt Event
return evt
}
func (zone Zone) Trace(args ...any) Event {
if tracePrinted {
return zone.NewEvent("👣", false, args...).Print()
}
var evt Event
return evt
}
func (zone Zone) Tracef(format string, v ...any) Event {
if tracePrinted {
s := fmt.Sprintf(format, v...)
return zone.Trace(s)
}
var evt Event
return evt
}
// Warn looks same as Warning, but:
// - Warn is printed even when Verbose=No, and
// - Warn is printed with the call stack info (similar to Error).
func Print(args ...any) Event {
return DefaultZone.V().NewEvent("📰", false, args...).Print().CallHook()
}
func Printf(format string, v ...any) Event {
s := fmt.Sprintf(format, v...)
return DefaultZone.V().NewEvent("📰", false, s).Print().CallHook()
}
func (zone Zone) Print(args ...any) Event {
return zone.V().NewEvent("📰", false, args...).Print().CallHook()
}
func (zone Zone) Printf(format string, v ...any) Event {
s := fmt.Sprintf(format, v...)
return zone.V().NewEvent("📰", false, s).Print().CallHook()
}
// Warn looks same as Warning, but:
// - Warn is printed even when Verbose=No, and
// - Warn is printed with the call stack info (similar to Error).
func Warn(args ...any) Event {
return DefaultZone.V().S(1).Warning(args...)
}
func Warnf(format string, v ...any) Event {
return DefaultZone.V().S(1).Warningf(format, v...)
}
// Warn looks same as Warning, but:
// - Warn is printed even when Verbose=No, and
// - Warn is printed with the call stack info (similar to Error).
func (zone Zone) Warn(args ...any) Event {
return zone.V().S(1).Warning(args...)
}
func (zone Zone) Warnf(format string, v ...any) Event {
return zone.V().S(1).Warningf(format, v...)
}
func Fatal(args ...any) {
DefaultZone.S(1).Fatal(args...)
}
func Fatalf(format string, v ...any) {
DefaultZone.S(1).Fatalf(format, v...)
}
func (zone Zone) Fatal(args ...any) {
msg := zone.S().NewEvent("🤯", true, args...).Message()
if timestampPrefixed {
log.Fatal(msg)
} else {
fmt.Println(msg)
os.Exit(1)
}
}
func (zone Zone) Fatalf(format string, v ...any) {
s := fmt.Sprintf(format, v...)
zone.S(1).Fatal(s)
}
func Panic(args ...any) {
DefaultZone.S(1).Panic(args...)
}
func Panicf(format string, v ...any) {
DefaultZone.S(1).Panicf(format, v...)
}
func (zone Zone) Panic(args ...any) {
msg := zone.S().NewEvent("😵", true, args...).Message()
if timestampPrefixed {
log.Panic(msg)
} else {
panic(msg)
}
}
func (zone Zone) Panicf(format string, v ...any) {
s := fmt.Sprintf(format, v...)
zone.S(1).Panic(s)
}
// --- following functions colorize output depending on configuration ---
func red(s string) string {
if outputColored {
return color.Red(s)
}
return s
}
func (zone Zone) highlightName() string {
if zone.Name == "" {
return ""
}
if outputColored {
return color.Yellow(fmt.Sprintf(fmtName, zone.Name)) + " "
}
return fmt.Sprintf(fmtNameNC, "["+zone.Name+"] ")
}
func bold(s string) string {
if outputColored {
return color.BoldWhite(s)
}
return s
}
func white(s string) string {
if outputColored {
return color.White(s)
}
return s
}