47 lines
956 B
Go
47 lines
956 B
Go
|
package adverr
|
||
|
|
||
|
import (
|
||
|
"runtime"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// CallTrace represents a call stack trace similar to Java's stack trace
|
||
|
type CallTrace struct {
|
||
|
frames *runtime.Frames
|
||
|
}
|
||
|
|
||
|
// Trace returns a new CallTrace starting from this call
|
||
|
// Use skip to skip the first entries in the trace
|
||
|
func Trace(skip int) *CallTrace {
|
||
|
if !TraceCallStack {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
pc := make([]uintptr, CallStackLength)
|
||
|
n := runtime.Callers(skip+1, pc)
|
||
|
pc = pc[:n]
|
||
|
return &CallTrace{runtime.CallersFrames(pc)}
|
||
|
}
|
||
|
|
||
|
func (ct *CallTrace) String() string {
|
||
|
if ct == nil {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
b := new(strings.Builder)
|
||
|
|
||
|
for frame, ok := ct.frames.Next(); ok; frame, ok = ct.frames.Next() {
|
||
|
b.WriteString("\tat ")
|
||
|
b.WriteString(frame.Function)
|
||
|
b.WriteString(" (")
|
||
|
b.WriteString(frame.File)
|
||
|
b.WriteString(":")
|
||
|
b.WriteString(strconv.Itoa(frame.Line))
|
||
|
b.WriteString(")")
|
||
|
b.WriteString("\n")
|
||
|
}
|
||
|
|
||
|
return b.String()
|
||
|
}
|