diff --git a/tracing-tracy/Cargo.toml b/tracing-tracy/Cargo.toml index 0f29cef..3962990 100644 --- a/tracing-tracy/Cargo.toml +++ b/tracing-tracy/Cargo.toml @@ -14,7 +14,7 @@ Inspect tracing-enabled Rust applications with Tracy [dependencies] tracing-core = { version = "0.1", default-features = false, features = ["std"] } -tracing-subscriber = { version = "0.2", default-features = false, features = ["registry"] } +tracing-subscriber = { version = "0.2", default-features = false, features = ["fmt", "registry"] } tracy-client = { path = "../tracy-client", version = "0.9.0", default-features = false } [dev-dependencies] diff --git a/tracing-tracy/src/lib.rs b/tracing-tracy/src/lib.rs index 52fa412..a44f3d0 100644 --- a/tracing-tracy/src/lib.rs +++ b/tracing-tracy/src/lib.rs @@ -38,7 +38,9 @@ use tracing_core::{ span::Id, Event, Subscriber, }; +use tracing_subscriber::fmt::format::{DefaultFields, FormatFields}; use tracing_subscriber::{ + fmt::FormattedFields, layer::{Context, Layer}, registry, }; @@ -53,7 +55,8 @@ thread_local! { /// A tracing layer that collects data in Tracy profiling format. #[derive(Clone)] -pub struct TracyLayer { +pub struct TracyLayer { + format: F, stack_depth: u16, } @@ -62,7 +65,7 @@ impl TracyLayer { /// /// Defaults to collecting stack traces. pub fn new() -> Self { - Self { stack_depth: 64 } + Self { format: DefaultFields::new(), stack_depth: 64 } } /// Specify the maximum number of stack frames that will be collected. @@ -80,18 +83,24 @@ impl Default for TracyLayer { } } -impl Layer for TracyLayer +impl Layer for TracyLayer where S: Subscriber + for<'a> registry::LookupSpan<'a>, + F: for<'writer> FormatFields<'writer> + 'static, { fn on_enter(&self, id: &Id, ctx: Context) { if let Some(span_data) = ctx.span(id) { let metadata = span_data.metadata(); let file = metadata.file().unwrap_or(""); let line = metadata.line().unwrap_or(0); + let name = if let Some(fields) = span_data.extensions().get::>() { + format!("{}: {}", metadata.name(), fields.fields.as_str()) + } else { + metadata.name().to_string() + }; TRACY_SPAN_STACK.with(|s| { s.borrow_mut().push_back(( - Span::new(metadata.name(), "", file, line, self.stack_depth), + Span::new(&name, "", file, line, self.stack_depth), id.into_u64() )); });