-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.go
More file actions
51 lines (36 loc) · 941 Bytes
/
trace.go
File metadata and controls
51 lines (36 loc) · 941 Bytes
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
package debugkit
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"github.com/fatih/color"
)
func Trace(v any) {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "???"
line = 0
}
file = filepath.Base(file)
colorHeader := color.New(color.FgYellow, color.Bold).SprintFunc()
fmt.Printf("\n%s\n", colorHeader(fmt.Sprintf("[%s:%d]", file, line)))
val := reflect.ValueOf(v)
visited := make(map[uintptr]bool)
printValue(val, 0, visited)
fmt.Println()
}
func TraceAll(values ...any) {
_, file, line, _ := runtime.Caller(1)
file = filepath.Base(file)
colorHeader := color.New(color.FgYellow, color.Bold).SprintFunc()
fmt.Printf("\n%s\n", colorHeader(fmt.Sprintf("[%s:%d]", file, line)))
for i, v := range values {
fmt.Printf("%s = ", colorField(fmt.Sprintf("arg%d", i)))
val := reflect.ValueOf(v)
visited := make(map[uintptr]bool)
printValue(val, 0, visited)
fmt.Println()
}
fmt.Println()
}