diff --git a/tracing-tracy/src/lib.rs b/tracing-tracy/src/lib.rs index b69568a..6431a4f 100644 --- a/tracing-tracy/src/lib.rs +++ b/tracing-tracy/src/lib.rs @@ -121,8 +121,7 @@ impl TracyLayer { error_msg: &'static str, ) -> &'d str { // From AllocSourceLocation - let mut max_len = - usize::from(u16::max_value()) - 2 - 4 - 4 - function.len() - 1 - file.len() - 1; + let mut max_len = usize::from(u16::MAX) - 2 - 4 - 4 - function.len() - 1 - file.len() - 1; if data.len() >= max_len { while !data.is_char_boundary(max_len) { max_len -= 1; @@ -148,64 +147,93 @@ where F: for<'writer> FormatFields<'writer> + 'static, { fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { - if let Some(span) = ctx.span(id) { - let mut extensions = span.extensions_mut(); - if extensions.get_mut::>().is_none() { - let mut fields = FormattedFields::::new(String::with_capacity(64)); - if self.fmt.format_fields(fields.as_writer(), attrs).is_ok() { - extensions.insert(fields); - } + let Some(span) = ctx.span(id) else { + return; + }; + + let mut extensions = span.extensions_mut(); + if extensions.get_mut::>().is_none() { + let mut fields = FormattedFields::::new(String::with_capacity(64)); + if self.fmt.format_fields(fields.as_writer(), attrs).is_ok() { + extensions.insert(fields); } } } fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) { - if let Some(span) = ctx.span(id) { - let mut extensions = span.extensions_mut(); - if let Some(fields) = extensions.get_mut::>() { - let _ = self.fmt.add_fields(fields, values); - } else { - let mut fields = FormattedFields::::new(String::with_capacity(64)); - if self.fmt.format_fields(fields.as_writer(), values).is_ok() { - extensions.insert(fields); - } + let Some(span) = ctx.span(id) else { + return; + }; + + let mut extensions = span.extensions_mut(); + if let Some(fields) = extensions.get_mut::>() { + let _ = self.fmt.add_fields(fields, values); + } else { + let mut fields = FormattedFields::::new(String::with_capacity(64)); + if self.fmt.format_fields(fields.as_writer(), values).is_ok() { + extensions.insert(fields); } } } + fn on_event(&self, event: &Event, _: Context<'_, S>) { + let mut visitor = TracyEventFieldVisitor { + dest: String::with_capacity(64), + first: true, + frame_mark: false, + }; + event.record(&mut visitor); + if !visitor.first { + self.client.message( + self.truncate_to_length( + &visitor.dest, + "", + "", + "event message is too long and was truncated", + ), + self.stack_depth, + ); + } + if visitor.frame_mark { + self.client.frame_mark(); + } + } + 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: Cow = - if let Some(fields) = span_data.extensions().get::>() { - if fields.fields.as_str().is_empty() { - metadata.name().into() - } else { - format!("{}{{{}}}", metadata.name(), fields.fields.as_str()).into() - } - } else { + let Some(span_data) = ctx.span(id) else { + return; + }; + + let metadata = span_data.metadata(); + let file = metadata.file().unwrap_or(""); + let line = metadata.line().unwrap_or(0); + let name: Cow = + if let Some(fields) = span_data.extensions().get::>() { + if fields.fields.is_empty() { metadata.name().into() - }; - TRACY_SPAN_STACK.with(|s| { - s.borrow_mut().push_back(( - self.client.clone().span_alloc( - Some(self.truncate_to_length( - &name, - file, - "", - "span information is too long and was truncated", - )), - "", + } else { + format!("{}{{{}}}", metadata.name(), fields.fields.as_str()).into() + } + } else { + metadata.name().into() + }; + TRACY_SPAN_STACK.with(|s| { + s.borrow_mut().push_back(( + self.client.clone().span_alloc( + Some(self.truncate_to_length( + &name, file, - line, - self.stack_depth, - ), - id.into_u64(), - )); - }); - } + "", + "span information is too long and was truncated", + )), + "", + file, + line, + self.stack_depth, + ), + id.into_u64(), + )); + }); } fn on_exit(&self, id: &Id, _: Context) { @@ -229,29 +257,6 @@ where } }); } - - fn on_event(&self, event: &Event, _: Context<'_, S>) { - let mut visitor = TracyEventFieldVisitor { - dest: String::with_capacity(64), - first: true, - frame_mark: false, - }; - event.record(&mut visitor); - if !visitor.first { - self.client.message( - self.truncate_to_length( - &visitor.dest, - "", - "", - "event message is too long and was truncated", - ), - self.stack_depth, - ); - } - if visitor.frame_mark { - self.client.frame_mark(); - } - } } struct TracyEventFieldVisitor { @@ -261,6 +266,13 @@ struct TracyEventFieldVisitor { } impl Visit for TracyEventFieldVisitor { + fn record_bool(&mut self, field: &Field, value: bool) { + match (value, field.name()) { + (true, "tracy.frame_mark") => self.frame_mark = true, + _ => self.record_debug(field, &value), + } + } + fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) { // FIXME: this is a very crude formatter, but we don’t have // an easy way to do anything better... @@ -271,13 +283,6 @@ impl Visit for TracyEventFieldVisitor { let _ = write!(&mut self.dest, ", {} = {:?}", field.name(), value); } } - - fn record_bool(&mut self, field: &Field, value: bool) { - match (value, field.name()) { - (true, "tracy.frame_mark") => self.frame_mark = true, - _ => self.record_debug(field, &value), - } - } } #[cfg(test)] diff --git a/tracy-client/src/lib.rs b/tracy-client/src/lib.rs index 293449e..ba0079d 100644 --- a/tracy-client/src/lib.rs +++ b/tracy-client/src/lib.rs @@ -57,12 +57,12 @@ pub mod internal { span_name: *const u8, file: *const u8, line: u32, - ) -> crate::SpanLocation { + ) -> SpanLocation { #[cfg(feature = "enable")] { let function_name = CString::new(&type_name[..type_name.len() - 3]).unwrap(); - crate::SpanLocation { - data: crate::sys::___tracy_source_location_data { + SpanLocation { + data: sys::___tracy_source_location_data { name: span_name.cast(), function: function_name.as_ptr(), file: file.cast(),