-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbg.go
98 lines (87 loc) · 2.28 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
93
94
95
96
97
98
package dbg
import (
"bufio"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
// CallerInfo retrieves and formats the file name and line number of the caller's
// location in the source code.
//
// It uses the runtime.Caller function to determine the caller's file and line
// information.
//
// Example usage:
//
// callerInfo := CallerInfo()
// fmt.Println("Caller:", callerInfo)
//
// The output will look like this:
//
// Caller: [filename:line]
//
// Where "filename" is the name of the file where the caller is located, and
// "line" is the line number in that file.
func CallerInfo() string {
pc, file, line, ok := runtime.Caller(1)
if !ok {
fmt.Fprintln(os.Stderr, "dbg.CallerInfo: Unable to parse runtime caller")
return ""
}
out := fmt.Sprintf("[%s:%d] [%s]", filepath.Base(file), line, runtime.FuncForPC(pc).Name())
return out
}
// Dbg is a debugging utility function that prints the given expression and its
// location in the source code to standard output. It also prints the file name
// and line number where the Dbg function is called.
//
// Parameters:
//
// exp - The expression or value to be printed.
//
// Example:
//
// Dbg(someValue)
//
// The output will look like this:
//
// [filename:line] variable = value
//
// Where "filename" is the name of the file where Dbg is called, "line" is the
// line number in that file, "variable" is the variable or expression being
// debugged, and "value" is the value of the expression.
func Dbg(exp any) {
pc, file, line, ok := runtime.Caller(1)
if !ok {
fmt.Fprintln(os.Stderr, "dbg.Dbg: Unable to parse runtime caller")
return
}
f, err := os.Open(file)
if err != nil {
fmt.Fprintln(os.Stderr, "dbg.Dbg: Unable to open expected file")
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
var out string
for i := 1; scanner.Scan(); i++ {
if i == line {
v := scanner.Text()[strings.Index(scanner.Text(), "(")+1 : strings.LastIndex(scanner.Text(), ")")]
out = fmt.Sprintf("[%s:%d] [%s] %s = %+v", filepath.Base(file), line, runtime.FuncForPC(pc).Name(), v, exp)
break
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return
}
switch exp.(type) {
case error:
fmt.Fprintln(os.Stderr, out)
default:
fmt.Fprintln(os.Stdout, out)
}
}