forked from phuslu/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
348 lines (336 loc) · 6.92 KB
/
formatter.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package log
import (
"reflect"
"strconv"
"unicode/utf16"
"unicode/utf8"
"unsafe"
)
// FormatterArgs is a parsed sturct from json input
type FormatterArgs struct {
Time string // "2019-07-10T05:35:54.277Z"
Level string // "info"
Caller string // "prog.go:42"
Goid string // "123"
Stack string // "<stack string>"
Message string // "a structure message"
KeyValues []struct {
Key string // "foo"
Value string // "bar"
ValueType byte // 's'
}
}
// Get gets the value associated with the given key.
func (args *FormatterArgs) Get(key string) (value string) {
for _, kv := range args.KeyValues {
if kv.Key == key {
value = kv.Value
break
}
}
return
}
func formatterArgsPos(key string) (pos int) {
switch key {
case "time":
pos = 1
case "level":
pos = 2
case "caller":
pos = 3
case "goid":
pos = 4
case "stack":
pos = 5
case "message", "msg":
pos = 6
}
return
}
// parseFormatterArgs extracts json string to json items
func parseFormatterArgs(json []byte, args *FormatterArgs) {
// treat formatter args as []string
const size = int(unsafe.Sizeof(FormatterArgs{}) / unsafe.Sizeof(""))
// nolint
slice := *(*[]string)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(args)), Len: size, Cap: size,
}))
var keys = true
var key, str []byte
var ok bool
var typ byte
_ = json[len(json)-1] // remove bounds check
if json[0] != '{' {
return
}
for i := 1; i < len(json); i++ {
if keys {
if json[i] != '"' {
continue
}
i, str, _, ok = jsonParseString(json, i+1)
if !ok {
return
}
key = str[1 : len(str)-1]
}
for ; i < len(json); i++ {
if json[i] <= ' ' || json[i] == ',' || json[i] == ':' {
continue
}
break
}
i, typ, str, ok = jsonParseAny(json, i, true)
if !ok {
return
}
switch typ {
case 's':
str = str[1 : len(str)-1]
case 'S':
str = jsonUnescape(str[1:len(str)-1], str[:0])
typ = 's'
}
pos := formatterArgsPos(b2s(key))
if pos == 0 && args.Time == "" {
pos = 1
}
if pos != 0 {
if pos == 2 && len(str) != 0 && str[len(str)-1] == '\n' {
str = str[:len(str)-1]
}
if slice[pos-1] == "" {
slice[pos-1] = b2s(str)
}
} else {
args.KeyValues = append(args.KeyValues, struct {
Key, Value string
ValueType byte
}{b2s(key), b2s(str), typ})
}
}
if args.Level == "" {
args.Level = "????"
}
}
func jsonParseString(json []byte, i int) (int, []byte, bool, bool) {
var s = i
_ = json[len(json)-1] // remove bounds check
for ; i < len(json); i++ {
if json[i] > '\\' {
continue
}
if json[i] == '"' {
return i + 1, json[s-1 : i+1], false, true
}
if json[i] == '\\' {
i++
for ; i < len(json); i++ {
if json[i] > '\\' {
continue
}
if json[i] == '"' {
// look for an escaped slash
if json[i-1] == '\\' {
n := 0
for j := i - 2; j > 0; j-- {
if json[j] != '\\' {
break
}
n++
}
if n%2 == 0 {
continue
}
}
return i + 1, json[s-1 : i+1], true, true
}
}
break
}
}
return i, json[s-1:], false, false
}
// jsonParseAny parses the next value from a json string.
// A Result is returned when the hit param is set.
// The return values are (i int, res Result, ok bool)
func jsonParseAny(json []byte, i int, hit bool) (int, byte, []byte, bool) {
var typ byte
var val []byte
_ = json[len(json)-1] // remove bounds check
for ; i < len(json); i++ {
if json[i] == '{' || json[i] == '[' {
i, val = jsonParseSquash(json, i)
if hit {
typ = 'o'
}
return i, typ, val, true
}
if json[i] <= ' ' {
continue
}
switch json[i] {
case '"':
i++
var vesc bool
var ok bool
i, val, vesc, ok = jsonParseString(json, i)
typ = 's'
if !ok {
return i, typ, val, false
}
if hit && vesc {
typ = 'S'
}
return i, typ, val, true
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
i, val = jsonParseNumber(json, i)
if hit {
typ = 'n'
}
return i, typ, val, true
case 't', 'f', 'n':
vc := json[i]
i, val = jsonParseLiteral(json, i)
if hit {
switch vc {
case 't':
typ = 't'
case 'f':
typ = 'f'
}
return i, typ, val, true
}
}
}
return i, typ, val, false
}
func jsonParseSquash(json []byte, i int) (int, []byte) {
// expects that the lead character is a '[' or '{' or '('
// squash the value, ignoring all nested arrays and objects.
// the first '[' or '{' or '(' has already been read
s := i
i++
depth := 1
_ = json[len(json)-1] // remove bounds check
for ; i < len(json); i++ {
if json[i] >= '"' && json[i] <= '}' {
switch json[i] {
case '"':
i++
s2 := i
for ; i < len(json); i++ {
if json[i] > '\\' {
continue
}
if json[i] == '"' {
// look for an escaped slash
if json[i-1] == '\\' {
n := 0
for j := i - 2; j > s2-1; j-- {
if json[j] != '\\' {
break
}
n++
}
if n%2 == 0 {
continue
}
}
break
}
}
case '{', '[', '(':
depth++
case '}', ']', ')':
depth--
if depth == 0 {
i++
return i, json[s:i]
}
}
}
}
return i, json[s:]
}
func jsonParseNumber(json []byte, i int) (int, []byte) {
var s = i
i++
_ = json[len(json)-1] // remove bounds check
for ; i < len(json); i++ {
if json[i] <= ' ' || json[i] == ',' || json[i] == ']' ||
json[i] == '}' {
return i, json[s:i]
}
}
return i, json[s:]
}
func jsonParseLiteral(json []byte, i int) (int, []byte) {
var s = i
i++
_ = json[len(json)-1] // remove bounds check
for ; i < len(json); i++ {
if json[i] < 'a' || json[i] > 'z' {
return i, json[s:i]
}
}
return i, json[s:]
}
// jsonUnescape unescapes a string
func jsonUnescape(json, str []byte) []byte {
_ = json[len(json)-1] // remove bounds check
var p [6]byte
for i := 0; i < len(json); i++ {
switch {
default:
str = append(str, json[i])
case json[i] < ' ':
return str
case json[i] == '\\':
i++
if i >= len(json) {
return str
}
switch json[i] {
default:
return str
case '\\':
str = append(str, '\\')
case '/':
str = append(str, '/')
case 'b':
str = append(str, '\b')
case 'f':
str = append(str, '\f')
case 'n':
str = append(str, '\n')
case 'r':
str = append(str, '\r')
case 't':
str = append(str, '\t')
case '"':
str = append(str, '"')
case 'u':
if i+5 > len(json) {
return str
}
m, _ := strconv.ParseUint(b2s(json[i+1:i+5]), 16, 64)
r := rune(m)
i += 5
if utf16.IsSurrogate(r) {
// need another code
if len(json[i:]) >= 6 && json[i] == '\\' &&
json[i+1] == 'u' {
// we expect it to be correct so just consume it
m, _ = strconv.ParseUint(b2s(json[i+2:i+6]), 16, 64)
r = utf16.DecodeRune(r, rune(m))
i += 6
}
}
str = append(str, p[:utf8.EncodeRune(p[:], r)]...)
i-- // backtrack index by one
}
}
}
return str
}