-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
63 lines (52 loc) · 1.09 KB
/
debug.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
package gotk
import (
"bytes"
"fmt"
"path/filepath"
"regexp"
"runtime/debug"
"strings"
)
var (
_StackRE *regexp.Regexp = regexp.MustCompile("\n.*\n\t.*")
)
func _fn2() {
defer func() {
var intf any
if intf = recover(); intf == nil {
return
}
fmt.Println("!!!", intf)
fmt.Println(">>>", Stack(""))
}()
_fn1()
}
func _fn1() {
var mySlice []int
j := mySlice[0]
fmt.Printf("Hello, playground %d", j)
}
func Stack(prefix string) (slice []string) {
bts := bytes.TrimSpace(debug.Stack())
// fmt.Printf(">>>\n%s\n<<<\n", bts)
out := _StackRE.FindAllStringSubmatch(string(bts), -1)
const skip = 2
if len(out) < skip {
return make([]string, 0)
}
slice = make([]string, 0, len(out)-skip)
for i := skip; i < len(out); i++ {
t := strings.Split(strings.TrimSpace(out[i][0]), "\n\t")
if len(t) <= 1 {
continue
}
if prefix != "" && !strings.HasPrefix(t[0], prefix) {
continue
}
fp := strings.Fields(t[1])[0]
fn := filepath.Base(strings.Split(t[0], "(")[0])
// gotk/debug_test.go:8(gotk.TestPanic)
slice = append(slice, fmt.Sprintf("%s(%s)", fp, fn))
}
return
}