/
panic_trace.go
78 lines (67 loc) · 1.66 KB
/
panic_trace.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package utils
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/debug"
"time"
)
var (
//定义panic调试输出的方式
panicToStd = flag.Bool("panic_trace", false, "panic info to standard error instead of file")
)
// PanicTrace trace panic stack info.
func PanicTrace() string {
s := []byte("/src/runtime/panic.go")
e := []byte("\ngoroutine ")
line := []byte("\n")
stack := make([]byte, 2048)
length := runtime.Stack(stack, true)
start := bytes.Index(stack, s)
stack = stack[start:length]
start = bytes.Index(stack, line) + 1
stack = stack[start:]
end := bytes.LastIndex(stack, line)
if end != -1 {
stack = stack[:end]
}
end = bytes.Index(stack, e)
if end != -1 {
stack = stack[:end]
}
stack = bytes.TrimRight(stack, "\n")
return string(stack)
}
func PanicTraceEx() {
if err := recover(); err != nil {
log.Println(PanicTrace())
}
}
func PanicDump(err interface{}) {
exeName := os.Args[0] //获取程序名称
now := time.Now() //获取当前时间
pid := os.Getpid() //获取进程ID
time_str := now.Format("2006-01-02.15-04-05") //设定时间格式
fname := fmt.Sprintf("%s-%d-%s-dump.log", exeName, pid, time_str) //保存错误信息文件名:程序名-进程ID-当前时间(年月日时分秒)
fmt.Println("dump to file ", fname)
f, err := os.Create(fname)
if err != nil {
return
}
defer f.Close()
f.WriteString(fmt.Sprintf("%v\r\n", err)) //输出panic信息
f.WriteString("========\r\n")
f.WriteString(string(debug.Stack())) //输出堆栈信息
}
func PanicHandler() {
if err := recover(); err != nil {
if *panicToStd {
log.Println(PanicTrace())
} else {
PanicDump(err)
}
}
}