_rust_cpp_test_lib_rustc_static/
lib.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use fuchsia_trace as trace;
6use std::future::poll_fn;
7use std::task::Poll;
8
9#[no_mangle]
10pub extern "C" fn rs_test_trace_enabled() -> bool {
11    return trace::is_enabled();
12}
13
14#[no_mangle]
15pub extern "C" fn rs_test_category_disabled() -> bool {
16    return trace::category_enabled(c"-disabled");
17}
18
19#[no_mangle]
20pub extern "C" fn rs_test_category_enabled() -> bool {
21    return trace::category_enabled(c"+enabled");
22}
23
24#[no_mangle]
25pub extern "C" fn rs_test_counter_macro() {
26    trace::counter!(c"+enabled", c"name", 42, "arg" => 10);
27}
28
29#[no_mangle]
30pub extern "C" fn rs_test_instant_macro() {
31    trace::instant!(c"+enabled", c"name", trace::Scope::Process, "arg" => 10);
32}
33
34#[no_mangle]
35pub extern "C" fn rs_test_duration_macro() {
36    trace::duration!(c"+enabled", c"name", "x" => 5, "y" => 10);
37}
38
39#[no_mangle]
40pub extern "C" fn rs_test_duration_macro_with_scope() {
41    // N.B. The ordering here is intentional. The duration! macro emits a trace
42    // event when the scoped object is dropped. From an output perspective,
43    // that means we are looking to see that the instant event occurs first.
44    trace::duration!(c"+enabled", c"name", "x" => 5, "y" => 10);
45    trace::instant!(c"+enabled", c"name", trace::Scope::Process, "arg" => 10);
46}
47
48#[no_mangle]
49pub extern "C" fn rs_test_duration_begin_end_macros() {
50    trace::duration_begin!(c"+enabled", c"name", "x" => 5);
51    trace::instant!(c"+enabled", c"name", trace::Scope::Process, "arg" => 10);
52    trace::duration_end!(c"+enabled", c"name", "y" => "foo");
53}
54
55#[no_mangle]
56pub extern "C" fn rs_test_blob_macro() {
57    trace::blob!(c"+enabled", c"name", "blob contents".as_bytes().to_vec().as_slice(), "x" => 5);
58}
59
60#[no_mangle]
61pub extern "C" fn rs_test_flow_begin_step_end_macros() {
62    trace::flow_begin!(c"+enabled", c"name", 123.into(), "x" => 5);
63    trace::flow_step!(c"+enabled", c"step", 123.into(), "z" => 42);
64    trace::flow_end!(c"+enabled", c"name", 123.into(), "y" => "foo");
65}
66
67#[no_mangle]
68pub extern "C" fn rs_test_arglimit() {
69    trace::duration!(c"+enabled", c"name",
70        "1" => 1,
71        "2" => 2,
72        "3" => 3,
73        "4" => 4,
74        "5" => 5,
75        "6" => 6,
76        "7" => 7,
77        "8" => 8,
78        "9" => 9,
79        "10" => 10,
80        "11" => 11,
81        "12" => 12,
82        "13" => 13,
83        "14" => 14,
84        "15" => 15
85    );
86}
87
88#[no_mangle]
89pub extern "C" fn rs_test_async_event_with_scope() {
90    // N.B. The ordering here is intentional. The async_enter! macro emits a trace event when the
91    // scoped object is instantiated and when it is dropped. From an output perspective, that means
92    // we are looking to see that the instant event occurs sandwiched between the two.
93    let _guard = trace::async_enter!(1.into(), c"+enabled", c"name", "x" => 5, "y" => 10);
94    trace::instant!(c"+enabled", c"name", trace::Scope::Process, "arg" => 10);
95}
96
97#[no_mangle]
98pub extern "C" fn rs_test_alert() {
99    trace::alert!(c"+enabled", c"alert_name");
100}
101
102fn trace_future_test(args: trace::TraceFutureArgs<'_>) {
103    let mut executor = fuchsia_async::TestExecutor::new();
104    let mut polled = false;
105    executor.run_singlethreaded(trace::TraceFuture::new(
106        args,
107        poll_fn(move |cx| {
108            if !polled {
109                polled = true;
110                cx.waker().clone().wake();
111                Poll::Pending
112            } else {
113                Poll::Ready(())
114            }
115        }),
116    ))
117}
118
119#[no_mangle]
120pub extern "C" fn rs_test_trace_future_enabled() {
121    trace_future_test(trace::trace_future_args!(c"+enabled", c"name", 3.into()));
122}
123
124#[no_mangle]
125pub extern "C" fn rs_test_trace_future_enabled_with_arg() {
126    trace_future_test(trace::trace_future_args!(c"+enabled", c"name", 3.into(), "arg" => 10));
127}
128
129#[no_mangle]
130pub extern "C" fn rs_test_trace_future_disabled() {
131    trace_future_test(trace::trace_future_args!(c"-disabled", c"name", 3.into()));
132}
133
134#[no_mangle]
135pub extern "C" fn rs_test_trace_future_disabled_with_arg() {
136    #[allow(unreachable_code)]
137    trace_future_test(trace::trace_future_args!(
138        c"-disabled",
139        c"name",
140        3.into(),
141        "arg" => {
142            panic!("arg should not be evaluated");
143            ()
144        }
145    ));
146}