-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanic.go
60 lines (47 loc) · 1011 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package program
import (
"bytes"
"fmt"
"os"
"runtime"
)
func Abort(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
}
func Panic(format string, args ...interface{}) {
panic(fmt.Sprintf(format, args...))
}
func RecoverValueString(value interface{}) (msg string) {
switch v := value.(type) {
case error:
msg = v.Error()
case string:
msg = v
default:
msg = fmt.Sprintf("%#v", v)
}
return
}
func StackTrace(skip, depth int, includeLocation bool) string {
pc := make([]uintptr, depth)
// Always skip runtime.Callers and utils.StackTrace
nbFrames := runtime.Callers(skip+2, pc)
pc = pc[:nbFrames]
var buf bytes.Buffer
frames := runtime.CallersFrames(pc)
for {
frame, more := frames.Next()
filePath := frame.File
line := frame.Line
function := frame.Function
fmt.Fprintf(&buf, "%s\n", function)
if includeLocation {
fmt.Fprintf(&buf, " %s:%d\n", filePath, line)
}
if !more {
break
}
}
return buf.String()
}