Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[etw] Add String() functions, JSON field option #285

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions pkg/etw/eventdescriptor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build windows

package etw

import "fmt"

// Channel represents the ETW logging channel that is used. It can be used by
// event consumers to give an event special treatment.
type Channel uint8
Expand All @@ -19,7 +19,11 @@ const (
// will always be collected.
type Level uint8

var _ fmt.Stringer = Level(0)

// Predefined ETW log levels from winmeta.xml in the Windows SDK.
//
//go:generate go run golang.org/x/tools/cmd/stringer -type=Level -trimprefix=Level
const (
LevelAlways Level = iota
LevelCritical
Expand All @@ -32,7 +36,11 @@ const (
// Opcode represents the operation that the event indicates is being performed.
type Opcode uint8

var _ fmt.Stringer = Opcode(0)

// Predefined ETW opcodes from winmeta.xml in the Windows SDK.
//
//go:generate go run golang.org/x/tools/cmd/stringer -type=Opcode -trimprefix=Opcode
const (
// OpcodeInfo indicates an informational event.
OpcodeInfo Opcode = iota
Expand All @@ -46,7 +54,7 @@ const (
OpcodeDCStop
)

// EventDescriptor represents various metadata for an ETW event.
// eventDescriptor represents various metadata for an ETW event.
type eventDescriptor struct {
id uint16
version uint8
Expand All @@ -57,7 +65,7 @@ type eventDescriptor struct {
keyword uint64
}

// NewEventDescriptor returns an EventDescriptor initialized for use with
// newEventDescriptor returns an EventDescriptor initialized for use with
// TraceLogging.
func newEventDescriptor() *eventDescriptor {
// Standard TraceLogging events default to the TraceLogging channel, and
Expand All @@ -68,7 +76,7 @@ func newEventDescriptor() *eventDescriptor {
}
}

// Identity returns the identity of the event. If the identity is not 0, it
// identity returns the identity of the event. If the identity is not 0, it
// should uniquely identify the other event metadata (contained in
// EventDescriptor, and field metadata). Only the lower 24 bits of this value
// are relevant.
Expand All @@ -78,7 +86,7 @@ func (ed *eventDescriptor) identity() uint32 {
return (uint32(ed.version) << 16) | uint32(ed.id)
}

// SetIdentity sets the identity of the event. If the identity is not 0, it
// setIdentity sets the identity of the event. If the identity is not 0, it
// should uniquely identify the other event metadata (contained in
// EventDescriptor, and field metadata). Only the lower 24 bits of this value
// are relevant.
Expand Down
8 changes: 8 additions & 0 deletions pkg/etw/fieldopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func StringField(name string, value string) FieldOpt {
}
}

// JSONStringField adds a JSON-encoded string field to the event.
func JSONStringField(name string, value string) FieldOpt {
return func(em *eventMetadata, ed *eventData) {
em.writeField(name, inTypeANSIString, outTypeJSON, 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) {
Expand Down
28 changes: 28 additions & 0 deletions pkg/etw/level_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions pkg/etw/opcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.