-
Notifications
You must be signed in to change notification settings - Fork 3
/
panic.go
36 lines (29 loc) · 862 Bytes
/
panic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package panic
import (
"os"
"runtime"
"strings"
"github.com/cloudfoundry/cli/cf/panic_printer"
"github.com/cloudfoundry/cli/cf/terminal"
)
func HandlePanics() {
panic_printer.UI = terminal.NewUI(os.Stdin, terminal.NewTeePrinter())
commandArgs := strings.Join(os.Args, " ")
stackTrace := generateBacktrace()
err := recover()
panic_printer.DisplayCrashDialog(err, commandArgs, stackTrace)
if err != nil {
os.Exit(1)
}
}
func generateBacktrace() string {
stackByteCount := 0
STACK_SIZE_LIMIT := 4 * 1024 * 1024
var bytes []byte
for stackSize := 1024; (stackByteCount == 0 || stackByteCount == stackSize) && stackSize < STACK_SIZE_LIMIT; stackSize = 2 * stackSize {
bytes = make([]byte, stackSize)
stackByteCount = runtime.Stack(bytes, true)
}
stackTrace := "\t" + strings.Replace(string(bytes), "\n", "\n\t", -1)
return stackTrace
}