Merge pull request #132 from jstarks/etwtime

pkg/etw: Add support for time.Time
This commit is contained in:
Justin
2019-05-07 10:25:08 -07:00
committed by GitHub
2 changed files with 18 additions and 0 deletions
+6
View File
@@ -3,6 +3,7 @@ package etw
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"syscall"
) )
// eventData maintains a buffer which builds up the data for an ETW event. It // eventData maintains a buffer which builds up the data for an ETW event. It
@@ -63,3 +64,8 @@ func (ed *eventData) writeUint32(value uint32) {
func (ed *eventData) writeUint64(value uint64) { func (ed *eventData) writeUint64(value uint64) {
binary.Write(&ed.buffer, binary.LittleEndian, value) binary.Write(&ed.buffer, binary.LittleEndian, value)
} }
// writeFiletime appends a FILETIME to the buffer.
func (ed *eventData) writeFiletime(value syscall.Filetime) {
binary.Write(&ed.buffer, binary.LittleEndian, value)
}
+12
View File
@@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"math" "math"
"reflect" "reflect"
"syscall"
"time"
"unsafe" "unsafe"
) )
@@ -380,6 +382,14 @@ func Struct(name string, opts ...FieldOpt) FieldOpt {
} }
} }
// Time adds a time to the event.
func Time(name string, value time.Time) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeFileTime, outTypeDateTimeUTC, 0)
ed.writeFiletime(syscall.NsecToFiletime(value.UTC().UnixNano()))
}
}
// Currently, we support logging basic builtin types (int, string, etc), slices // 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 // 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 // foo int"), and structs (recursively logging their fields). We do not support
@@ -454,6 +464,8 @@ func SmartField(name string, v interface{}) FieldOpt {
return Float64Array(name, v) return Float64Array(name, v)
case error: case error:
return StringField(name, v.Error()) return StringField(name, v.Error())
case time.Time:
return Time(name, v)
default: default:
switch rv := reflect.ValueOf(v); rv.Kind() { switch rv := reflect.ValueOf(v); rv.Kind() {
case reflect.Bool: case reflect.Bool: