mysql_async/
tracing_utils.rs

1/// Compile-time tracing level.
2pub trait TracingLevel: Send + Sync + 'static {
3    #[cfg(feature = "tracing")]
4    const LEVEL: tracing::Level;
5}
6
7/// INFO tracing level.
8pub struct LevelInfo;
9
10impl TracingLevel for LevelInfo {
11    #[cfg(feature = "tracing")]
12    const LEVEL: tracing::Level = tracing::Level::INFO;
13}
14
15/// TRACE tracing level.
16pub struct LevelTrace;
17
18impl TracingLevel for LevelTrace {
19    #[cfg(feature = "tracing")]
20    const LEVEL: tracing::Level = tracing::Level::TRACE;
21}
22
23#[cfg(feature = "tracing")]
24macro_rules! create_span {
25    ($s:expr, $($field:tt)*) => {
26        if $s == tracing::Level::TRACE {
27            tracing::trace_span!($($field)*)
28        } else if $s == tracing::Level::DEBUG {
29            tracing::debug_span!($($field)*)
30        } else if $s == tracing::Level::INFO {
31            tracing::info_span!($($field)*)
32        } else if $s == tracing::Level::WARN {
33            tracing::warn_span!($($field)*)
34        } else if $s == tracing::Level::ERROR {
35            tracing::error_span!($($field)*)
36        } else {
37            unreachable!();
38        }
39    }
40}
41
42#[cfg(feature = "tracing")]
43macro_rules! instrument_result {
44    ($fut:expr, $span:expr) => {{
45        let fut = async {
46            $fut.await.or_else(|e| {
47                tracing::error!(error = %e);
48                Err(e)
49            })
50        };
51        <_ as tracing::Instrument>::instrument(fut, $span)
52    }};
53}