-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanic.go
103 lines (86 loc) · 2.31 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package mygo
import (
"bytes"
"errors"
"fmt"
"os"
"runtime"
)
var (
dunno = []byte("???") // 未知错误
centerDot = []byte("·")
dot = []byte(".")
slash = []byte("/")
)
// Recover 对 recover() 的封装,增加stack的信息记录、输出
func (*GoPanic) Recover(err *error) {
if e := recover(); e != nil {
stack := stack()
msg := fmt.Sprintf("Panic Recover: %v\n\t%s\n", e, stack)
logger.Errorf(msg)
if err != nil {
*err = errors.New(msg)
}
}
}
func stack() []byte {
buf := &bytes.Buffer{} // 存储结果
var (
sourceCode [][]byte // 存储出现错误的代码文件
lastFile string // 文件指针,用以作为标识
)
for i := 2; ; i++ {
// file: 发送panic的文件,line:对应的代码行数,pc:16进制
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
// 输出堆栈
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
// 如果file == lastFile,说明已经读取到末尾,不再有更深层的堆栈信息
if file != lastFile {
data, err := os.ReadFile(file) // datas:读取file后的文件信息
if err != nil {
continue
}
// 读取到的文件信息
sourceCode = bytes.Split(data, []byte{'\n'})
lastFile = file
}
// 代码行数修正。因为在array中以0开头,而在现实中以1开头
line--
fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(sourceCode, line))
}
return buf.Bytes()
}
// source 返回出错文件对应的函数
func source(lines [][]byte, n int) []byte {
// 未知错误
if n < 0 || n >= len(lines) {
return dunno
}
return bytes.Trim(lines[n], " \t")
}
func function(pc uintptr) []byte {
fn := runtime.FuncForPC(pc)
if fn == nil {
return dunno
}
name := []byte(fn.Name())
// The name includes the path name to the package, which is unnecessary
// since the file name is already included. Plus, it has center dots.
// That is, we see
// runtime/debug.*T·ptrmethod
// and want
// *T.ptrmethod
// Since the package path might contains dots (e.g. code.google.com/...),
// we first remove the path prefix if there is one.
if lastSlash := bytes.LastIndex(name, slash); lastSlash >= 0 {
name = name[lastSlash+1:]
}
if period := bytes.Index(name, dot); period >= 0 {
name = name[period+1:]
}
name = bytes.Replace(name, centerDot, dot, -1)
return name
}