-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtraces.go
More file actions
106 lines (89 loc) · 2.3 KB
/
traces.go
File metadata and controls
106 lines (89 loc) · 2.3 KB
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
104
105
106
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 _, wt := range wrappedTraces {
combined := Trace{Stack: make([]Stack, len(trace.Stack), len(trace.Stack)+len(wt.Stack))}
copy(combined.Stack, trace.Stack)
combined.Stack = append(combined.Stack, wt.Stack...)
traces = append(traces, combined)
}
} else {
if opts.EnsureDuplicates && stack.LinePos != nil {
opts.dupLocs[*stack.LinePos] = struct{}{}
}
// Suppress standalone emission for locationless container nodes (no LinePos,
// non-empty List): they are pure grouping headers, not actionable errors.
if stack.LinePos != nil || len(st.List) == 0 {
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)
}