optional_log/
lib.rs

1//! # What
2//!
3//! This crate provides an optional wrapper around the ["log" crate](https://crates.io/crates/log),
4//! which allows you to provide an optional "log" feature for you crates easily.
5//!
6//! # How
7//!
8//! In your "Cargo.toml":
9//!
10//! ```toml
11//! [dependencies]
12//! log = { version = "0.4", optional = true }
13//! optional-log = "0.1"
14//!
15//! [feature]
16//! log = ["dep:log", "optional-log/log"]
17//! ```
18//!
19//! Then use macros of "optional-log" crate instead of those of the ["log" crate](https://crates.io/crates/log).
20//!
21//! In this way, once the "log" feature of your crate is enabled by downstream, these macros will be expanded to
22//! the corresponding macros of the ["log" crate](https://crates.io/crates/log), otherwise they do nothing.
23//!
24//! The [`log_enabled!`] macro will always return `false` if the "log" feature is not enabled.
25
26#[macro_export]
27#[cfg(feature = "log")]
28macro_rules! log {
29    ($($t:tt)*) => {
30        ::log::log!($($t)*)
31    };
32}
33
34#[macro_export]
35#[cfg(not(feature = "log"))]
36macro_rules! log {
37    ($($t:tt)*) => {
38        ()
39    };
40}
41
42#[macro_export]
43#[cfg(feature = "log")]
44macro_rules! trace {
45    ($($t:tt)*) => {
46        ::log::trace!($($t)*)
47    };
48}
49
50#[macro_export]
51#[cfg(not(feature = "log"))]
52macro_rules! trace {
53    ($($t:tt)*) => {
54        ()
55    };
56}
57
58#[macro_export]
59#[cfg(feature = "log")]
60macro_rules! debug {
61    ($($t:tt)*) => {
62        ::log::debug!($($t)*)
63    };
64}
65
66#[macro_export]
67#[cfg(not(feature = "log"))]
68macro_rules! debug {
69    ($($t:tt)*) => {
70        ()
71    };
72}
73
74#[macro_export]
75#[cfg(feature = "log")]
76macro_rules! info {
77    ($($t:tt)*) => {
78        ::log::info!($($t)*)
79    };
80}
81
82#[macro_export]
83#[cfg(not(feature = "log"))]
84macro_rules! info {
85    ($($t:tt)*) => {
86        ()
87    };
88}
89
90#[macro_export]
91#[cfg(feature = "log")]
92macro_rules! warn {
93    ($($t:tt)*) => {
94        ::log::warn!($($t)*)
95    };
96}
97
98#[macro_export]
99#[cfg(not(feature = "log"))]
100macro_rules! warn {
101    ($($t:tt)*) => {
102        ()
103    };
104}
105
106#[macro_export]
107#[cfg(feature = "log")]
108macro_rules! error {
109    ($($t:tt)*) => {
110        ::log::error!($($t)*)
111    };
112}
113
114#[macro_export]
115#[cfg(not(feature = "log"))]
116macro_rules! error {
117    ($($t:tt)*) => {
118        ()
119    };
120}
121
122#[macro_export]
123#[cfg(feature = "log")]
124macro_rules! log_enabled {
125    ($($t:tt)*) => {
126        ::log::log_enabled!($($t)*)
127    };
128}
129
130#[macro_export]
131#[cfg(not(feature = "log"))]
132macro_rules! log_enabled {
133    ($($t:tt)*) => {
134        false
135    };
136}