Improve stack trace formatting

This commit is contained in:
Kovid Goyal
2023-07-25 18:50:21 +05:30
parent a30687125c
commit a55a918d4f

View File

@@ -6,7 +6,7 @@ import (
"encoding/base64"
"fmt"
"os"
"runtime/debug"
"runtime"
"strings"
"time"
@@ -251,11 +251,20 @@ func (self *Loop) DebugPrintln(args ...any) {
func (self *Loop) Run() (err error) {
defer func() {
if r := recover(); r != nil {
stack := utils.Splitlines(string(debug.Stack()))
pcs := make([]uintptr, 256)
n := runtime.Callers(2, pcs)
frames := runtime.CallersFrames(pcs[:n])
err = fmt.Errorf("Paniced: %s", r)
fmt.Fprintf(os.Stderr, "\r\nPaniced with error: %s\r\nStacktrace:\r\n", r)
for _, line := range stack {
fmt.Fprintf(os.Stderr, "%s\r\n", line)
fmt.Fprintf(os.Stderr, "\r\nPaniced with error: %s\r\nStacktrace (most recent call first):\r\n", r)
found_first_frame := false
for frame, more := frames.Next(); more; frame, more = frames.Next() {
if !found_first_frame {
if strings.HasPrefix(frame.Function, "runtime.") {
continue
}
found_first_frame = true
}
fmt.Fprintf(os.Stderr, "%s\r\n\t%s:%d\r\n", frame.Function, frame.File, frame.Line)
}
if self.terminal_options.alternate_screen {
term, err := tty.OpenControllingTerm(tty.SetRaw)