Skip to content

Commit f763d36

Browse files
committed
docs(common): add a comment explaining how to use the timer! macro
1 parent 91d085c commit f763d36

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

crates/matrix-sdk-common/src/tracing_timer.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use ruma::time::Instant;
1616
use tracing::{callsite::DefaultCallsite, Callsite as _};
1717

18-
/// A named RAII that will show on Drop how long its covered section took to
18+
/// A named RAII that will show on `Drop` how long its covered section took to
1919
/// execute.
2020
pub struct TracingTimer {
2121
id: String,
@@ -72,11 +72,29 @@ impl TracingTimer {
7272
}
7373
}
7474

75-
/// Macro to create a RAII timer that will log a `tracing` event once it's
76-
/// dropped.
75+
/// Macro to create a RAII timer that will log on `Drop` how long its covered
76+
/// section took to execute.
7777
///
7878
/// The tracing level can be specified as a first argument, but it's optional.
7979
/// If it's missing, this will use the debug level.
80+
///
81+
/// ```rust,no_run
82+
/// # fn do_long_computation(_x: u32) {}
83+
/// # fn main() {
84+
/// use matrix_sdk_common::timer;
85+
///
86+
/// // It's possible to specify the tracing level we want to be used for the log message on drop.
87+
/// {
88+
/// let _timer = timer!(tracing::Level::TRACE, "do long computation");
89+
/// // But it's optional; by default it's set to `DEBUG`.
90+
/// let _debug_timer = timer!("do long computation but time it in DEBUG");
91+
/// // The macro doesn't support formatting / structured events (yet?), but you can use
92+
/// // `format!()` for that.
93+
/// let other_timer = timer!(format!("do long computation for parameter = {}", 123));
94+
/// do_long_computation(123);
95+
/// } // The log statements will happen here.
96+
/// # }
97+
/// ```
8098
#[macro_export]
8199
macro_rules! timer {
82100
($level:expr, $string:expr) => {{

0 commit comments

Comments
 (0)