kaspad/util/panics/panics.go
Ori Newman b7b41f1a94 [NOD-159] Wrap all goroutines to handle panics (#290)
* [NOD-159] Wrap all goroutines to handle panics

* [NOD-159] Fix gofmt errors

* [NOD-159] Add comment to HandlePanic

* [NOD-159] Merge panics and gowrapper packages

* [NOD-159] Added missing initialization
2019-05-07 16:13:06 +03:00

32 lines
684 B
Go

package panics
import (
"os"
"runtime/debug"
"github.com/btcsuite/btclog"
"github.com/daglabs/btcd/logger"
)
// HandlePanic recovers panics, log them, and then exits the process.
func HandlePanic(log btclog.Logger) {
if err := recover(); err != nil {
log.Criticalf("Fatal error: %s", err)
log.Criticalf("Stack trace: %s", debug.Stack())
if logger.LogRotator != nil {
logger.LogRotator.Close()
}
os.Exit(1)
}
}
// GoroutineWrapperFunc returns a goroutine wrapper function that handles panics and write them to the log.
func GoroutineWrapperFunc(log btclog.Logger) func(func()) {
return func(f func()) {
go func() {
defer HandlePanic(log)
f()
}()
}
}