-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbg.go
92 lines (82 loc) · 2.05 KB
/
dbg.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
// package dbg provides a more helpful [fmt.Println] for debugging in Go.
//
// dbg.Dbg wraps [spew.Dump] to additionally print the name of each argument and the
// filename and line number of the caller.
package dbg
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/davecgh/go-spew/spew"
)
// Dbg pretty-prints its arguments with their names, types and values to os.Stdout.
func Dbg(args ...any) {
impl(os.Stdout, args...)
}
// To pretty-prints its arguments with their names, types and values to the given Writer.
func To(w io.Writer, args ...any) {
impl(w, args...)
}
func impl(w io.Writer, args ...any) {
_, file, line, _ := runtime.Caller(2)
argNames := extractFunctionCallArgs(file, line)
fileName := filepath.Base(file)
fmt.Fprintf(w, "%s:%d:", fileName, line)
if len(args) == 0 {
fmt.Fprintln(w)
return
}
if len(argNames) != len(args) {
spew.Fdump(w, args...)
return
}
for i, arg := range args {
dumped := strings.TrimSpace(spew.Sdump(arg))
fmt.Fprintf(w, " %s = %s", argNames[i], strings.ReplaceAll(dumped, "\n", " \n"))
if strings.Contains(dumped, "\n") || i == len(args)-1 {
fmt.Fprint(w, "\n")
} else {
fmt.Fprint(w, ",")
}
}
}
func extractFunctionCallArgs(filename string, lineNum int) (args []string) {
sourceBytes, err := os.ReadFile(filename)
if err != nil {
return nil
}
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, filename, nil, parser.AllErrors)
if err != nil {
return nil
}
for n := range ast.Preorder(file) {
if fset.Position(n.Pos()).Line != lineNum {
continue
}
callExpr, ok := n.(*ast.CallExpr)
if !ok {
continue
}
if fn, ok := callExpr.Fun.(*ast.SelectorExpr); !ok {
continue
} else if fn.Sel.Name != "Dbg" && fn.Sel.Name != "To" {
continue
}
for _, arg := range callExpr.Args {
start := fset.Position(arg.Pos()).Offset
end := fset.Position(arg.End()).Offset
if start >= 0 && end <= len(sourceBytes) {
args = append(args, string(sourceBytes[start:end]))
}
}
break
}
return args
}