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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! Compile time conditional logging.
//!
//! The `cfg_log` crate provides a way to conditionally use the [logging macros] from the
//! `log` crate depending on whether the `log` feature of the main crate is enabled or not.
//!
//!   [logging macros]: https://docs.rs/log/latest/log/#macros
//!
//! Suppose the `Cargo.toml` file of a crate using `cfg_log` contains
//!
//! ```toml
//! [dependencies]
//! cfg_log = "0.1.0"
//! log = { version = "0.4.17", optional = true }
//! ```
//!
//! Then the code
//!
//! ```rust
//! use cfg_log::*;
//!
//! fn main() {
//!     debug!("the answer is {}", 42);
//! }
//! ```
//!
//! is equivalent to
//!
//! ```rust
//! #[cfg(feature = "log")]
//! use log::*;
//!
//! fn main() {
//!     #[cfg(feature = "log")]
//!     debug!("the answer is {}", 42);
//! }

/// Equivalent to [`log::log_enabled!`](https://docs.rs/log/latest/log/macro.log_enabled.html)
/// if the feature `log` is enabled, `false` otherwise.
#[macro_export]
macro_rules! log_enabled {
    ($($tt:tt)*) => {{
        #[cfg(feature = "log")]
        {
            ::log::log_enabled!($($tt)*)
        }
        #[cfg(not(feature = "log"))]
        {
            false
        }
    }};
}

/// Equivalent to [`log::log!`](https://docs.rs/log/latest/log/macro.log.html)
/// if the feature `log` is enabled, discarded otherwise.
#[macro_export]
macro_rules! log {
    ($($tt:tt)*) => {
        #[cfg(feature="log")]
        ::log::log!($($tt)*);
    };
}

/// Equivalent to [`log::trace!`](https://docs.rs/log/latest/log/macro.trace.html)
/// if the feature `log` is enabled, discarded otherwise.
#[macro_export]
macro_rules! trace {
    ($($tt:tt)*) => {
        #[cfg(feature="log")]
        ::log::trace!($($tt)*);
    };
}

/// Equivalent to [`log::debug!`](https://docs.rs/log/latest/log/macro.debug.html)
/// if the feature `log` is enabled, discarded otherwise.
#[macro_export]
macro_rules! debug {
    ($($tt:tt)*) => {
        #[cfg(feature="log")]
        ::log::debug!($($tt)*);
    };
}

/// Equivalent to [`log::info!`](https://docs.rs/log/latest/log/macro.info.html)
/// if the feature `log` is enabled, discarded otherwise.
#[macro_export]
macro_rules! info {
    ($($tt:tt)*) => {
        #[cfg(feature="log")]
        ::log::info!($($tt)*);
    };
}

/// Equivalent to [`log::warn!`](https://docs.rs/log/latest/log/macro.warn.html)
/// if the feature `log` is enabled, discarded otherwise.
#[macro_export]
macro_rules! warn {
    ($($tt:tt)*) => {
        #[cfg(feature="log")]
        ::log::warn!($($tt)*);
    };
}

/// Equivalent to [`log::error!`](https://docs.rs/log/latest/log/macro.error.html)
/// if the feature `log` is enabled, discarded otherwise.
#[macro_export]
macro_rules! error {
    ($($tt:tt)*) => {
        #[cfg(feature="log")]
        ::log::error!($($tt)*);
    };
}