-
Notifications
You must be signed in to change notification settings - Fork 2
/
crashreport.go
275 lines (254 loc) · 6.35 KB
/
crashreport.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package mcla
import (
"bytes"
"errors"
"io"
"strings"
)
var (
ErrUnexpectedIndent = errors.New("Crash report details format incorrect: unexpected indent")
ErrMissingColon = errors.New("Crash report details format incorrect: missing colon")
)
var (
crashReportHeader = strings.ToUpper("---- Minecraft Crash Report ----")
headThreadKey = strings.ToUpper("Head")
affectedLevelKey = strings.ToUpper("Affected level")
descriptionHeader = strings.ToUpper("Description:")
detailsKeyHeader = strings.ToUpper("Details:")
stacktraceHeader = strings.ToUpper("Stacktrace:")
threadKeyHeader = strings.ToUpper("Thread:")
)
func hasIndent(line []byte) bool {
return bytes.HasPrefix(line, ([]byte)("\t")) || bytes.HasPrefix(line, ([]byte)(" "))
}
func hasDbIndent(line []byte) bool {
return bytes.HasPrefix(line, ([]byte)("\t\t")) || bytes.HasPrefix(line, ([]byte)(" "))
}
// Details:
type ReportDetails map[string][]string
func parseReportDetails(sc *lineScanner) (d ReportDetails, err error) {
if !sc.Scan() {
return
}
return parseReportDetails0(sc)
}
func parseReportDetails0(sc *lineScanner) (d ReportDetails, err error) {
d = make(ReportDetails)
line := sc.Bytes()
for {
if len(line) < 2 || !hasIndent(line) {
return
}
if hasDbIndent(line) {
return nil, ErrUnexpectedIndent
}
line = line[1:]
i := bytes.IndexByte(line, ':')
if i < 0 {
return nil, ErrMissingColon
}
var (
key string = (string)(bytes.TrimSpace(line[:i]))
values []string
)
if line = bytes.TrimSpace(line[i+1:]); len(line) > 0 {
values = []string{(string)(line)}
}
for {
if !sc.Scan() {
d.set(key, values)
return
}
if line = sc.Bytes(); len(line) < 2 || !hasIndent(line) {
d.set(key, values)
return
}
if !hasDbIndent(line) {
d.set(key, values)
break
}
values = append(values, (string)(bytes.TrimSpace(line)))
}
}
}
func (d ReportDetails) set(key string, values []string) {
d[strings.ToUpper(key)] = values
}
func (d ReportDetails) Has(key string) (ok bool) {
_, ok = d[strings.ToUpper(key)]
return
}
func (d ReportDetails) Get(key string) (value string) {
values, ok := d[strings.ToUpper(key)]
if !ok {
return ""
}
return strings.Join(values, "\n")
}
func (d ReportDetails) GetValues(key string) (values []string) {
return d[strings.ToUpper(key)]
}
// -- Head --
type HeadThread struct {
Thread string `json:"thread"`
Stacktrace Stacktrace `json:"stacktrace"`
}
func parseHeadThread(sc *lineScanner) (res HeadThread, err error) {
if !sc.Scan() {
return
}
for {
oline := sc.Text()
line := strings.ToUpper(oline)
switch {
case strings.HasPrefix(line, "--"):
return
case strings.HasPrefix(line, threadKeyHeader):
res.Thread = strings.TrimSpace(oline[len(threadKeyHeader):])
if !sc.Scan() {
return
}
case strings.HasPrefix(line, stacktraceHeader):
res.Stacktrace = parseStacktrace(sc)
default:
if !sc.Scan() {
return
}
}
}
}
// -- Affected level --
type AffectedLevel struct {
Details ReportDetails `json:"details"`
Stacktrace Stacktrace `json:"stacktrace"`
}
func parseAffectedLevel(sc *lineScanner) (res AffectedLevel, err error) {
if !sc.Scan() {
return
}
firstline := true
for {
line := strings.ToUpper(sc.Text())
switch {
case strings.HasPrefix(line, "--"):
return
case strings.HasPrefix(line, detailsKeyHeader):
if res.Details, err = parseReportDetails(sc); err != nil {
return
}
case firstline && hasIndent(sc.Bytes()):
if res.Details, err = parseReportDetails0(sc); err != nil {
return
}
case strings.HasPrefix(line, stacktraceHeader):
res.Stacktrace = parseStacktrace(sc)
default:
if !sc.Scan() {
return
}
}
firstline = false
}
}
type DetailsItem struct {
Details ReportDetails `json:"details"`
}
func parseDetailsItem(sc *lineScanner) (res DetailsItem, err error) {
if !sc.Scan() {
return
}
firstline := true
for {
line := strings.ToUpper(sc.Text())
switch {
case strings.HasPrefix(line, "--"):
return
case strings.HasPrefix(line, detailsKeyHeader):
if res.Details, err = parseReportDetails(sc); err != nil {
return
}
case firstline && hasIndent(sc.Bytes()):
if res.Details, err = parseReportDetails0(sc); err != nil {
return
}
default:
if !sc.Scan() {
return
}
}
firstline = false
}
}
type CrashReport struct { // ---- Minecraft Crash Report ----
Description string `json:"description"` // Description:
Error *JavaError `json:"error"`
HeadThread HeadThread `json:"head"` // -- Head --
AffectedLevel AffectedLevel `json:"affectedLevel"` // -- Affected level --
OtherDetails map[string]DetailsItem `json:"others"` // -- <KEY> --
}
func ParseCrashReport(r io.Reader) (report *CrashReport, err error) {
sc := newLineScanner(r)
for {
if !sc.Scan() {
if err = sc.Err(); err == nil {
err = io.EOF
}
return
}
if strings.HasSuffix(strings.ToUpper(strings.TrimSpace(sc.Text())), crashReportHeader) {
break
}
}
report = &CrashReport{
OtherDetails: make(map[string]DetailsItem),
}
if !sc.Scan() {
return
}
var line string
var flag int = 0
for {
line = sc.Text()
uline := strings.ToUpper(line)
switch {
case len(uline) == 0 && flag == 1:
flag = 2
report.Error = parseJavaError(sc)
case strings.HasPrefix(uline, descriptionHeader):
if flag != 0 {
return nil, errors.New("Key `Description` duplicated")
}
report.Description = strings.TrimSpace(line[len(descriptionHeader):])
if !sc.Scan() {
return
}
flag = 1
case strings.HasPrefix(uline, "-- ") && strings.HasSuffix(uline, " --"):
name := strings.ToUpper((string)(uline[len("-- ") : len(uline)-len(" --")]))
switch {
case name == headThreadKey:
if report.HeadThread, err = parseHeadThread(sc); err != nil {
return
}
case name == affectedLevelKey:
if report.AffectedLevel, err = parseAffectedLevel(sc); err != nil {
return
}
report.OtherDetails[affectedLevelKey] = DetailsItem{report.AffectedLevel.Details}
default:
var details DetailsItem
if details, err = parseDetailsItem(sc); err != nil {
return
}
report.OtherDetails[name] = details
}
default:
if !sc.Scan() {
return
}
}
}
}
func (report *CrashReport) GetDetails(key string) (value DetailsItem) {
return report.OtherDetails[strings.ToUpper(key)]
}