Skip to content

Commit

Permalink
display a neat stack trace on panic
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Apr 2, 2022
1 parent 4f13930 commit ea52e93
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"os/signal"
"runtime"
"runtime/debug"
"strings"
"syscall"
"text/tabwriter"
"time"
Expand Down Expand Up @@ -415,8 +415,37 @@ Can you please open an issue on github including these details:
_, _ = fmt.Fprintf(w, "\t%s:\t%s\n", "compiled", date)
_, _ = fmt.Fprintf(w, "\t%s:\t%s\n", "by", builtBy)
_, _ = fmt.Fprintf(w, "\t%s:\t%s\n", "error", r)
_, _ = fmt.Fprintf(w, "\t%s:\t%s\n", "stack", string(debug.Stack()))
_, _ = fmt.Fprintf(w, "\t%s:\n%s\n", "stack", getStack())
w.Flush()
fmt.Fprint(os.Stderr, "===============================================================\n")
}
}

func getStack() string {
stack := ""
pc := make([]uintptr, 10)
n := runtime.Callers(4, pc)
if n == 0 {
return ""
}

pc = pc[:n] // pass only valid pcs to runtime.CallersFrames
frames := runtime.CallersFrames(pc)

// Loop to get frames.
// A fixed number of PCs can expand to an indefinite number of Frames.
for {
frame, more := frames.Next()

// stop when we get to the runtime bootstrap
if strings.Contains(frame.File, "runtime/") {
break
}
stack += fmt.Sprintf("%s\n\t%s:%d\n", frame.Function, frame.File, frame.Line)

if !more {
break
}
}
return stack
}

0 comments on commit ea52e93

Please sign in to comment.