-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraces.go
98 lines (80 loc) · 1.93 KB
/
traces.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
package stacktrace
type TracesOpt interface {
Apply(o *TracesOptions)
}
type TracesOptions struct {
// EnsureDuplicates ensures that duplicates are not printed
EnsureDuplicates bool
dupLocs map[string]struct{}
}
func NewTracesOptions() *TracesOptions {
opts := &TracesOptions{
EnsureDuplicates: false,
dupLocs: make(map[string]struct{}),
}
return opts
}
type ensureDuplicatesOpt struct{}
func (ensureDuplicatesOpt) Apply(o *TracesOptions) {
o.EnsureDuplicates = true
}
func WithEnsureDuplicates() TracesOpt {
return &ensureDuplicatesOpt{}
}
type Stack struct {
LinePos *string
Severity *Severity
Message string
Type *Type
}
func NewStack() *Stack {
return &Stack{}
}
type Trace struct {
Stack []Stack
}
func NewTrace() *Trace {
return &Trace{Stack: make([]Stack, 0)}
}
func (st *StackTrace) getTraces(opts *TracesOptions) []Trace {
traces := make([]Trace, 0)
tracesWithList := func() []Trace {
for _, elem := range st.List {
elemTraces := elem.getTraces(opts)
traces = append(traces, elemTraces...)
}
return traces
}
trace := NewTrace()
stack := NewStack()
stack.LinePos = st.GetLocWithPosPtr()
stack.Severity = st.Severity
stack.Message = st.MessageWithInfo()
stack.Type = st.Type
if stack.LinePos != nil {
if _, ok := opts.dupLocs[*stack.LinePos]; ok {
return tracesWithList()
}
}
trace.Stack = append(trace.Stack, *stack)
if st.Wrapped != nil {
wrappedTraces := st.Wrapped.getTraces(opts)
if len(wrappedTraces) == 0 {
return tracesWithList()
}
for i := range wrappedTraces {
trace.Stack = append(trace.Stack, wrappedTraces[i].Stack...)
}
} else if opts.EnsureDuplicates && stack.LinePos != nil {
opts.dupLocs[*stack.LinePos] = struct{}{}
}
traces = append(traces, *trace)
return tracesWithList()
}
func (st *StackTrace) GetTraces(opts ...TracesOpt) []Trace {
o := NewTracesOptions()
for _, opt := range opts {
opt.Apply(o)
}
return st.getTraces(o)
}