diff --git a/Cargo.toml b/Cargo.toml index fe9c7f6..0b75f34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,8 +22,8 @@ tracing = {version = "0.1", default-features = false, features = ["std"]} tracing-subscriber = {version="0.3", default-features = false, features=["std", "fmt", "registry"]} tracelogging = ">= 1.1.0" tracelogging_dynamic = ">= 1.1.0" -eventheader = "0.2" -eventheader_dynamic = "0.2" +eventheader = "0.3.1" +eventheader_dynamic = "0.3.1" chrono = {version="0.4", default-features = false, features=["std"]} [dev-dependencies] diff --git a/benches/etw.rs b/benches/etw.rs index 0ecf6a7..f233b06 100644 --- a/benches/etw.rs +++ b/benches/etw.rs @@ -1,6 +1,7 @@ #![allow(unused_imports, dead_code)] use criterion::{criterion_group, criterion_main, Criterion}; +#[cfg(target_os = "windows")] use etw_helpers::{FileMode, SessionBuilder}; use tracing::{event, span, Level}; use tracing_etw::*; @@ -11,7 +12,7 @@ pub fn etw_benchmark(c: &mut Criterion) { let builder = LayerBuilder::new("etw_bench"); let provider_id = builder.get_provider_id().to_u128(); let _subscriber = tracing_subscriber::registry() - .with(builder.build_with_layer_filter()) + .with(builder.build()) .init(); let etw_session = SessionBuilder::new_file_mode( diff --git a/benches/user_events.rs b/benches/user_events.rs index 0cf2fb0..3535383 100644 --- a/benches/user_events.rs +++ b/benches/user_events.rs @@ -7,9 +7,9 @@ use tracing_subscriber::{self, prelude::*}; #[cfg(target_os = "linux")] pub fn user_events_benchmark(c: &mut Criterion) { - let builder = EtwLayerBuilder::new("user_events_bench"); + let builder = LayerBuilder::new("user_events_bench"); let _subscriber = tracing_subscriber::registry() - .with(builder.build_with_layer_filter()) + .with(builder.build()) .init(); // Disabled provider diff --git a/src/bin/main.rs b/src/bin/main.rs index 0e6531f..c1bd6c5 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -8,8 +8,8 @@ fn main() { // .without_time(); let subscriber = tracing_subscriber::registry() - .with(LayerBuilder::new("test").build_with_layer_filter()) - .with(LayerBuilder::new_common_schema_events("test2").build_with_layer_filter()) + .with(LayerBuilder::new("test").build()) // Collects everything + .with(LayerBuilder::new_common_schema_events("test2").build_with_target("geneva")) .with(tracing_subscriber::fmt::layer().with_span_events(FmtSpan::ACTIVE)); let _sub = subscriber.try_init(); @@ -39,4 +39,6 @@ fn main() { drop(_enter); drop(span); + + event!(target: "geneva", Level::INFO, "Only for geneva!"); } diff --git a/src/layer.rs b/src/layer.rs index ecb0caa..9fa2868 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -3,9 +3,12 @@ use std::{pin::Pin, sync::Arc}; use tracelogging::Guid; use tracing::{span, Subscriber}; -use tracing_subscriber::filter::Filtered; +use tracing_subscriber::filter::{Filtered, FilterExt, Targets, combinator::And}; use tracing_subscriber::layer::Filter; use tracing_subscriber::{registry::LookupSpan, Layer}; +use tracing::metadata::LevelFilter; + +use crate::native::ProviderGroup; use crate::native::{EventMode, EventWriter}; use crate::values::*; @@ -157,7 +160,7 @@ where } #[cfg(feature = "global_filter")] - pub fn build_with_global_filter(self) -> EtwLayer + pub fn build(self) -> EtwLayer where S: Subscriber + for<'a> LookupSpan<'a>, { @@ -175,17 +178,30 @@ where } } - #[cfg(not(feature = "global_filter"))] - pub fn build_with_layer_filter( - self, - ) -> Filtered, EtwFilter, S> + fn build_target_filter(&self, target: &'static str,) -> Targets { + let mut targets = Targets::new().with_target(&self.provider_name, LevelFilter::TRACE); + + match self.provider_group { + ProviderGroup::Windows(_guid) => {}, + ProviderGroup::Linux(ref name) => { + targets = targets.with_target(name.clone(), LevelFilter::TRACE); + } + _ => {} + } + + if !target.is_empty() { + targets = targets.with_target(target, LevelFilter::TRACE) + } + + targets + } + + fn build_layer(&self) -> EtwLayer where S: Subscriber + for<'a> LookupSpan<'a>, Mode::Provider: EventWriter + 'static, { - self.validate_config(); - - let layer = EtwLayer:: { + EtwLayer:: { provider: Mode::Provider::new( &self.provider_name, &self.provider_id, @@ -194,13 +210,54 @@ where ), default_keyword: self.default_keyword, _p: PhantomData, - }; + } + } - let filter = EtwFilter:: { - provider: layer.provider.clone(), + fn build_filter(&self, provider: Pin>) -> EtwFilter + where + S: Subscriber + for<'a> LookupSpan<'a>, + P: EventWriter + 'static, + { + EtwFilter:: { + provider, default_keyword: self.default_keyword, _p: PhantomData, - }; + } + } + + #[cfg(not(feature = "global_filter"))] + pub fn build_with_target( + self, + target: &'static str, + ) -> Filtered, And, Targets, S>, S> + where + S: Subscriber + for<'a> LookupSpan<'a>, + Mode::Provider: EventWriter + 'static, + { + self.validate_config(); + + let layer = self.build_layer(); + + let filter = self.build_filter(layer.provider.clone()); + + let targets = self.build_target_filter(target); + + layer.with_filter(filter.and(targets)) + } + + #[cfg(not(feature = "global_filter"))] + pub fn build( + self + ) -> Filtered, EtwFilter, S> + where + S: Subscriber + for<'a> LookupSpan<'a>, + Mode::Provider: EventWriter + 'static, + { + self.validate_config(); + + let layer = self.build_layer(); + + let filter = self.build_filter(layer.provider.clone()); layer.with_filter(filter) }