Skip to content

Commit

Permalink
Include stack trace in panic logs
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Aug 12, 2024
1 parent 27e9c0d commit a67c887
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 13 deletions.
7 changes: 4 additions & 3 deletions m/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package m
import (
"fmt"
"regexp"
"runtime/debug"
"strings"
"time"

Expand Down Expand Up @@ -323,7 +324,7 @@ func (p *Pager) StartPaging(screen twin.Screen, chromaStyle *chroma.Style, chrom

go func() {
defer func() {
panicHandler("StartPaging()/moreLinesAvailable", recover())
panicHandler("StartPaging()/moreLinesAvailable", recover(), debug.Stack())
}()

for range p.reader.moreLinesAdded {
Expand All @@ -342,7 +343,7 @@ func (p *Pager) StartPaging(screen twin.Screen, chromaStyle *chroma.Style, chrom

go func() {
defer func() {
panicHandler("StartPaging()/spinner", recover())
panicHandler("StartPaging()/spinner", recover(), debug.Stack())
}()

// Spin the spinner as long as contents is still loading
Expand All @@ -368,7 +369,7 @@ func (p *Pager) StartPaging(screen twin.Screen, chromaStyle *chroma.Style, chrom

go func() {
defer func() {
panicHandler("StartPaging()/maybeDone", recover())
panicHandler("StartPaging()/maybeDone", recover(), debug.Stack())
}()

for range p.reader.maybeDone {
Expand Down
5 changes: 3 additions & 2 deletions m/panicHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
log "github.com/sirupsen/logrus"
)

func panicHandler(goroutineName string, recoverResult any) {
func panicHandler(goroutineName string, recoverResult any, stackTrace []byte) {
if recoverResult == nil {
return
}

log.WithFields(log.Fields{
"recoverResult": recoverResult,
"panic": recoverResult,
"stackTrace": string(stackTrace),
}).Error("Goroutine panicked: " + goroutineName)
}
3 changes: 2 additions & 1 deletion m/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"path"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -366,7 +367,7 @@ func newReaderFromStream(reader io.Reader, originalFileName *string, formatter c

go func() {
defer func() {
panicHandler("newReaderFromStream()/readStream()", recover())
panicHandler("newReaderFromStream()/readStream()", recover(), debug.Stack())
}()

returnMe.readStream(reader, formatter, lexer)
Expand Down
3 changes: 2 additions & 1 deletion m/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package m
import (
"fmt"
"runtime"
"runtime/debug"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -125,7 +126,7 @@ func (p *Pager) findFirstHit(startPosition linenumbers.LineNumber, beforePositio

go func(i int, searchStart linenumbers.LineNumber, chunkBefore *linenumbers.LineNumber) {
defer func() {
panicHandler("findFirstHit()/chunkSearch", recover())
panicHandler("findFirstHit()/chunkSearch", recover(), debug.Stack())
}()

findings[i] <- p._findFirstHit(searchStart, chunkBefore, backwards)
Expand Down
5 changes: 3 additions & 2 deletions twin/panicHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
log "github.com/sirupsen/logrus"
)

func panicHandler(goroutineName string, recoverResult any) {
func panicHandler(goroutineName string, recoverResult any, stackTrace []byte) {
if recoverResult == nil {
return
}

log.WithFields(log.Fields{
"recoverResult": recoverResult,
"panic": recoverResult,
"stackTrace": string(stackTrace),
}).Error("Goroutine panicked: " + goroutineName)
}
3 changes: 2 additions & 1 deletion twin/screen-setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"os/signal"
"runtime/debug"
"sync/atomic"
"syscall"

Expand Down Expand Up @@ -118,7 +119,7 @@ func (screen *UnixScreen) setupSigwinchNotification() {
signal.Notify(sigwinch, syscall.SIGWINCH)
go func() {
defer func() {
panicHandler("setupSigwinchNotification()/SIGWINCH", recover())
panicHandler("setupSigwinchNotification()/SIGWINCH", recover(), debug.Stack())
}()

for {
Expand Down
3 changes: 2 additions & 1 deletion twin/screen-setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package twin
import (
"io"
"os"
"runtime/debug"
"testing"
"time"

Expand All @@ -32,7 +33,7 @@ func TestInterruptableReader_blockedOnReadImmediate(t *testing.T) {
readResultChan := make(chan readResult)
go func() {
defer func() {
panicHandler("TestInterruptableReader_blockedOnReadImmediate()", recover())
panicHandler("TestInterruptableReader_blockedOnReadImmediate()", recover(), debug.Stack())
}()

buffer := make([]byte, 1)
Expand Down
3 changes: 2 additions & 1 deletion twin/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"regexp"
"runtime/debug"
"strconv"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -181,7 +182,7 @@ func NewScreenWithMouseModeAndColorCount(mouseMode MouseMode, terminalColorCount

go func() {
defer func() {
panicHandler("NewScreenWithMouseModeAndColorCount()/mainLoop()", recover())
panicHandler("NewScreenWithMouseModeAndColorCount()/mainLoop()", recover(), debug.Stack())
}()

screen.mainLoop()
Expand Down
3 changes: 2 additions & 1 deletion twin/screen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package twin
import (
"io"
"os"
"runtime/debug"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -270,7 +271,7 @@ func TestInterruptableReader_blockedOnRead(t *testing.T) {
readResultChan := make(chan readResult)
go func() {
defer func() {
panicHandler("TestInterruptableReader_blockedOnRead()", recover())
panicHandler("TestInterruptableReader_blockedOnRead()", recover(), debug.Stack())
}()

buffer := make([]byte, 1)
Expand Down

0 comments on commit a67c887

Please sign in to comment.