etw-wrapper
Strongly typed Event Tracing for Windows (ETW) logger generator for Rust.
Point the gen_etw_wrapper! macro at an ETW manifest file to emit a struct with a method for each event.
use ;
gen_etw_wrapper!;
[!NOTE] This crate is Windows-only. Use
#[cfg]gates when sharing a crate with other platforms.
The Windows APIs are linked directly, so there is no dependency on the windows crate and no version to keep in sync with the rest of your dependency tree. Types such as Guid, FileTime, and EventDescriptor are defined by this crate and are layout-compatible with their Win32 counterparts.
Getting started
Add the crate to your Cargo.toml:
[]
= "1.0.0"
The gen_etw_wrapper! macro is available by default through the macro feature. If you only want the runtime pieces, disable default features:
[]
= { = "1.0.0", = false }
See Manual API for instructions on using the runtime helpers directly.
Usage
Generating a provider
Invoke the macro at module scope with the path to your manifest. Paths are resolved relative to the invoking crate's CARGO_MANIFEST_DIR, the directory containing its Cargo.toml.
The generated struct uses the default naming convention PascalCaseProviderSymbolLogger. For example, PROVIDER_WIDGETSERVICE becomes ProviderWidgetserviceLogger.
gen_etw_wrapper!;
To pick your own struct name, map the provider's manifest symbol to it with ->:
gen_etw_wrapper!;
If the provider symbol is not a valid Rust identifier, quote it:
gen_etw_wrapper!;
Configuring event errors
Generated event methods return etw_wrapper::Result<()> by default. Applications that treat logging as best-effort can configure event methods to return () and ignore errors encountered while preparing or writing an event:
gen_etw_wrapper!;
This setting applies only to generated event methods. WidgetLogger::register() continues to return etw_wrapper::Result<WidgetLogger>.
Event methods can also panic on invalid input:
gen_etw_wrapper!;
Input and Windows write failures are configured independently:
| Option | Errors covered |
|---|---|
panic_on_input |
Errors detected while validating or preparing caller-provided fields |
panic_on_write |
Errors returned by the Windows event writer |
Both options accept true, false (the default), or a Rust cfg(...) predicate:
gen_etw_wrapper!;
Predicates are evaluated in the crate invoking the macro. When one is false, errors in that category are returned unless event_methods_return_unit = true. The generated method's return type depends only on event_methods_return_unit, which accepts only true or false, so it does not change between build configurations. Feature names used in a predicate must be declared by the crate invoking the macro.
Emitting events
Given this manifest template and event:
the macro generates:
[!IMPORTANT] The macro emits events but does not compile or register the provider's message and metadata resource tables. Applications that need decoded event names, fields, and messages must handle those resources separately, for example with
mc.exeandwevtutil.exe.To compile and embed the resources, use a build-time helper such as
embed-resource.
Type mapping
The macro maps manifest inType values to Rust parameter types as follows:
Manifest inType |
Rust type | Notes |
|---|---|---|
win:Int8 |
i8 |
|
win:UInt8 |
u8 |
|
win:Int16 |
i16 |
|
win:UInt16 |
u16 |
|
win:Int32 |
i32 |
|
win:UInt32, win:HexInt32 |
u32 |
|
win:Int64 |
i64 |
|
win:UInt64, win:HexInt64 |
u64 |
|
win:Float |
f32 |
|
win:Double |
f64 |
|
win:Boolean |
bool |
Encoded as a 32-bit Windows BOOL |
win:Pointer |
usize |
|
win:GUID |
Guid |
|
win:FILETIME |
FileTime |
|
win:SYSTEMTIME |
SystemTime |
|
win:UnicodeString |
&str |
Interior NULs become spaces, length="N" or length="Field" produces exactly that many units including NUL |
win:AnsiString |
&[u8] |
Caller supplies provider-code-page bytes and the final NUL |
win:AnsiString with outType="win:Utf8/win:Json/win:Xml" |
&str |
Encoded as UTF-8, interior NULs become spaces |
win:Binary |
&[u8] |
length="N" becomes &[u8; N], length="Field" derives that field (see below) |
win:SID |
&field::Sid |
Borrow a raw SID with the unsafe Sid::from_psid, or use an owned SidBuf |
Arrays
The macro supports the manifest count attribute for every input type in the table above. A constant count is enforced at the type level:
A field-reference count is derived from the array and is not exposed as a separate parameter:
Scalar arrays use &[T; N] or &[T]. String arrays use arrays or slices of &str; provider-code-page ANSI strings use &[u8]; and SID arrays use &field::Sid. The generated method packs variable-length elements into the contiguous layout expected by ETW. Fixed-length binary arrays use &[[u8; LENGTH]; COUNT] for two-dimensional fixed sizes.
When both binary length and count are field references, the method accepts a slice of byte slices and derives both fields. It returns Error::MismatchedArrayLengths if the elements do not all have the same length.
Variable-length fields
Binary
When a win:Binary field uses length="OtherField", the raw event structure expects the length in that field. The generated method derives the field from the slice length instead of exposing it as a parameter:
This template generates:
Strings
For win:UnicodeString length="N", the generated method emits exactly N UTF-16 code units as required by ETW: at most N - 1 content units, space padding when needed, and a terminating NUL.
A default win:AnsiString uses the provider's ANSI code page, so its generated parameter remains raw bytes. A fixed-length default ANSI field becomes &[u8; N]; the array must already contain the required padding and final NUL. If the manifest explicitly selects outType="win:Utf8", win:Json, or win:Xml, the generated method accepts &str, performs UTF-8 encoding, and handles fixed-length padding and truncation.
A string length="OtherField" names the field carrying the width, which decoders read instead of scanning for a NUL. Unlike a binary length, that field stays a parameter because it sets the width rather than describing a value the macro already has:
Each call encodes name to exactly name_length code units, padding or truncating as it does for a constant length. It returns Error::EmptyFixedLengthString if the width leaves no room for the terminator. A default win:AnsiString arrives already encoded, so its buffer must match the declared width; a mismatch returns Error::LengthMismatch.
Manual API
If you prefer not to use the macro, you can use the runtime directly. EtwLogger handles the ETW callbacks and unregisters the provider when it is dropped. It can be moved and stored freely.
The field module provides converters for passing fields to write: scalar and slice for fixed-size values; str16, to_u16cstring, and to_u16cstring_fixed_len for UTF-16 strings; str8 for provider-code-page byte buffers; to_cstring and to_cstring_fixed_len for explicit UTF-8 string output types; bytes for binary blobs; and sid for a Sid.
scalar and slice accept the integer and floating-point primitives, usize for win:Pointer, and the Guid, FileTime, and SystemTime types. The sealed field::Scalar trait prevents strings or custom types with padding from being copied into an event by mistake.
String helpers return Error::MissingNulTerminator for unterminated buffers and Error::EmptyFixedLengthString when a fixed length has no room for the terminator.
Call enabled() before preparing descriptors when you want to skip work for an event the provider is not currently accepting. Pass descriptors to write() in the same order as the fields in the manifest template.
use ;
let ctx = register?;
let descriptor = EventDescriptor ;
if ctx.enabled
License
Licensed under the ISC License.