-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwire.go
More file actions
347 lines (264 loc) · 5.82 KB
/
wire.go
File metadata and controls
347 lines (264 loc) · 5.82 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
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
package tlog
import (
"unicode/utf8"
"unsafe"
"nikand.dev/go/hacked/low"
"tlog.app/go/loc"
"tlog.app/go/tlog/tlwire"
)
type (
RawMessage []byte
Modify []byte
Timestamp int64
Tag string
Tags []Tag
FormatNext string
format struct {
Fmt string
Args []interface{}
}
)
const KeyAuto = ""
var (
Hidden = RawMessage{byte(tlwire.Special | tlwire.Hidden)}
None = RawMessage{byte(tlwire.Special | tlwire.None)}
Nil = RawMessage{byte(tlwire.Special | tlwire.Nil)}
Break = RawMessage{byte(tlwire.Special | tlwire.Break)}
NextAsHex = Modify{byte(tlwire.Semantic | tlwire.Hex)}
NextAsMessage = Modify{byte(tlwire.Semantic | WireMessage)}
NextAsType = FormatNext("%T")
)
// Wire semantic tags.
const (
WireLabel = tlwire.SemanticTlogBase + iota
WireID
WireMessage
WireEventKind
WireLogLevel
WireTag
_
_
_
_
SemanticCommunityBase = tlwire.SemanticTlogBase + 10
)
func AppendLabels(e *tlwire.Encoder, b []byte, kvs []interface{}) []byte {
const tag = tlwire.Semantic | WireLabel
var d tlwire.LowDecoder
w := len(b)
b = append(b, low.Spaces[:len(kvs)/2+1]...)
r := len(b)
b = AppendKVs(e, b, kvs)
for r < len(b) {
end := d.Skip(b, r)
w += copy(b[w:], b[r:end])
r = end
end = d.Skip(b, r)
if b[r] != byte(tag) {
b[w] = byte(tag)
w++
}
w += copy(b[w:], b[r:end])
r = end
}
return b[:w]
}
func NextIs(semantic int) Modify {
return Modify(tlwire.LowEncoder{}.AppendTag(nil, tlwire.Semantic, semantic))
}
func RawTag(tag tlwire.Tag, sub int) RawMessage {
return RawMessage(tlwire.LowEncoder{}.AppendTag(nil, tag, sub))
}
func Special(value int) RawMessage {
return RawMessage(tlwire.LowEncoder{}.AppendTag(nil, tlwire.Special, value))
}
func AppendKVs(e *tlwire.Encoder, b []byte, kvs []interface{}) []byte {
return appendKVs0(e, b, kvs)
}
//go:linkname appendKVs0 tlog.app/go/tlog.appendKVs
//go:noescape
func appendKVs0(e *tlwire.Encoder, b []byte, kvs []interface{}) []byte
func init() { // prevent deadcode warnings
appendKVs(nil, nil, nil)
}
func appendKVs(e *tlwire.Encoder, b []byte, kvs []interface{}) []byte {
for i := 0; i < len(kvs); {
var k string
switch el := kvs[i].(type) {
case string:
k = el
if k == KeyAuto {
k = autoKey(kvs[i:])
}
i++
case tlwire.TlogAppender:
b = el.TlogAppend(b)
i++
continue
default:
k = "MISSING_KEY"
}
b = e.AppendString(b, k)
value:
if i == len(kvs) {
b = append(b, byte(tlwire.Special|tlwire.Undefined))
break
}
switch v := kvs[i].(type) {
case int:
b = e.AppendInt(b, v)
case string:
b = e.AppendString(b, v)
case []byte:
b = e.AppendBytes(b, v)
case RawMessage:
b = append(b, v...)
case Modify:
b = append(b, v...)
i++
goto value
case FormatNext:
i++
if i == len(kvs) {
b = append(b, byte(tlwire.Special|tlwire.Undefined))
break
}
b = e.AppendFormatf(b, string(v), kvs[i])
default:
b = e.AppendValue(b, v)
}
i++
}
return b
}
func autoKey(kvs []any) (k string) {
if len(kvs) == 1 {
return "MISSING_VALUE"
}
if m, ok := kvs[1].(Modify); ok && string(m) == string(NextAsMessage) {
return KeyMessage
}
switch kvs[1].(type) {
case ID:
k = KeySpan
case LogLevel:
k = KeyLogLevel
case EventKind:
k = KeyEventKind
case loc.PC:
k = KeyCaller
case loc.PCs:
k = KeyCaller
case Tag, Tags:
k = KeyTag
default:
k = "UNSUPPORTED_AUTO_KEY"
}
return
}
func (ek EventKind) String() string {
return string(ek)
}
func (id ID) TlogAppend(b []byte) []byte {
var e tlwire.LowEncoder
b = append(b, byte(tlwire.Semantic|WireID))
return e.AppendBytes(b, id[:])
}
func (id *ID) TlogParse(p []byte, i int) int {
if p[i] != byte(tlwire.Semantic|WireID) {
panic("not an id")
}
i++
if p[i] != byte(tlwire.Bytes|16) {
panic("not an id")
}
i++
i += copy((*id)[:], p[i:])
return i
}
func (ek EventKind) TlogAppend(b []byte) []byte {
var e tlwire.LowEncoder
b = append(b, byte(tlwire.Semantic|WireEventKind))
return e.AppendString(b, string(ek))
}
func (ek *EventKind) TlogParse(p []byte, i int) int {
var d tlwire.LowDecoder
if p[i] != byte(tlwire.Semantic|WireEventKind) {
panic("not an event type")
}
i++
v, i := d.Bytes(p, i)
r, w := utf8.DecodeRune(v)
if w == utf8.RuneError || w != len(v) {
panic("bad rune")
}
*ek = EventKind(r)
return i
}
func (l LogLevel) TlogAppend(b []byte) []byte {
var e tlwire.LowEncoder
b = append(b, byte(tlwire.Semantic|WireLogLevel))
return e.AppendInt(b, int(l))
}
func (l *LogLevel) TlogParse(p []byte, i int) int {
var d tlwire.LowDecoder
if p[i] != byte(tlwire.Semantic|WireLogLevel) {
panic("not a log level")
}
i++
v, i := d.Signed(p, i)
*l = LogLevel(v)
return i
}
func (r RawMessage) TlogAppend(b []byte) []byte {
return append(b, r...)
}
func (r *RawMessage) TlogParse(p []byte, st int) (i int) {
var d tlwire.LowDecoder
i = d.Skip(p, st)
*r = append((*r)[:0], p[st:i]...)
return i
}
func (t Tag) TlogAppend(b []byte) []byte {
var e tlwire.Encoder
b = e.AppendSemantic(b, WireTag)
b = e.AppendString(b, string(t))
return b
}
func (ts Tags) TlogAppend(b []byte) []byte {
if len(ts) == 0 {
return b
}
var e tlwire.Encoder
b = e.AppendSemantic(b, WireTag)
b = e.AppendArray(b, len(ts))
for _, t := range ts {
b = e.AppendString(b, string(t))
}
return b
}
func IterTags(p []byte, st int, f func(Tag)) int {
var d tlwire.Decoder
var b []byte
if p[st] != byte(tlwire.Semantic|WireTag) {
panic("not a tag")
}
st++
tag, sub, i := d.Tag(p, st)
if tag == tlwire.String {
b, i = d.Bytes(p, st)
f(Tag(unsafe.String(unsafe.SliceData(b), len(b))))
return i
}
for el := 0; sub == -1 || el < int(sub); el++ {
if sub == -1 && d.Break(p, &i) {
break
}
if d.TagOnly(p, i) != tlwire.String {
panic("not a tag")
}
b, i = d.Bytes(p, i)
f(Tag(unsafe.String(unsafe.SliceData(b), len(b))))
}
return i
}