Repository navigation
Expand file tree
/
Copy pathdebug.go
More file actions
84 lines (69 loc) · 2.04 KB
/
Copy pathdebug.go
File metadata and controls
84 lines (69 loc) · 2.04 KB
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
package neffos
import (
"log"
"os"
)
// Printer is what `EnableDebug` writes neffos debug messages to.
// `*log.Logger` and kataras/golog satisfy it; wrap anything else in a `PrinterFunc`.
type Printer interface {
Printf(format string, args ...any)
}
// PrinterFunc adapts a function to the `Printer` interface:
//
// neffos.EnableDebug(neffos.PrinterFunc(func(format string, args ...any) {
// slog.Debug(fmt.Sprintf(format, args...))
// }))
type PrinterFunc func(format string, args ...any)
// Printf calls f.
func (f PrinterFunc) Printf(format string, args ...any) { f(format, args...) }
var debugPrinter Printer
// EnableDebug enables debug mode and sets the printer that receives the
// debug messages. A nil "printer" prints through a new `log.Logger` on stderr
// prefixed with "| neffos |".
//
// Note that neffos, currently, uses debug mode only on the build state of the events.
// Therefore enabling the debugger has zero performance cost on up-and-running servers and clients.
//
// There is no way to disable the debug mode on serve-time.
func EnableDebug(printer Printer) {
if debugEnabled() {
Debugf("debug mode is already set")
return
}
if printer == nil {
logger := log.New(os.Stderr, "| neffos | ", 0)
logger.Println("debug mode is set")
printer = logger
}
debugPrinter = printer
}
func debugEnabled() bool {
return debugPrinter != nil
}
// Debugf prints debug messages to the printer defined on `EnableDebug`.
// Runs only on debug mode.
func Debugf(format string, args ...any) {
if !debugEnabled() {
return
}
if len(args) == 1 {
// handles:
// Debugf("format", func() dargs {
// time-consumed action that should run only on debug.
// })
if onDebugWithArgs, ok := args[0].(func() dargs); ok {
args = onDebugWithArgs()
}
}
debugPrinter.Printf(format, args...)
}
type dargs []any
// debugEach calls visitor for each entry of m. Runs only on debug mode.
func debugEach[K comparable, V any](m map[K]V, visitor func(K, V)) {
if !debugEnabled() || visitor == nil {
return
}
for k, v := range m {
visitor(k, v)
}
}