initial commit
This commit is contained in:
46
calltrace.go
Normal file
46
calltrace.go
Normal file
@ -0,0 +1,46 @@
|
||||
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()
|
||||
}
|
Reference in New Issue
Block a user