zlog/pkgerrors/stacktrace.go

67 lines
1.4 KiB
Go
Raw Normal View History

package pkgerrors
import (
"github.com/pkg/errors"
)
var (
StackSourceFileName = "source"
StackSourceLineName = "line"
StackSourceFunctionName = "func"
)
2019-01-02 21:19:14 +00:00
type state struct {
b []byte
}
// Write implement fmt.Formatter interface.
func (s *state) Write(b []byte) (n int, err error) {
s.b = b
return len(b), nil
}
// Width implement fmt.Formatter interface.
func (s *state) Width() (wid int, ok bool) {
return 0, false
}
// Precision implement fmt.Formatter interface.
func (s *state) Precision() (prec int, ok bool) {
return 0, false
}
// Flag implement fmt.Formatter interface.
func (s *state) Flag(c int) bool {
return false
}
func frameField(f errors.Frame, s *state, c rune) string {
f.Format(s, c)
return string(s.b)
}
// MarshalStack implements pkg/errors stack trace marshaling.
//
// zerolog.ErrorStackMarshaler = MarshalStack
2019-01-02 19:12:56 +00:00
func MarshalStack(err error) interface{} {
type stackTracer interface {
StackTrace() errors.StackTrace
}
var st errors.StackTrace
if err, ok := err.(stackTracer); ok {
st = err.StackTrace()
} else {
return nil
}
2019-01-02 21:19:14 +00:00
s := &state{}
2019-01-02 19:12:56 +00:00
out := make([]map[string]string, 0, len(st))
for _, frame := range st {
out = append(out, map[string]string{
2019-01-02 21:19:14 +00:00
StackSourceFileName: frameField(frame, s, 's'),
StackSourceLineName: frameField(frame, s, 'd'),
StackSourceFunctionName: frameField(frame, s, 'n'),
2019-01-02 19:12:56 +00:00
})
}
2019-01-02 19:12:56 +00:00
return out
}