mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [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
32 lines
684 B
Go
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()
|
|
}()
|
|
}
|
|
}
|