1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! Usage sample for a tracing subscriber in libatrace_rust.
16
17 use tracing::{debug, error, event, info, span, trace, warn, Level};
18
19 use atrace_tracing_subscriber::AtraceSubscriber;
20 use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
21
22 #[tracing::instrument]
mul_by_100_instrumented(num: i32) -> i3223 fn mul_by_100_instrumented(num: i32) -> i32 {
24 let result = num * 100;
25 std::thread::sleep(std::time::Duration::from_millis(300));
26 event!(Level::INFO, num, result);
27 result
28 }
29
events_and_spans_demo()30 fn events_and_spans_demo() {
31 let power_level = 8999;
32
33 event!(Level::INFO, foo = "bar", power_level, "This is a {} message", "formattable");
34 std::thread::sleep(std::time::Duration::from_millis(100));
35
36 let span = span!(Level::TRACE, "Span name", baz = "quux");
37 std::thread::sleep(std::time::Duration::from_millis(300));
38
39 let _span_guard = span.enter();
40
41 let _entered_span = span!(Level::TRACE, "Entered span").entered();
42 std::thread::sleep(std::time::Duration::from_millis(300));
43
44 trace!("test {} log {}", "VERBOSE", mul_by_100_instrumented(42));
45 debug!("test {} log", "DEBUG");
46 info!("test {} log", "INFO");
47 warn!("test {} log", "WARNING");
48 error!("test {} log", "ERROR");
49 }
50
main() -> Result<(), Box<dyn std::error::Error>>51 fn main() -> Result<(), Box<dyn std::error::Error>> {
52 tracing_subscriber::registry()
53 .with(AtraceSubscriber::default().with_filter())
54 .with(tracing_subscriber::fmt::layer())
55 .init();
56
57 events_and_spans_demo();
58
59 Ok(())
60 }
61