1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#[cfg(feature = "tracing")]
mod fuchsia;
#[cfg(feature = "tracing")]
pub mod __backend {
    pub use crate::fuchsia::*;
}

#[cfg(not(feature = "tracing"))]
mod noop;
#[cfg(not(feature = "tracing"))]
pub mod __backend {
    pub use crate::noop::*;
}

pub use __backend::{Id, TraceFutureExt};

/// Convenience macro for creating a trace duration event from this macro invocation to the end of
/// the current scope.
///
/// See `fuchsia_trace::duration!` for more details.
#[macro_export]
macro_rules! duration {
    ($category:expr, $name:expr $(, $key:expr => $val:expr)*) => {
        let args;
        let _scope = {
            static CACHE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
            if $crate::__backend::TraceCategoryContext::acquire_cached($category, &CACHE)
                .is_some()
            {
                args = [$($crate::__backend::ArgValue::of($key, $val)),*];
                Some($crate::__backend::duration($category, $name, &args))
            } else {
                None
            }
        };
    }
}

/// Writes a flow begin event with the specified id.
///
/// See `fuchsia_trace::flow_begin!` for more details.
#[macro_export]
macro_rules! flow_begin {
    ($category:expr, $name:expr, $flow_id:expr $(, $key:expr => $val:expr)*) => {{
        static CACHE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        if let Some(context) =
            $crate::__backend::TraceCategoryContext::acquire_cached($category, &CACHE)
        {
            $crate::__backend::flow_begin(&context, $name, ($flow_id).into(),
                                          &[$($crate::__backend::ArgValue::of($key, $val)),*])
        }
    }}
}

/// Writes a flow step event with the specified id.
///
/// See `fuchsia_trace::flow_step!` for more details.
#[macro_export]
macro_rules! flow_step {
    ($category:expr, $name:expr, $flow_id:expr $(, $key:expr => $val:expr)*) => {{
        static CACHE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        if let Some(context) =
            $crate::__backend::TraceCategoryContext::acquire_cached($category, &CACHE)
        {
            $crate::__backend::flow_step(&context, $name, ($flow_id).into(),
                                         &[$($crate::__backend::ArgValue::of($key, $val)),*])
        }
    }}
}

/// Writes a flow end event with the specified id.
///
/// See `fuchsia_trace::flow_end!` for more details.
#[macro_export]
macro_rules! flow_end {
    ($category:expr, $name:expr, $flow_id:expr $(, $key:expr => $val:expr)*) => {{
        static CACHE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        if let Some(context) =
            $crate::__backend::TraceCategoryContext::acquire_cached($category, &CACHE)
        {
            $crate::__backend::flow_end(&context, $name, ($flow_id).into(),
                                        &[$($crate::__backend::ArgValue::of($key, $val)),*])
        }
    }}
}

/// Constructs a `TraceFutureArgs` object to be passed to `TraceFutureExt::trace`.
///
/// See `fuchsia_trace::trace_future_args!` for more details.
#[macro_export]
macro_rules! trace_future_args {
    ($category:expr, $name:expr $(, $key:expr => $val:expr)*) => {{
        static CACHE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let context = $crate::__backend::TraceCategoryContext::acquire_cached($category, &CACHE);
        let args = if context.is_some() {
            vec![$($crate::__backend::ArgValue::of($key, $val)),*]
        } else {
            vec![]
        };
        $crate::__backend::trace_future_args(context, $category, $name, args)
    }}
}

#[cfg(test)]
mod tests {
    use crate::TraceFutureExt;

    #[fuchsia::test]
    fn test_duration() {
        let trace_only_var = 6;
        duration!(c"category", c"name");
        duration!(c"category", c"name", "arg" => 5);
        duration!(c"category", c"name", "arg" => 5, "arg2" => trace_only_var);
    }

    #[fuchsia::test]
    fn test_flow_begin() {
        let trace_only_var = 6;
        let flow_id = 5u64;
        flow_begin!(c"category", c"name", flow_id);
        flow_begin!(c"category", c"name", flow_id, "arg" => 5);
        flow_begin!(c"category", c"name", flow_id, "arg" => 5, "arg2" => trace_only_var);
    }

    #[fuchsia::test]
    fn test_flow_step() {
        let trace_only_var = 6;
        let flow_id = 5u64;
        flow_step!(c"category", c"name", flow_id);
        flow_step!(c"category", c"name", flow_id, "arg" => 5);
        flow_step!(c"category", c"name", flow_id, "arg" => 5, "arg2" => trace_only_var);
    }

    #[fuchsia::test]
    fn test_flow_end() {
        let trace_only_var = 6;
        let flow_id = 5u64;
        flow_end!(c"category", c"name", flow_id);
        flow_end!(c"category", c"name", flow_id, "arg" => 5);
        flow_end!(c"category", c"name", flow_id, "arg" => 5, "arg2" => trace_only_var);
    }

    #[fuchsia::test]
    async fn test_trace_future() {
        let value = async move { 5 }.trace(trace_future_args!(c"category", c"name")).await;
        assert_eq!(value, 5);

        let value =
            async move { 5 }.trace(trace_future_args!(c"category", c"name", "arg1" => 6)).await;
        assert_eq!(value, 5);

        let trace_only_var = 7;
        let value = async move { 5 }
            .trace(trace_future_args!(c"category", c"name", "arg1" => 6, "ar2" => trace_only_var))
            .await;
        assert_eq!(value, 5);
    }

    #[fuchsia::test]
    fn test_arg_types() {
        duration!(c"category", c"name", "bool" => true);
        duration!(c"category", c"name", "i32" => 5i32, "u32" => 5u32);
        duration!(c"category", c"name", "i64" => 5i64, "u64" => 5u64);
        duration!(c"category", c"name", "isize" => 5isize, "usize" => 5usize);
        duration!(c"category", c"name", "f64" => 5f64);

        let owned_str = "test-str".to_owned();
        duration!(c"category", c"name", "str" => owned_str.as_str());

        let mut value = 5u64;
        duration!(c"category", c"name", "const-ptr" => &value as *const u64);
        duration!(c"category", c"name", "mut-ptr" => &mut value as *mut u64);
    }
}