-
Notifications
You must be signed in to change notification settings - Fork 3
/
stacktrace.go
42 lines (33 loc) · 893 Bytes
/
stacktrace.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
package log
import (
"bytes"
"runtime"
"github.com/go-errors/errors"
)
const (
defaultSkipStackTrace = 5
)
// logStackTracer implements the zerolog.ErrorStackMarshaler func signature.
func logStackTracer(err error) interface{} {
return stackTracer(err)
}
func stackTracer(err error) string {
// is it the "github.com/go-errors/errors" error type? If so, it has a stack trace we can use
var e *errors.Error
if errors.As(err, &e) {
return string(e.Stack())
}
// Otherwise we just get the current stack trace (minus the ca-go calls)
return currentStack(defaultSkipStackTrace)
}
func currentStack(skip int) string {
stack := make([]uintptr, errors.MaxStackDepth)
length := runtime.Callers(skip, stack)
stack = stack[:length]
buf := bytes.Buffer{}
for _, pc := range stack {
frame := errors.NewStackFrame(pc)
buf.WriteString(frame.String())
}
return buf.String()
}