6 releases (stable)

1.2.1 Jun 30, 2023
1.2.0 May 24, 2023
1.1.0 Mar 25, 2023
0.1.0 Sep 13, 2022

#223 in Windows APIs

Download history 44/week @ 2023-12-19 38/week @ 2023-12-26 316/week @ 2024-01-02 355/week @ 2024-01-09 234/week @ 2024-01-16 40/week @ 2024-01-23 3/week @ 2024-02-20 14/week @ 2024-02-27 1/week @ 2024-03-05 140/week @ 2024-03-12 82/week @ 2024-03-19 22/week @ 2024-03-26 57/week @ 2024-04-02

302 downloads per month
Used in opentelemetry-etw-logs

MIT license

220KB
2.5K SLoC

TraceLogging Dynamic for Rust

The tracelogging_dynamic crate provides a way to log TraceLogging events when the event schema is not known at compile-time.

This implementation is less user-friendly and has higher runtime costs than the implementation in the tracelogging crate. This implementation should only be used when the set of events to log cannot be deteremined ahead of time. For example, this might be useful when implementing a middle-layer library that provides generic logging facilities to a dynamic upper layer.

use tracelogging_dynamic as tld;

// Pinning is required because the register() method sets up a callback with ETW.
let provider =
    Box::pin(tld::Provider::new("MyCompany.MyComponent", &tld::Provider::options()));

// Register the provider. If you don't register (or if register fails) then enabled()
// will always return false and write() will be a no-op.
unsafe {
    provider.as_ref().register();
}

// If provider is not enabled for a given level + keyword, the write() call will do
// nothing. Check enabled(level, keyword) before building the event so we don't waste
// time on an event that nobody will receive.
let my_event_level = tld::Level::Verbose; // Severity level.
let my_event_keyword = 0x123; // User-defined category bits.
if provider.enabled(my_event_level, my_event_keyword) {
    let field1_value = "FieldValue";
    let field2_value = b'A';
    // Create and write an event with two fields:
    tld::EventBuilder::new()
        // Most events specify 0 for event tag.
        .reset("MyEventName", my_event_level, my_event_keyword, 0)
        // Most fields use Default for event format and 0 for field tag.
        .add_str8("FieldName1", field1_value, tld::OutType::Default, 0)
        .add_u8("FieldName2", field2_value, tld::OutType::String, 0)
        // If activity_id is None, event uses the current thread's activity.
        // If related_id is None, event will not have a related activity.
        .write(&provider, None, None);
}

Configuration

This crate supports the following configurable features:

  • etw: Use Windows ETW APIs to log events. If not enabled, all logging operations will be no-ops. Enabled by default.

In addition, this crate will log events only if compiled for a Windows operating system. If compiled for a non-Windows operating system, all logging operations will be no-ops.

Dependencies