-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace_cleaner.go
58 lines (51 loc) · 1.12 KB
/
trace_cleaner.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
package panik
import (
"io"
"regexp"
"strings"
)
const packagePath = "github.com/setlog/panik"
type traceCleaner struct {
destination io.Writer
buffer string
removeNextLine bool
}
func (tc *traceCleaner) Write(p []byte) (n int, err error) {
tc.buffer += string(p)
for {
nextLineIndex := strings.Index(tc.buffer, "\n") + 1
if nextLineIndex == 0 {
return len(p), nil
}
line := tc.buffer[:nextLineIndex]
tc.buffer = tc.buffer[nextLineIndex:]
if tc.removeNextLine {
n += len(line)
tc.removeNextLine = false
continue
}
if isUnwantedLine(line) {
n += len(line)
tc.removeNextLine = true
} else {
written, err := tc.destination.Write([]byte(line))
n += written
if err != nil {
return n, err
}
}
}
}
var unwantedLineRegExps []*regexp.Regexp = []*regexp.Regexp{
regexp.MustCompile(`^panic\(.*$`),
regexp.MustCompile(`^runtime/debug.Stack\(.*$`),
regexp.MustCompile(`^` + packagePath + `\..*\(.*$`),
}
func isUnwantedLine(line string) bool {
for _, regExp := range unwantedLineRegExps {
if regExp.MatchString(line[:len(line)-1]) {
return true
}
}
return false
}