-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbark_binary.go
More file actions
251 lines (217 loc) · 5.81 KB
/
bark_binary.go
File metadata and controls
251 lines (217 loc) · 5.81 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
package bark
import (
"encoding/binary"
"io"
"math"
"sync"
"time"
)
const (
BinTypeInfo = uint16(1)
BinTagString = uint8(1)
BinTagInt = uint8(2)
BinTagInt8 = uint8(3)
BinTagInt16 = uint8(4)
BinTagInt32 = uint8(5)
BinTagInt64 = uint8(6)
BinTagUint = uint8(7)
BinTagUint8 = uint8(8)
BinTagUint16 = uint8(9)
BinTagUint32 = uint8(10)
BinTagUint64 = uint8(11)
BinTagFloat32 = uint8(12)
BinTagFloat64 = uint8(13)
BinTagBool = uint8(14)
BinTagErr = uint8(15)
BinTagComplex64 = uint8(16)
BinTagComplex128 = uint8(17)
BinTagUintptr = uint8(18)
BinTagBytes = uint8(19)
)
type BinaryLogger struct {
pool sync.Pool
out io.Writer
}
type BinaryEvent struct {
buf []byte
out io.Writer
pool *sync.Pool
}
func NewBinaryLogger(w io.Writer) *BinaryLogger {
l := &BinaryLogger{
out: w,
}
l.pool.New = func() any {
return &BinaryEvent{
buf: make([]byte, 0, 512),
out: w,
pool: &l.pool,
}
}
return l
}
func (l *BinaryLogger) Info() *BinaryEvent {
e := l.pool.Get().(*BinaryEvent)
e.buf = e.buf[:0]
e.buf = append(e.buf, 0, 0, 0, 0, 0, 0)
e.buf = binary.LittleEndian.AppendUint64(e.buf, uint64(time.Now().UnixNano()))
return e
}
// appendKey adds [KeyLen][KeyBytes]
func (e *BinaryEvent) appendKey(key string) {
if len(key) > 255 {
key = key[:255]
}
e.buf = append(e.buf, uint8(len(key)))
e.buf = append(e.buf, key...)
}
func (e *BinaryEvent) Str(key, val string) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagString)
if len(val) > 65535 {
val = val[:65535]
}
e.buf = binary.LittleEndian.AppendUint16(e.buf, uint16(len(val)))
e.buf = append(e.buf, val...)
return e
}
func (e *BinaryEvent) Bytes(key string, val []byte) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagBytes)
vLen := len(val)
if vLen > 65535 {
vLen = 65535
}
e.buf = binary.LittleEndian.AppendUint16(e.buf, uint16(vLen))
e.buf = append(e.buf, val[:vLen]...)
return e
}
// Integers
func (e *BinaryEvent) Int(key string, val int) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagInt)
e.buf = binary.LittleEndian.AppendUint64(e.buf, uint64(val))
return e
}
func (e *BinaryEvent) Int8(key string, val int8) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagInt8)
e.buf = append(e.buf, uint8(val))
return e
}
func (e *BinaryEvent) Int16(key string, val int16) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagInt16)
e.buf = binary.LittleEndian.AppendUint16(e.buf, uint16(val))
return e
}
func (e *BinaryEvent) Int32(key string, val int32) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagInt32)
e.buf = binary.LittleEndian.AppendUint32(e.buf, uint32(val))
return e
}
func (e *BinaryEvent) Int64(key string, val int64) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagInt64)
e.buf = binary.LittleEndian.AppendUint64(e.buf, uint64(val))
return e
}
// Unsigned Integers
func (e *BinaryEvent) Uint(key string, val uint) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagUint)
e.buf = binary.LittleEndian.AppendUint64(e.buf, uint64(val))
return e
}
func (e *BinaryEvent) Uint8(key string, val uint8) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagUint8)
e.buf = append(e.buf, val)
return e
}
func (e *BinaryEvent) Uint16(key string, val uint16) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagUint16)
e.buf = binary.LittleEndian.AppendUint16(e.buf, val)
return e
}
func (e *BinaryEvent) Uint32(key string, val uint32) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagUint32)
e.buf = binary.LittleEndian.AppendUint32(e.buf, val)
return e
}
func (e *BinaryEvent) Uint64(key string, val uint64) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagUint64)
e.buf = binary.LittleEndian.AppendUint64(e.buf, val)
return e
}
func (e *BinaryEvent) Uintptr(key string, val uintptr) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagUintptr)
e.buf = binary.LittleEndian.AppendUint64(e.buf, uint64(val))
return e
}
// Floats
func (e *BinaryEvent) Float32(key string, val float32) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagFloat32)
e.buf = binary.LittleEndian.AppendUint32(e.buf, math.Float32bits(val))
return e
}
func (e *BinaryEvent) Float64(key string, val float64) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagFloat64)
e.buf = binary.LittleEndian.AppendUint64(e.buf, math.Float64bits(val))
return e
}
// Complex
func (e *BinaryEvent) Complex64(key string, val complex64) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagComplex64)
e.buf = binary.LittleEndian.AppendUint32(e.buf, math.Float32bits(real(val)))
e.buf = binary.LittleEndian.AppendUint32(e.buf, math.Float32bits(imag(val)))
return e
}
func (e *BinaryEvent) Complex128(key string, val complex128) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagComplex128)
e.buf = binary.LittleEndian.AppendUint64(e.buf, math.Float64bits(real(val)))
e.buf = binary.LittleEndian.AppendUint64(e.buf, math.Float64bits(imag(val)))
return e
}
// Others
func (e *BinaryEvent) Bool(key string, val bool) *BinaryEvent {
e.appendKey(key)
e.buf = append(e.buf, BinTagBool)
if val {
e.buf = append(e.buf, 1)
} else {
e.buf = append(e.buf, 0)
}
return e
}
func (e *BinaryEvent) Error(err error) *BinaryEvent {
if err == nil {
return e
}
e.appendKey("error")
e.buf = append(e.buf, BinTagErr)
msg := err.Error()
if len(msg) > 65535 {
msg = msg[:65535]
}
e.buf = binary.LittleEndian.AppendUint16(e.buf, uint16(len(msg)))
e.buf = append(e.buf, msg...)
return e
}
func (e *BinaryEvent) Msg(msg string) {
e.Str("message", msg)
payloadSize := len(e.buf) - 6
binary.LittleEndian.PutUint16(e.buf[0:2], BinTypeInfo)
binary.LittleEndian.PutUint32(e.buf[2:6], uint32(payloadSize))
e.out.Write(e.buf)
e.pool.Put(e)
}