Skip to content

Commit

Permalink
ValuesSplit values.rs into separate pieces (#18)
Browse files Browse the repository at this point in the history
* Split values.rs into separate pieces

values.rs was confusing as to why Visit was implemented multiple times for different structs. Split the two implementations into separate files with better names.

Also removed an unnecessary generic.

Also add some basic builder tests.

* fmt

* fmt
  • Loading branch information
Robo210 authored Dec 10, 2024
1 parent 2d1e313 commit 62a5bcb
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 278 deletions.
6 changes: 3 additions & 3 deletions src/layer/layer_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tracing_subscriber::{registry::LookupSpan, Layer};
use crate::{
native::{EventWriter, ProviderTypes},
statics::*,
values::*,
values::{span_values::*, *},
};

use super::*;
Expand Down Expand Up @@ -180,7 +180,7 @@ where
0
};

attrs.values().record(&mut ValueVisitor {
attrs.values().record(&mut SpanValueVisitor {
fields: &mut data.fields,
});

Expand Down Expand Up @@ -296,7 +296,7 @@ where
return;
};

values.record(&mut ValueVisitor {
values.record(&mut SpanValueVisitor {
fields: &mut data.fields,
});
}
Expand Down
36 changes: 36 additions & 0 deletions src/layer_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,39 @@ where
Ok(layer.with_filter(filter.and(targets)))
}
}

#[cfg(test)]
mod test {
use tracing_subscriber::{self, prelude::*};

use crate::native::GuidWrapper;

use super::LayerBuilder;

#[test]
fn build_normal() {
tracing_subscriber::registry()
.with(LayerBuilder::new("test_build_normal").build().unwrap());
}

#[test]
fn build_with_target() {
tracing_subscriber::registry().with(
LayerBuilder::new("test_build_with_target")
.with_default_keyword(5)
.build_with_target("asdf")
.unwrap(),
);
}

#[test]
fn build_provider_id() {
let provider_id = GuidWrapper::from_name("name");
tracing_subscriber::registry().with(
LayerBuilder::new("test_build_provider_id")
.with_provider_id(&provider_id)
.build()
.unwrap(),
);
}
}
25 changes: 14 additions & 11 deletions src/native/common_schema/etw_cs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{error::EtwError, values::*};
use crate::{
error::EtwError,
values::{event_values::*, *},
};
use std::{
cell::RefCell,
io::{Cursor, Write},
Expand All @@ -19,12 +22,14 @@ pub(crate) struct CommonSchemaPartCBuilder<'a> {
}

impl<'a> CommonSchemaPartCBuilder<'a> {
fn make_visitor(eb: &'a mut EventBuilder) -> VisitorWrapper<CommonSchemaPartCBuilder<'a>> {
VisitorWrapper::from(CommonSchemaPartCBuilder { eb })
fn make_visitor(
eb: &'a mut EventBuilder,
) -> EventBuilderVisitorWrapper<CommonSchemaPartCBuilder<'a>> {
EventBuilderVisitorWrapper::from(CommonSchemaPartCBuilder { eb })
}
}

impl<T> AddFieldAndValue<T> for CommonSchemaPartCBuilder<'_> {
impl AddFieldAndValue for CommonSchemaPartCBuilder<'_> {
fn add_field_value(&mut self, fv: &FieldAndValue) {
let mut field_name: &'static str = fv.field_name;

Expand All @@ -33,7 +38,7 @@ impl<T> AddFieldAndValue<T> for CommonSchemaPartCBuilder<'_> {
assert!(matches!(fv.value, ValueTypes::v_str(_)));
}

<&mut EventBuilder as AddFieldAndValue<EventBuilder>>::add_field_value(
<&mut EventBuilder as AddFieldAndValue>::add_field_value(
&mut self.eb,
&FieldAndValue {
field_name,
Expand Down Expand Up @@ -83,7 +88,7 @@ impl CommonSchemaProvider {

impl crate::native::ProviderTypes for CommonSchemaProvider {
type Provider = Self;
type ProviderGroupType = tracelogging_dynamic::Guid;
type ProviderGroupType = crate::native::native_guid;

#[inline(always)]
fn supports_enable_callback() -> bool {
Expand Down Expand Up @@ -141,7 +146,7 @@ impl crate::native::EventWriter<CommonSchemaProvider> for CommonSchemaProvider {
_timestamp: SystemTime,
_activity_id: &[u8; 16],
_related_activity_id: &[u8; 16],
_fields: &'b [crate::values::FieldValueIndex],
_fields: &'b [crate::values::span_values::FieldValueIndex],
_level: &tracing_core::Level,
_keyword: u64,
_event_tag: u32,
Expand All @@ -156,7 +161,7 @@ impl crate::native::EventWriter<CommonSchemaProvider> for CommonSchemaProvider {
start_stop_times: (std::time::SystemTime, std::time::SystemTime),
_activity_id: &[u8; 16],
_related_activity_id: &[u8; 16],
fields: &'b [crate::values::FieldValueIndex],
fields: &'b [crate::values::span_values::FieldValueIndex],
level: &tracing_core::Level,
keyword: u64,
event_tag: u32,
Expand Down Expand Up @@ -245,9 +250,7 @@ impl crate::native::EventWriter<CommonSchemaProvider> for CommonSchemaProvider {
let mut pfv = CommonSchemaPartCBuilder { eb: eb.deref_mut() };

for f in fields {
<CommonSchemaPartCBuilder<'_> as AddFieldAndValue<
CommonSchemaPartCBuilder<'_>,
>>::add_field_value(
<CommonSchemaPartCBuilder<'_> as AddFieldAndValue>::add_field_value(
&mut pfv,
&FieldAndValue {
field_name: f.field,
Expand Down
20 changes: 10 additions & 10 deletions src/native/common_schema/user_events_cs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::EtwError;
use crate::values::*;
use crate::values::{event_values::*, *};
use eventheader::*;
use eventheader_dynamic::EventBuilder;
use std::{
Expand All @@ -20,12 +20,14 @@ pub(crate) struct CommonSchemaPartCBuilder<'a> {
}

impl<'a> CommonSchemaPartCBuilder<'a> {
fn make_visitor(eb: &'a mut EventBuilder) -> VisitorWrapper<CommonSchemaPartCBuilder<'a>> {
VisitorWrapper::from(CommonSchemaPartCBuilder { eb })
fn make_visitor(
eb: &'a mut EventBuilder,
) -> EventBuilderVisitorWrapper<CommonSchemaPartCBuilder<'a>> {
EventBuilderVisitorWrapper::from(CommonSchemaPartCBuilder { eb })
}
}

impl<T> AddFieldAndValue<T> for CommonSchemaPartCBuilder<'_> {
impl AddFieldAndValue for CommonSchemaPartCBuilder<'_> {
fn add_field_value(&mut self, fv: &FieldAndValue) {
let mut field_name: &'static str = fv.field_name;

Expand All @@ -34,7 +36,7 @@ impl<T> AddFieldAndValue<T> for CommonSchemaPartCBuilder<'_> {
assert!(matches!(fv.value, ValueTypes::v_str(_)));
}

<&mut EventBuilder as AddFieldAndValue<EventBuilder>>::add_field_value(
<&mut EventBuilder as AddFieldAndValue>::add_field_value(
&mut self.eb,
&FieldAndValue {
field_name,
Expand Down Expand Up @@ -167,7 +169,7 @@ impl crate::native::EventWriter<CommonSchemaProvider> for CommonSchemaProvider {
_timestamp: SystemTime,
_activity_id: &[u8; 16],
_related_activity_id: &[u8; 16],
_fields: &'b [crate::values::FieldValueIndex],
_fields: &'b [crate::values::span_values::FieldValueIndex],
_level: &tracing_core::Level,
_keyword: u64,
_event_tag: u32,
Expand All @@ -182,7 +184,7 @@ impl crate::native::EventWriter<CommonSchemaProvider> for CommonSchemaProvider {
start_stop_times: (std::time::SystemTime, std::time::SystemTime),
_activity_id: &[u8; 16],
_related_activity_id: &[u8; 16],
fields: &'b [crate::values::FieldValueIndex],
fields: &'b [crate::values::span_values::FieldValueIndex],
level: &tracing_core::Level,
keyword: u64,
event_tag: u32,
Expand Down Expand Up @@ -277,9 +279,7 @@ impl crate::native::EventWriter<CommonSchemaProvider> for CommonSchemaProvider {
let mut pfv = CommonSchemaPartCBuilder { eb: eb.deref_mut() };

for f in fields {
<CommonSchemaPartCBuilder<'_> as AddFieldAndValue<
CommonSchemaPartCBuilder<'_>,
>>::add_field_value(
<CommonSchemaPartCBuilder<'_> as AddFieldAndValue>::add_field_value(
&mut pfv,
&FieldAndValue {
field_name: f.field,
Expand Down
17 changes: 10 additions & 7 deletions src/native/etw.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::statics::GLOBAL_ACTIVITY_SEED;
use crate::{error::EtwError, values::*};
use crate::{
error::EtwError,
values::{event_values::*, *},
};
use chrono::{Datelike, Timelike};
use std::{cell::RefCell, ops::DerefMut, pin::Pin, sync::Arc, time::SystemTime};
use tracelogging::*;
Expand Down Expand Up @@ -40,7 +43,7 @@ impl From<std::time::SystemTime> for Win32SystemTime {
}
}

impl<T> AddFieldAndValue<T> for &'_ mut tracelogging_dynamic::EventBuilder {
impl AddFieldAndValue for &'_ mut tracelogging_dynamic::EventBuilder {
fn add_field_value(&mut self, fv: &FieldAndValue) {
match fv.value {
ValueTypes::None => (),
Expand Down Expand Up @@ -171,7 +174,7 @@ impl super::EventWriter<Provider> for Provider {
timestamp: SystemTime,
activity_id: &[u8; 16],
related_activity_id: &[u8; 16],
fields: &'b [crate::values::FieldValueIndex],
fields: &'b [crate::values::span_values::FieldValueIndex],
level: &tracing_core::Level,
keyword: u64,
event_tag: u32,
Expand All @@ -194,7 +197,7 @@ impl super::EventWriter<Provider> for Provider {
);

for f in fields {
<&mut EventBuilder as AddFieldAndValue<EventBuilder>>::add_field_value(
<&mut EventBuilder as AddFieldAndValue>::add_field_value(
&mut eb.deref_mut(),
&FieldAndValue {
field_name: f.field,
Expand Down Expand Up @@ -227,7 +230,7 @@ impl super::EventWriter<Provider> for Provider {
start_stop_times: (std::time::SystemTime, std::time::SystemTime),
activity_id: &[u8; 16],
related_activity_id: &[u8; 16],
fields: &'b [crate::values::FieldValueIndex],
fields: &'b [crate::values::span_values::FieldValueIndex],
level: &tracing_core::Level,
keyword: u64,
event_tag: u32,
Expand All @@ -250,7 +253,7 @@ impl super::EventWriter<Provider> for Provider {
);

for f in fields {
<&mut EventBuilder as AddFieldAndValue<EventBuilder>>::add_field_value(
<&mut EventBuilder as AddFieldAndValue>::add_field_value(
&mut eb.deref_mut(),
&FieldAndValue {
field_name: f.field,
Expand Down Expand Up @@ -319,7 +322,7 @@ impl super::EventWriter<Provider> for Provider {
0,
);

event.record(&mut VisitorWrapper::from(eb.deref_mut()));
event.record(&mut EventBuilderVisitorWrapper::from(eb.deref_mut()));

let act = tracelogging_dynamic::Guid::from_bytes_le(&activity_id);
let related = tracelogging_dynamic::Guid::from_bytes_le(&related_activity_id);
Expand Down
4 changes: 2 additions & 2 deletions src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub trait EventWriter<Mode: ProviderTypes> {
timestamp: std::time::SystemTime,
activity_id: &[u8; 16],
related_activity_id: &[u8; 16],
fields: &'b [crate::values::FieldValueIndex],
fields: &'b [crate::values::span_values::FieldValueIndex],
level: &tracing_core::Level,
keyword: u64,
event_tag: u32,
Expand All @@ -137,7 +137,7 @@ pub trait EventWriter<Mode: ProviderTypes> {
start_stop_times: (std::time::SystemTime, std::time::SystemTime),
activity_id: &[u8; 16],
related_activity_id: &[u8; 16],
fields: &'b [crate::values::FieldValueIndex],
fields: &'b [crate::values::span_values::FieldValueIndex],
level: &tracing_core::Level,
keyword: u64,
event_tag: u32,
Expand Down
4 changes: 2 additions & 2 deletions src/native/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl crate::native::EventWriter<Provider> for Provider {
_timestamp: SystemTime,
_activity_id: &[u8; 16],
_related_activity_id: &[u8; 16],
_fields: &'b [crate::values::FieldValueIndex],
_fields: &'b [crate::values::span_values::FieldValueIndex],
_level: &tracing_core::Level,
_keyword: u64,
_event_tag: u32,
Expand All @@ -60,7 +60,7 @@ impl crate::native::EventWriter<Provider> for Provider {
_start_stop_times: (std::time::SystemTime, std::time::SystemTime),
_activity_id: &[u8; 16],
_related_activity_id: &[u8; 16],
_fields: &'b [crate::values::FieldValueIndex],
_fields: &'b [crate::values::span_values::FieldValueIndex],
_level: &tracing_core::Level,
_keyword: u64,
_event_tag: u32,
Expand Down
14 changes: 7 additions & 7 deletions src/native/user_events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::EtwError;
use crate::statics::GLOBAL_ACTIVITY_SEED;
use crate::values::*;
use crate::values::{event_values::*, *};
use eventheader::*;
use eventheader_dynamic::EventBuilder;
use std::{cell::RefCell, ops::DerefMut, pin::Pin, sync::Arc, time::SystemTime};
Expand All @@ -20,7 +20,7 @@ static mut ETW_META_PTR: *const crate::_details::EventMetadata = core::ptr::null

thread_local! {static EBW: std::cell::RefCell<EventBuilder> = RefCell::new(EventBuilder::new());}

impl<T> AddFieldAndValue<T> for &'_ mut eventheader_dynamic::EventBuilder {
impl AddFieldAndValue for &'_ mut eventheader_dynamic::EventBuilder {
fn add_field_value(&mut self, fv: &FieldAndValue) {
match fv.value {
ValueTypes::None => (),
Expand Down Expand Up @@ -175,7 +175,7 @@ impl crate::native::EventWriter<Provider> for Provider {
timestamp: SystemTime,
activity_id: &[u8; 16],
related_activity_id: &[u8; 16],
fields: &'b [crate::values::FieldValueIndex],
fields: &'b [crate::values::span_values::FieldValueIndex],
level: &tracing_core::Level,
keyword: u64,
event_tag: u32,
Expand Down Expand Up @@ -207,7 +207,7 @@ impl crate::native::EventWriter<Provider> for Provider {
);

for f in fields {
<&mut EventBuilder as AddFieldAndValue<EventBuilder>>::add_field_value(
<&mut EventBuilder as AddFieldAndValue>::add_field_value(
&mut eb.deref_mut(),
&FieldAndValue {
field_name: f.field,
Expand Down Expand Up @@ -238,7 +238,7 @@ impl crate::native::EventWriter<Provider> for Provider {
start_stop_times: (std::time::SystemTime, std::time::SystemTime),
activity_id: &[u8; 16],
related_activity_id: &[u8; 16],
fields: &'b [crate::values::FieldValueIndex],
fields: &'b [crate::values::span_values::FieldValueIndex],
level: &tracing_core::Level,
keyword: u64,
event_tag: u32,
Expand Down Expand Up @@ -271,7 +271,7 @@ impl crate::native::EventWriter<Provider> for Provider {
);

for f in fields {
<&mut EventBuilder as AddFieldAndValue<EventBuilder>>::add_field_value(
<&mut EventBuilder as AddFieldAndValue>::add_field_value(
&mut eb.deref_mut(),
&FieldAndValue {
field_name: f.field,
Expand Down Expand Up @@ -347,7 +347,7 @@ impl crate::native::EventWriter<Provider> for Provider {
0,
);

let mut visitor = VisitorWrapper::from(eb.deref_mut());
let mut visitor = EventBuilderVisitorWrapper::from(eb.deref_mut());
event.record(&mut visitor);

let _ = eb.write(
Expand Down
Loading

0 comments on commit 62a5bcb

Please sign in to comment.