mysql_async/
tracing_utils.rs

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
/// Compile-time tracing level.
pub trait TracingLevel: Send + Sync + 'static {
    #[cfg(feature = "tracing")]
    const LEVEL: tracing::Level;
}

/// INFO tracing level.
pub struct LevelInfo;

impl TracingLevel for LevelInfo {
    #[cfg(feature = "tracing")]
    const LEVEL: tracing::Level = tracing::Level::INFO;
}

/// TRACE tracing level.
pub struct LevelTrace;

impl TracingLevel for LevelTrace {
    #[cfg(feature = "tracing")]
    const LEVEL: tracing::Level = tracing::Level::TRACE;
}

#[cfg(feature = "tracing")]
macro_rules! create_span {
    ($s:expr, $($field:tt)*) => {
        if $s == tracing::Level::TRACE {
            tracing::trace_span!($($field)*)
        } else if $s == tracing::Level::DEBUG {
            tracing::debug_span!($($field)*)
        } else if $s == tracing::Level::INFO {
            tracing::info_span!($($field)*)
        } else if $s == tracing::Level::WARN {
            tracing::warn_span!($($field)*)
        } else if $s == tracing::Level::ERROR {
            tracing::error_span!($($field)*)
        } else {
            unreachable!();
        }
    }
}

#[cfg(feature = "tracing")]
macro_rules! instrument_result {
    ($fut:expr, $span:expr) => {{
        let fut = async {
            $fut.await.or_else(|e| {
                tracing::error!(error = %e);
                Err(e)
            })
        };
        <_ as tracing::Instrument>::instrument(fut, $span)
    }};
}