forked from microsoft/go-winio
/
fieldopt.go
502 lines (462 loc) · 13.8 KB
/
fieldopt.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package etw
import (
"fmt"
"math"
"reflect"
"unsafe"
)
// FieldOpt defines the option function type that can be passed to
// Provider.WriteEvent to add fields to the event.
type FieldOpt func(em *eventMetadata, ed *eventData)
// WithFields returns the variadic arguments as a single slice.
func WithFields(opts ...FieldOpt) []FieldOpt {
return opts
}
// BoolField adds a single bool field to the event.
func BoolField(name string, value bool) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeUint8, outTypeBoolean, 0)
bool8 := uint8(0)
if value {
bool8 = uint8(1)
}
ed.writeUint8(bool8)
}
}
// BoolArray adds an array of bool to the event.
func BoolArray(name string, values []bool) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeUint8, outTypeBoolean, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
bool8 := uint8(0)
if v {
bool8 = uint8(1)
}
ed.writeUint8(bool8)
}
}
}
// StringField adds a single string field to the event.
func StringField(name string, value string) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeANSIString, outTypeUTF8, 0)
ed.writeString(value)
}
}
// StringArray adds an array of string to the event.
func StringArray(name string, values []string) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeANSIString, outTypeUTF8, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeString(v)
}
}
}
// IntField adds a single int field to the event.
func IntField(name string, value int) FieldOpt {
switch unsafe.Sizeof(value) {
case 4:
return Int32Field(name, int32(value))
case 8:
return Int64Field(name, int64(value))
default:
panic("Unsupported int size")
}
}
// IntArray adds an array of int to the event.
func IntArray(name string, values []int) FieldOpt {
inType := inTypeNull
var writeItem func(*eventData, int)
switch unsafe.Sizeof(values[0]) {
case 4:
inType = inTypeInt32
writeItem = func(ed *eventData, item int) { ed.writeInt32(int32(item)) }
case 8:
inType = inTypeInt64
writeItem = func(ed *eventData, item int) { ed.writeInt64(int64(item)) }
default:
panic("Unsupported int size")
}
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inType, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
writeItem(ed, v)
}
}
}
// Int8Field adds a single int8 field to the event.
func Int8Field(name string, value int8) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeInt8, outTypeDefault, 0)
ed.writeInt8(value)
}
}
// Int8Array adds an array of int8 to the event.
func Int8Array(name string, values []int8) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeInt8, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeInt8(v)
}
}
}
// Int16Field adds a single int16 field to the event.
func Int16Field(name string, value int16) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeInt16, outTypeDefault, 0)
ed.writeInt16(value)
}
}
// Int16Array adds an array of int16 to the event.
func Int16Array(name string, values []int16) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeInt16, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeInt16(v)
}
}
}
// Int32Field adds a single int32 field to the event.
func Int32Field(name string, value int32) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeInt32, outTypeDefault, 0)
ed.writeInt32(value)
}
}
// Int32Array adds an array of int32 to the event.
func Int32Array(name string, values []int32) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeInt32, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeInt32(v)
}
}
}
// Int64Field adds a single int64 field to the event.
func Int64Field(name string, value int64) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeInt64, outTypeDefault, 0)
ed.writeInt64(value)
}
}
// Int64Array adds an array of int64 to the event.
func Int64Array(name string, values []int64) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeInt64, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeInt64(v)
}
}
}
// UintField adds a single uint field to the event.
func UintField(name string, value uint) FieldOpt {
switch unsafe.Sizeof(value) {
case 4:
return Uint32Field(name, uint32(value))
case 8:
return Uint64Field(name, uint64(value))
default:
panic("Unsupported uint size")
}
}
// UintArray adds an array of uint to the event.
func UintArray(name string, values []uint) FieldOpt {
inType := inTypeNull
var writeItem func(*eventData, uint)
switch unsafe.Sizeof(values[0]) {
case 4:
inType = inTypeUint32
writeItem = func(ed *eventData, item uint) { ed.writeUint32(uint32(item)) }
case 8:
inType = inTypeUint64
writeItem = func(ed *eventData, item uint) { ed.writeUint64(uint64(item)) }
default:
panic("Unsupported uint size")
}
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inType, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
writeItem(ed, v)
}
}
}
// Uint8Field adds a single uint8 field to the event.
func Uint8Field(name string, value uint8) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeUint8, outTypeDefault, 0)
ed.writeUint8(value)
}
}
// Uint8Array adds an array of uint8 to the event.
func Uint8Array(name string, values []uint8) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeUint8, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeUint8(v)
}
}
}
// Uint16Field adds a single uint16 field to the event.
func Uint16Field(name string, value uint16) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeUint16, outTypeDefault, 0)
ed.writeUint16(value)
}
}
// Uint16Array adds an array of uint16 to the event.
func Uint16Array(name string, values []uint16) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeUint16, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeUint16(v)
}
}
}
// Uint32Field adds a single uint32 field to the event.
func Uint32Field(name string, value uint32) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeUint32, outTypeDefault, 0)
ed.writeUint32(value)
}
}
// Uint32Array adds an array of uint32 to the event.
func Uint32Array(name string, values []uint32) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeUint32, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeUint32(v)
}
}
}
// Uint64Field adds a single uint64 field to the event.
func Uint64Field(name string, value uint64) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeUint64, outTypeDefault, 0)
ed.writeUint64(value)
}
}
// Uint64Array adds an array of uint64 to the event.
func Uint64Array(name string, values []uint64) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeUint64, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeUint64(v)
}
}
}
// UintptrField adds a single uintptr field to the event.
func UintptrField(name string, value uintptr) FieldOpt {
inType := inTypeNull
var writeItem func(*eventData, uintptr)
switch unsafe.Sizeof(value) {
case 4:
inType = inTypeHexInt32
writeItem = func(ed *eventData, item uintptr) { ed.writeUint32(uint32(item)) }
case 8:
inType = inTypeHexInt64
writeItem = func(ed *eventData, item uintptr) { ed.writeUint64(uint64(item)) }
default:
panic("Unsupported uintptr size")
}
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inType, outTypeDefault, 0)
writeItem(ed, value)
}
}
// UintptrArray adds an array of uintptr to the event.
func UintptrArray(name string, values []uintptr) FieldOpt {
inType := inTypeNull
var writeItem func(*eventData, uintptr)
switch unsafe.Sizeof(values[0]) {
case 4:
inType = inTypeHexInt32
writeItem = func(ed *eventData, item uintptr) { ed.writeUint32(uint32(item)) }
case 8:
inType = inTypeHexInt64
writeItem = func(ed *eventData, item uintptr) { ed.writeUint64(uint64(item)) }
default:
panic("Unsupported uintptr size")
}
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inType, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
writeItem(ed, v)
}
}
}
// Float32Field adds a single float32 field to the event.
func Float32Field(name string, value float32) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeFloat, outTypeDefault, 0)
ed.writeUint32(math.Float32bits(value))
}
}
// Float32Array adds an array of float32 to the event.
func Float32Array(name string, values []float32) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeFloat, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeUint32(math.Float32bits(v))
}
}
}
// Float64Field adds a single float64 field to the event.
func Float64Field(name string, value float64) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeDouble, outTypeDefault, 0)
ed.writeUint64(math.Float64bits(value))
}
}
// Float64Array adds an array of float64 to the event.
func Float64Array(name string, values []float64) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeArray(name, inTypeDouble, outTypeDefault, 0)
ed.writeUint16(uint16(len(values)))
for _, v := range values {
ed.writeUint64(math.Float64bits(v))
}
}
}
// Struct adds a nested struct to the event, the FieldOpts in the opts argument
// are used to specify the fields of the struct.
func Struct(name string, opts ...FieldOpt) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeStruct(name, uint8(len(opts)), 0)
for _, opt := range opts {
opt(em, ed)
}
}
}
// Currently, we support logging basic builtin types (int, string, etc), slices
// of basic builtin types, error, types derived from the basic types (e.g. "type
// foo int"), and structs (recursively logging their fields). We do not support
// slices of derived types (e.g. "[]foo").
//
// For types that we don't support, the value is formatted via fmt.Sprint, and
// we also log a message that the type is unsupported along with the formatted
// type. The intent of this is to make it easier to see which types are not
// supported in traces, so we can evaluate adding support for more types in the
// future.
func SmartField(name string, v interface{}) FieldOpt {
switch v := v.(type) {
case bool:
return BoolField(name, v)
case []bool:
return BoolArray(name, v)
case string:
return StringField(name, v)
case []string:
return StringArray(name, v)
case int:
return IntField(name, v)
case []int:
return IntArray(name, v)
case int8:
return Int8Field(name, v)
case []int8:
return Int8Array(name, v)
case int16:
return Int16Field(name, v)
case []int16:
return Int16Array(name, v)
case int32:
return Int32Field(name, v)
case []int32:
return Int32Array(name, v)
case int64:
return Int64Field(name, v)
case []int64:
return Int64Array(name, v)
case uint:
return UintField(name, v)
case []uint:
return UintArray(name, v)
case uint8:
return Uint8Field(name, v)
case []uint8:
return Uint8Array(name, v)
case uint16:
return Uint16Field(name, v)
case []uint16:
return Uint16Array(name, v)
case uint32:
return Uint32Field(name, v)
case []uint32:
return Uint32Array(name, v)
case uint64:
return Uint64Field(name, v)
case []uint64:
return Uint64Array(name, v)
case uintptr:
return UintptrField(name, v)
case []uintptr:
return UintptrArray(name, v)
case float32:
return Float32Field(name, v)
case []float32:
return Float32Array(name, v)
case float64:
return Float64Field(name, v)
case []float64:
return Float64Array(name, v)
case error:
return StringField(name, v.Error())
default:
switch rv := reflect.ValueOf(v); rv.Kind() {
case reflect.Bool:
return SmartField(name, rv.Bool())
case reflect.Int:
return SmartField(name, int(rv.Int()))
case reflect.Int8:
return SmartField(name, int8(rv.Int()))
case reflect.Int16:
return SmartField(name, int16(rv.Int()))
case reflect.Int32:
return SmartField(name, int32(rv.Int()))
case reflect.Int64:
return SmartField(name, int64(rv.Int()))
case reflect.Uint:
return SmartField(name, uint(rv.Uint()))
case reflect.Uint8:
return SmartField(name, uint8(rv.Uint()))
case reflect.Uint16:
return SmartField(name, uint16(rv.Uint()))
case reflect.Uint32:
return SmartField(name, uint32(rv.Uint()))
case reflect.Uint64:
return SmartField(name, uint64(rv.Uint()))
case reflect.Uintptr:
return SmartField(name, uintptr(rv.Uint()))
case reflect.Float32:
return SmartField(name, float32(rv.Float()))
case reflect.Float64:
return SmartField(name, float64(rv.Float()))
case reflect.String:
return SmartField(name, rv.String())
case reflect.Struct:
fields := make([]FieldOpt, 0, rv.NumField())
for i := 0; i < rv.NumField(); i++ {
field := rv.Field(i)
if field.CanInterface() {
fields = append(fields, SmartField(name, field.Interface()))
}
}
return Struct(name, fields...)
}
}
return StringField(name, fmt.Sprintf("(Unsupported: %T) %v", v, v))
}