Skip to content

Commit

Permalink
config: enable customization of behaviour upon an error condition
Browse files Browse the repository at this point in the history
  • Loading branch information
nagisa committed Jan 6, 2024
1 parent e0b5e0a commit 300b61d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
20 changes: 20 additions & 0 deletions tracing-tracy/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ pub trait Config {
fn format_fields_in_zone_name(&self) -> bool {
true
}

/// Apply handling for errors detected by the [`TracyLayer`].
///
/// Fundamentally the way the tracing crate and the Tracy profiler work are somewhat
/// incompatible in certain ways. For instance, a [`tracing::Span`] can be created on one
/// thread and moved to another, where it is cleaned up. Tracy on the other hand expects that
/// its eqvivalent concept of zone remains entirely within a thread.
///
/// Another example a limitation in `Tracy` where the message length or zone name cannot exceed
/// a certain (low) limit of bytes.
///
/// Although `tracing_tracy` does it best to paper over these sorts of differences, it can’t
/// always make them invisible. In certain cases detecting these sorts of issues is
/// straightforward, and it is when `tracing_tracy` will invoke this method to enable users to
/// report the issues in whatever way they wish to.
///
/// By default a message coloured in red is emitted to the tracy client.
fn on_error(&self, client: &Client, error: &'static str) {
client.color_message(error, 0xFF000000, self.stack_depth());
}
}

/// A type that implements the [`Config`] trait with runtime-adjustable values.
Expand Down
15 changes: 6 additions & 9 deletions tracing-tracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ impl<C: Config> TracyLayer<C> {
while !data.is_char_boundary(max_len) {
max_len -= 1;
}
self.client
.color_message(error_msg, 0xFF000000, self.config.stack_depth());
self.config.on_error(&self.client, error_msg);
&data[..max_len]
} else {
data
Expand Down Expand Up @@ -298,19 +297,17 @@ where

if let Some((span, span_id)) = stack_frame {
if id.into_u64() != span_id {
self.client.color_message(
self.config.on_error(
&self.client,
"Tracing spans exited out of order! \
Trace may not be accurate for this span stack.",
0xFF000000,
self.config.stack_depth(),
Trace might not be accurate for this span stack.",
);
}
drop(span);
} else {
self.client.color_message(
self.config.on_error(
&self.client,
"Exiting a tracing span, but got nothing on the tracy span stack!",
0xFF000000,
self.config.stack_depth(),
);
}
}
Expand Down

0 comments on commit 300b61d

Please sign in to comment.