use crate::{adjust_stack_depth, Client};
use std::ffi::CString;
pub struct Span {
#[cfg(feature = "enable")]
client: Client,
#[cfg(feature = "enable")]
zone: sys::___tracy_c_zone_context,
#[cfg(feature = "enable")]
_no_send_sync: std::marker::PhantomData<*mut sys::___tracy_c_zone_context>,
#[cfg(not(feature = "enable"))]
_no_send_sync: std::marker::PhantomData<*mut ()>,
}
pub struct SpanLocation {
#[cfg(feature = "enable")]
pub(crate) _function_name: CString,
#[cfg(feature = "enable")]
pub(crate) data: sys::___tracy_source_location_data,
#[cfg(not(feature = "enable"))]
pub(crate) _internal: (),
}
unsafe impl Send for SpanLocation {}
unsafe impl Sync for SpanLocation {}
impl Client {
#[inline]
pub fn span(self, loc: &'static SpanLocation, callstack_depth: u16) -> Span {
#[cfg(feature = "enable")]
unsafe {
let zone = if callstack_depth == 0 {
sys::___tracy_emit_zone_begin(&loc.data, 1)
} else {
let stack_depth = adjust_stack_depth(callstack_depth).into();
sys::___tracy_emit_zone_begin_callstack(&loc.data, stack_depth, 1)
};
Span {
client: self,
zone,
_no_send_sync: std::marker::PhantomData,
}
}
#[cfg(not(feature = "enable"))]
Span {
_no_send_sync: std::marker::PhantomData,
}
}
#[inline]
pub fn span_alloc(
self,
name: &str,
function: &str,
file: &str,
line: u32,
callstack_depth: u16,
) -> Span {
#[cfg(feature = "enable")]
unsafe {
let loc = sys::___tracy_alloc_srcloc_name(
line,
file.as_ptr().cast(),
file.len(),
function.as_ptr().cast(),
function.len(),
name.as_ptr().cast(),
name.len(),
);
let zone = if callstack_depth == 0 {
sys::___tracy_emit_zone_begin_alloc(loc, 1)
} else {
let stack_depth = adjust_stack_depth(callstack_depth).into();
sys::___tracy_emit_zone_begin_alloc_callstack(loc, stack_depth, 1)
};
Span {
client: self,
zone,
_no_send_sync: std::marker::PhantomData,
}
}
#[cfg(not(feature = "enable"))]
Span {
_no_send_sync: std::marker::PhantomData,
}
}
}
impl Span {
pub fn emit_value(&self, value: u64) {
#[cfg(feature = "enable")]
unsafe {
sys::___tracy_emit_zone_value(self.zone, value);
}
}
pub fn emit_text(&self, text: &str) {
#[cfg(feature = "enable")]
unsafe {
sys::___tracy_emit_zone_text(self.zone, text.as_ptr().cast(), text.len());
}
}
pub fn emit_color(&self, color: u32) {
#[cfg(feature = "enable")]
unsafe {
sys::___tracy_emit_zone_color(self.zone, color);
}
}
}
impl Drop for Span {
fn drop(&mut self) {
#[cfg(feature = "enable")]
unsafe {
sys::___tracy_emit_zone_end(self.zone);
std::convert::identity(&self.client);
}
}
}
#[macro_export]
macro_rules! span_location {
($name: literal) => {{
struct S;
static LOC: $crate::internal::Lazy<$crate::internal::SpanLocation> =
$crate::internal::Lazy::new(|| {
$crate::internal::make_span_location(
$crate::internal::type_name::<S>(),
concat!($name, "\0").as_ptr(),
concat!(file!(), "\0").as_ptr(),
line!(),
)
});
&*LOC
}};
}
#[macro_export]
macro_rules! span {
($name:literal) => {
$crate::span!($name, 62)
};
($name:literal, $callstack_depth:expr) => {{
let location = $crate::span_location!($name);
$crate::Client::running()
.expect("span! without a running Client")
.span(location, $callstack_depth)
}};
}