-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Usage | ||
|
||
It's a powerful panic handler to simplify reasoning of panics | ||
|
||
## Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/donutloop/toolkit/xpanic" | ||
"log" | ||
) | ||
|
||
func main() { | ||
logF := func(format string, args ...interface{}) { log.Println(fmt.Sprintf(format, args...)) } | ||
panicHandler := BuildPanicHandler(logF, xpanic.CrashOnErrorDeactivated) | ||
defer panicHandler | ||
panic("hello world") | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package xpanic | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os/signal" | ||
"runtime/pprof" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
// If crashOnError is set a coredump will be produced and kill program else it continues. | ||
const ( | ||
CrashOnErrorActivated = true | ||
CrashOnErrorDeactivated = false | ||
) | ||
|
||
// BuildPanicHandler builds a panic handler and verifies a none nil logger got passed | ||
func BuildPanicHandler(errorf func(format string, args ...interface{}), crashOnError bool) func() { | ||
if errorf == nil { | ||
panic("errorf is not set") | ||
} | ||
// handlePanic writes a message to the logger and causes a backtrace to be produced. | ||
return func() { | ||
r := recover() | ||
if r != nil { | ||
errorf("capture panic infos") | ||
|
||
errorf(fmt.Sprintf("panic: %s", r)) | ||
Backtrace(errorf) | ||
|
||
if crashOnError { | ||
signal.Reset(syscall.SIGABRT) | ||
errorf("finished capturing of panic infos") | ||
syscall.Kill(0, syscall.SIGABRT) | ||
} else { | ||
errorf("finished capturing of panic infos") | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Backtrace writes a multi-line backtrace to the logger. | ||
func Backtrace(errorf func(format string, args ...interface{})) { | ||
profiles := pprof.Profiles() | ||
buf := new(bytes.Buffer) | ||
|
||
for _, p := range profiles { | ||
// https://golang.org/pkg/runtime/pprof/#Profile.WriteTo. | ||
err := pprof.Lookup(p.Name()).WriteTo(buf, 2) | ||
if err != nil { | ||
errorf("could not write profile: %v", err) | ||
} | ||
} | ||
|
||
for _, line := range strings.Split(buf.String(), "\n") { | ||
errorf(line) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package xpanic_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/donutloop/toolkit/xpanic" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestHandlePanic(t *testing.T) { | ||
|
||
var buff bytes.Buffer | ||
panicHandler := xpanic.BuildPanicHandler(func(format string, args ...interface{}) { buff.WriteString(fmt.Sprintf(format, args...)) }, xpanic.CrashOnErrorDeactivated) | ||
|
||
var wait sync.WaitGroup | ||
wait.Add(1) | ||
go func() { | ||
defer panicHandler() | ||
defer wait.Done() | ||
panic("hello world") | ||
}() | ||
|
||
wait.Wait() | ||
|
||
if buff.Len() == 0 { | ||
t.Fatal("buff is empty") | ||
} | ||
} |