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
// Copyright 2020 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.

use fuchsia_trace as trace;
use std::{future::poll_fn, task::Poll};

#[no_mangle]
pub extern "C" fn rs_test_trace_enabled() -> bool {
    return trace::is_enabled();
}

#[no_mangle]
pub extern "C" fn rs_test_category_disabled() -> bool {
    return trace::category_enabled(c"-disabled");
}

#[no_mangle]
pub extern "C" fn rs_test_category_enabled() -> bool {
    return trace::category_enabled(c"+enabled");
}

#[no_mangle]
pub extern "C" fn rs_test_counter_macro() {
    trace::counter!(c"+enabled", c"name", 42, "arg" => 10);
}

#[no_mangle]
pub extern "C" fn rs_test_instant_macro() {
    trace::instant!(c"+enabled", c"name", trace::Scope::Process, "arg" => 10);
}

#[no_mangle]
pub extern "C" fn rs_test_duration_macro() {
    trace::duration!(c"+enabled", c"name", "x" => 5, "y" => 10);
}

#[no_mangle]
pub extern "C" fn rs_test_duration_macro_with_scope() {
    // N.B. The ordering here is intentional. The duration! macro emits a trace
    // event when the scoped object is dropped. From an output perspective,
    // that means we are looking to see that the instant event occurs first.
    trace::duration!(c"+enabled", c"name", "x" => 5, "y" => 10);
    trace::instant!(c"+enabled", c"name", trace::Scope::Process, "arg" => 10);
}

#[no_mangle]
pub extern "C" fn rs_test_duration_begin_end_macros() {
    trace::duration_begin!(c"+enabled", c"name", "x" => 5);
    trace::instant!(c"+enabled", c"name", trace::Scope::Process, "arg" => 10);
    trace::duration_end!(c"+enabled", c"name", "y" => "foo");
}

#[no_mangle]
pub extern "C" fn rs_test_blob_macro() {
    trace::blob!(c"+enabled", c"name", "blob contents".as_bytes().to_vec().as_slice(), "x" => 5);
}

#[no_mangle]
pub extern "C" fn rs_test_flow_begin_step_end_macros() {
    trace::flow_begin!(c"+enabled", c"name", 123.into(), "x" => 5);
    trace::flow_step!(c"+enabled", c"step", 123.into(), "z" => 42);
    trace::flow_end!(c"+enabled", c"name", 123.into(), "y" => "foo");
}

#[no_mangle]
pub extern "C" fn rs_test_arglimit() {
    trace::duration!(c"+enabled", c"name",
        "1" => 1,
        "2" => 2,
        "3" => 3,
        "4" => 4,
        "5" => 5,
        "6" => 6,
        "7" => 7,
        "8" => 8,
        "9" => 9,
        "10" => 10,
        "11" => 11,
        "12" => 12,
        "13" => 13,
        "14" => 14,
        "15" => 15
    );
}

#[no_mangle]
pub extern "C" fn rs_test_async_event_with_scope() {
    // N.B. The ordering here is intentional. The async_enter! macro emits a trace event when the
    // scoped object is instantiated and when it is dropped. From an output perspective, that means
    // we are looking to see that the instant event occurs sandwiched between the two.
    let _guard = trace::async_enter!(1.into(), c"+enabled", c"name", "x" => 5, "y" => 10);
    trace::instant!(c"+enabled", c"name", trace::Scope::Process, "arg" => 10);
}

#[no_mangle]
pub extern "C" fn rs_test_alert() {
    trace::alert!(c"+enabled", c"alert_name");
}

fn trace_future_test(args: trace::TraceFutureArgs<'_>) {
    let mut executor = fuchsia_async::TestExecutor::new();
    let mut polled = false;
    executor.run_singlethreaded(trace::TraceFuture::new(
        args,
        poll_fn(move |cx| {
            if !polled {
                polled = true;
                cx.waker().clone().wake();
                Poll::Pending
            } else {
                Poll::Ready(())
            }
        }),
    ))
}

#[no_mangle]
pub extern "C" fn rs_test_trace_future_enabled() {
    trace_future_test(trace::trace_future_args!(c"+enabled", c"name", 3.into()));
}

#[no_mangle]
pub extern "C" fn rs_test_trace_future_enabled_with_arg() {
    trace_future_test(trace::trace_future_args!(c"+enabled", c"name", 3.into(), "arg" => 10));
}

#[no_mangle]
pub extern "C" fn rs_test_trace_future_disabled() {
    trace_future_test(trace::trace_future_args!(c"-disabled", c"name", 3.into()));
}

#[no_mangle]
pub extern "C" fn rs_test_trace_future_disabled_with_arg() {
    #[allow(unreachable_code)]
    trace_future_test(trace::trace_future_args!(
        c"-disabled",
        c"name",
        3.into(),
        "arg" => panic!("arg should not be evaluated")
    ));
}