pub struct TracySpan {
#[cfg(feature = "tracy")]
_inner: tracy_client::Span,
#[cfg(not(feature = "tracy"))]
_phantom: (),
}
pub struct TracyClient {
#[cfg(feature = "tracy")]
inner: tracy_client::Client,
active: bool,
}
impl TracyClient {
pub fn new() -> Self {
#[cfg(feature = "tracy")]
{
TracyClient {
inner: tracy_client::Client::start(),
active: true,
}
}
#[cfg(not(feature = "tracy"))]
{
TracyClient { active: false }
}
}
#[inline]
pub fn is_active(&self) -> bool {
self.active
}
#[inline]
pub fn span(&self, name: &str) -> TracySpan {
#[cfg(feature = "tracy")]
{
let span = self.inner.clone().span_alloc(Some(name), name, "", 0, 0);
TracySpan { _inner: span }
}
#[cfg(not(feature = "tracy"))]
{
let _ = name;
TracySpan { _phantom: () }
}
}
#[inline]
pub fn frame_mark(&self, name: &str) {
#[cfg(feature = "tracy")]
{
let frame_name = tracy_client::FrameName::new_leak(name.to_owned());
self.inner.secondary_frame_mark(frame_name);
}
#[cfg(not(feature = "tracy"))]
let _ = name;
}
#[inline]
pub fn message(&self, msg: &str) {
#[cfg(feature = "tracy")]
{
self.inner.message(msg, 0);
}
#[cfg(not(feature = "tracy"))]
let _ = msg;
}
}
impl Default for TracyClient {
fn default() -> Self {
Self::new()
}
}
#[macro_export]
macro_rules! tracy_span {
($client:expr, $name:expr) => {
let _tracy_span_guard = $client.span($name);
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tracy_client_default_features() {
let client = TracyClient::new();
#[cfg(not(feature = "tracy"))]
assert!(
!client.is_active(),
"TracyClient should be inactive without the tracy feature"
);
#[cfg(feature = "tracy")]
assert!(
client.is_active(),
"TracyClient should be active with the tracy feature"
);
}
#[test]
fn test_tracy_span_drop() {
let client = TracyClient::new();
{
let _span = client.span("test_span_drop");
}
}
#[test]
fn test_tracy_frame_mark() {
let client = TracyClient::new();
client.frame_mark("test_frame");
}
#[test]
fn test_tracy_message() {
let client = TracyClient::new();
client.message("test message from tracy integration test");
}
#[test]
fn test_tracy_default_impl() {
let client = TracyClient::default();
#[cfg(not(feature = "tracy"))]
assert!(!client.is_active());
}
#[test]
fn test_tracy_span_macro() {
let client = TracyClient::new();
tracy_span!(client, "macro_test_span");
}
}