-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.go
50 lines (44 loc) · 872 Bytes
/
stack.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
package stackdriver
import (
"fmt"
"runtime"
"strings"
)
type Stack []uintptr
func (s *Stack) String() string {
sb := strings.Builder{}
frames := runtime.CallersFrames(*s)
for {
f, more := frames.Next()
if strings.HasSuffix(f.File, "stackdriver/stacktrace.go") {
continue
}
sb.WriteString(
fmt.Sprintf("\nat %s:%d\n --> %s", f.File, f.Line, f.Function),
)
if !more {
return sb.String()
}
}
}
func (s *Stack) Slice() []string {
var out []string
frames := runtime.CallersFrames(*s)
for {
f, more := frames.Next()
if strings.HasPrefix(f.Function, "log/slog.") {
continue
}
out = append(out, fmt.Sprintf("%s:%d (%s)", f.File, f.Line, f.Function))
if !more {
return out
}
}
}
func CaptureStack() *Stack {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
var s Stack = pcs[0:n]
return &s
}