Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored and nagisa committed Dec 27, 2023
1 parent 76ffff2 commit 1618816
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 81 deletions.
161 changes: 83 additions & 78 deletions tracing-tracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ impl<F> TracyLayer<F> {
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;
Expand All @@ -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::<FormattedFields<F>>().is_none() {
let mut fields = FormattedFields::<F>::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::<FormattedFields<F>>().is_none() {
let mut fields = FormattedFields::<F>::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::<FormattedFields<F>>() {
let _ = self.fmt.add_fields(fields, values);
} else {
let mut fields = FormattedFields::<F>::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::<FormattedFields<F>>() {
let _ = self.fmt.add_fields(fields, values);
} else {
let mut fields = FormattedFields::<F>::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<S>) {
if let Some(span_data) = ctx.span(id) {
let metadata = span_data.metadata();
let file = metadata.file().unwrap_or("<not available>");
let line = metadata.line().unwrap_or(0);
let name: Cow<str> =
if let Some(fields) = span_data.extensions().get::<FormattedFields<F>>() {
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("<not available>");
let line = metadata.line().unwrap_or(0);
let name: Cow<str> =
if let Some(fields) = span_data.extensions().get::<FormattedFields<F>>() {
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<S>) {
Expand All @@ -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 {
Expand All @@ -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...
Expand All @@ -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)]
Expand Down
6 changes: 3 additions & 3 deletions tracy-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 1618816

Please sign in to comment.