-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacktrace.go
More file actions
38 lines (33 loc) · 962 Bytes
/
stacktrace.go
File metadata and controls
38 lines (33 loc) · 962 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
package yikes
import (
"log/slog"
"runtime"
"strconv"
)
func getStacktrace() slog.Attr {
buf := make([]uintptr, 1024) //nolint:mnd // a kilobyte ought to be enough for anyone
// 0 is runtime.Callers, 1 is getStacktrace, 2 is either report or
// topLevelReport, 3 is the exported function of yikes that was called,
// 4 is the caller.
offset := 4
var numFrames int
for {
numFrames = runtime.Callers(offset, buf)
if numFrames < len(buf) {
break
}
buf = make([]uintptr, len(buf)*2) //nolint:mnd // if it's not, just double it
}
frames := runtime.CallersFrames(buf[:numFrames])
var result []slog.Attr
var index int
for frame, more := frames.Next(); more; frame, more = frames.Next() {
result = append(result, slog.Group(strconv.Itoa(index),
slog.String("function", frame.Function),
slog.String("file", frame.File),
slog.Int("line", frame.Line),
))
index++
}
return slog.Any("stacktrace", slog.GroupValue(result...))
}