This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire.go
217 lines (166 loc) · 3.13 KB
/
wire.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
package tlog
import (
"unicode/utf8"
_ "unsafe"
"github.com/nikandfor/loc"
"github.com/nikandfor/tlog/tlwire"
)
type (
RawMessage []byte
Modify []byte
Timestamp int64
FormatNext string
format struct {
Fmt string
Args []interface{}
}
)
const KeyAuto = ""
var (
HexNext = Modify{tlwire.Semantic | tlwire.Hex}
)
const (
WireLabel = tlwire.SemanticTlogBase + iota
WireID
WireMessage
WireEventKind
WireLogLevel
_
_
_
_
_
SemanticUserBase
)
var (
e tlwire.Encoder
d tlwire.Decoder
)
func AppendKVs(b []byte, kvs []interface{}) []byte {
return appendKVs0(b, kvs)
}
//go:linkname appendKVs0 github.com/nikandfor/tlog.appendKVs
//go:noescape
func appendKVs0(b []byte, kvs []interface{}) []byte
func appendKVs(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 RawMessage:
b = append(b, el...)
i++
continue
default:
k = "MISSING_KEY"
}
b = e.AppendString(b, k)
value:
if i == len(kvs) {
b = append(b, tlwire.Special|tlwire.Undefined)
break
}
switch v := kvs[i].(type) {
case string:
b = e.AppendString(b, v)
case int:
b = e.AppendInt(b, v)
case Modify:
b = append(b, v...)
i++
goto value
case FormatNext:
i++
if i == len(kvs) {
b = append(b, tlwire.Special|tlwire.Undefined)
break
}
b = e.AppendFormat(b, string(v), kvs[i])
default:
b = e.AppendValue(b, v)
}
i++
}
return b
}
func autoKey(kvs []interface{}) (k string) {
if len(kvs) == 1 {
return "MISSING_VALUE"
}
switch kvs[1].(type) {
// case Message:
// k = KeyMessage
case ID:
k = KeySpan
case LogLevel:
k = KeyLogLevel
case EventKind:
k = KeyEventKind
case loc.PC:
k = KeyCaller
case loc.PCs:
k = KeyCaller
default:
k = "UNSUPPORTED_AUTO_KEY"
}
return
}
func (id ID) TlogAppend(b []byte) []byte {
b = append(b, tlwire.Semantic|WireID)
return e.AppendBytes(b, id[:])
}
func (id *ID) TlogParse(p []byte, i int) int {
if p[i] != tlwire.Semantic|WireID {
panic("not an id")
}
i++
if p[i] != tlwire.Bytes|16 {
panic("not an id")
}
i++
i += copy((*id)[:], p[i:])
return i
}
func (ek EventKind) TlogAppend(b []byte) []byte {
b = append(b, tlwire.Semantic|WireEventKind)
return e.AppendString(b, string(ek))
}
func (ek *EventKind) TlogParse(p []byte, i int) int {
if p[i] != 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 {
b = append(b, tlwire.Semantic|WireLogLevel)
return e.AppendInt(b, int(l))
}
func (l *LogLevel) TlogParse(p []byte, i int) int {
if p[i] != 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) {
i = d.Skip(p, st)
*r = append((*r)[:0], p[st:i]...)
return i
}