Skip to content

Commit

Permalink
TraceProperties: use typed values and sane defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
daladim committed Nov 7, 2022
1 parent 4129e37 commit 8dead92
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/native/etw_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ impl EventTraceProperties {
etw_trace_properties.BufferSize = trace_properties.buffer_size;
etw_trace_properties.MinimumBuffers = trace_properties.min_buffer;
etw_trace_properties.MaximumBuffers = trace_properties.max_buffer;
etw_trace_properties.FlushTimer = trace_properties.flush_timer;
etw_trace_properties.FlushTimer = trace_properties.flush_timer.as_secs().clamp(1, u32::MAX as u64) as u32; // See https://learn.microsoft.com/en-us/windows/win32/api/evntrace/ns-evntrace-event_trace_properties

if trace_properties.log_file_mode != 0 {
etw_trace_properties.LogFileMode = trace_properties.log_file_mode;
if trace_properties.log_file_mode.is_empty() == false {
etw_trace_properties.LogFileMode = trace_properties.log_file_mode.bits();
} else {
etw_trace_properties.LogFileMode =
u32::from(LoggingMode::RealTime) | u32::from(LoggingMode::NoPerProcBuffering);
(LoggingMode::EVENT_TRACE_REAL_TIME_MODE | LoggingMode::EVENT_TRACE_NO_PER_PROCESSOR_BUFFERING).bits()
}

etw_trace_properties.LogFileMode |= T::augmented_file_mode();
Expand Down
26 changes: 21 additions & 5 deletions src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
use std::marker::PhantomData;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;

use self::private::PrivateTraceTrait;

use crate::native::etw_types::{EventRecord, EventTraceProperties};
use crate::native::etw_types::{EventRecord, EventTraceProperties, LoggingMode};
use crate::native::{evntrace, version_helper};
use crate::native::evntrace::{ControlHandle, TraceHandle, start_trace, open_trace, process_trace, enable_provider, control_trace, close_trace};
use crate::provider::Provider;
Expand Down Expand Up @@ -51,18 +52,33 @@ type TraceResult<T> = Result<T, TraceError>;
/// These are some configuration settings that will be included in an [`EVENT_TRACE_PROPERTIES`](https://learn.microsoft.com/en-us/windows/win32/api/evntrace/ns-evntrace-event_trace_properties)
///
/// [More info](https://docs.microsoft.com/en-us/message-analyzer/specifying-advanced-etw-session-configuration-settings#configuring-the-etw-session)
#[derive(Debug, Copy, Clone, Default)]
#[derive(Debug, Copy, Clone)]
pub struct TraceProperties {
/// Represents the ETW Session in KB
pub buffer_size: u32,
/// Represents the ETW Session minimum number of buffers to use
pub min_buffer: u32,
/// Represents the ETW Session maximum number of buffers in the buffer pool
pub max_buffer: u32,
/// Represents the ETW Session flush interval in seconds
pub flush_timer: u32,
/// Represents the ETW Session flush interval.
///
/// This duration will be rounded to the closest second (and 0 will be translated as 1 second)
pub flush_timer: Duration,
/// Represents the ETW Session [Logging Mode](https://docs.microsoft.com/en-us/windows/win32/etw/logging-mode-constants)
pub log_file_mode: u32,
pub log_file_mode: LoggingMode,
}

impl Default for TraceProperties {
fn default() -> Self {
// Sane defaults, inspired by https://learn.microsoft.com/en-us/windows/win32/api/evntrace/ns-evntrace-event_trace_properties
TraceProperties {
buffer_size: 32,
min_buffer: 0,
max_buffer: 0,
flush_timer: Duration::from_secs(1),
log_file_mode: LoggingMode::EVENT_TRACE_REAL_TIME_MODE | LoggingMode::EVENT_TRACE_NO_PER_PROCESSOR_BUFFERING,
}
}
}

/// Data used by callbacks when the trace is running
Expand Down

0 comments on commit 8dead92

Please sign in to comment.