spdlog/
log_macros.rs

1/// Logs a message at the specified level.
2///
3/// This macro will generically log with the specified [`Level`] and `format!`
4/// based argument list.
5#[doc = include_str!("./include/doc/log-macro-nameed-opt-params.md")]
6/// # Examples
7///
8/// ```
9/// use spdlog::{log, Level};
10///
11/// # let app_events = spdlog::default_logger();
12/// let data = (42, "Forty-two");
13///
14/// // Using the global default logger
15/// log!(Level::Info, "Received data: {}, {}", data.0, data.1);
16///
17/// // Or using the specified logger
18/// log!(logger: app_events, Level::Info, "Received data: {}, {}", data.0, data.1);
19/// ```
20///
21/// [`Level`]: crate::Level
22#[macro_export]
23macro_rules! log {
24    (logger: $logger:expr, $level:expr, $($arg:tt)+) => ({
25        let logger = &$logger;
26        const LEVEL: $crate::Level = $level;
27        const SHOULD_LOG: bool = $crate::STATIC_LEVEL_FILTER.__test_const(LEVEL);
28        if SHOULD_LOG && logger.should_log(LEVEL) {
29            $crate::__log(logger, LEVEL, $crate::source_location_current!(), format_args!($($arg)+));
30        }
31    });
32    ($level:expr, $($arg:tt)+) => ($crate::log!(logger: $crate::default_logger(), $level, $($arg)+))
33}
34
35/// Logs a message at the critical level.
36#[doc = include_str!("./include/doc/log-macro-nameed-opt-params.md")]
37/// # Examples
38///
39/// ```
40/// use spdlog::critical;
41///
42/// # let app_events = spdlog::default_logger();
43/// let (left, right) = (true, false);
44///
45/// // Using the global default logger
46/// critical!("Runtime assertion failed. Left: `{}`, Right: `{}`", left, right);
47///
48/// // Or using the specified logger
49/// critical!(logger: app_events, "Runtime assertion failed. Left: `{}`, Right: `{}`", left, right);
50/// ```
51#[macro_export]
52macro_rules! critical {
53    (logger: $logger:expr, $($arg:tt)+) => (
54        $crate::log!(logger: $logger, $crate::Level::Critical, $($arg)+)
55    );
56    ($($arg:tt)+) => (
57        $crate::log!($crate::Level::Critical, $($arg)+)
58    )
59}
60
61/// Logs a message at the error level.
62#[doc = include_str!("./include/doc/log-macro-nameed-opt-params.md")]
63/// # Examples
64///
65/// ```
66/// use spdlog::error;
67///
68/// # let app_events = spdlog::default_logger();
69/// let (err_info, port) = ("No connection", 22);
70///
71/// // Using the global default logger
72/// error!("Error: {} on port {}", err_info, port);
73///
74/// // Or using the specified logger
75/// error!(logger: app_events, "App Error: {}, Port: {}", err_info, port);
76/// ```
77#[macro_export]
78macro_rules! error {
79    (logger: $logger:expr, $($arg:tt)+) => (
80        $crate::log!(logger: $logger, $crate::Level::Error, $($arg)+)
81    );
82    ($($arg:tt)+) => (
83        $crate::log!($crate::Level::Error, $($arg)+)
84    )
85}
86
87/// Logs a message at the warn level.
88#[doc = include_str!("./include/doc/log-macro-nameed-opt-params.md")]
89/// # Examples
90///
91/// ```
92/// use spdlog::warn;
93///
94/// # let input_events = spdlog::default_logger();
95/// let warn_description = "Invalid Input";
96///
97/// // Using the global default logger
98/// warn!("Warning! {}!", warn_description);
99///
100/// // Or using the specified logger
101/// warn!(logger: input_events, "App received warning: {}", warn_description);
102/// ```
103#[macro_export]
104macro_rules! warn {
105    (logger: $logger:expr, $($arg:tt)+) => (
106        $crate::log!(logger: $logger, $crate::Level::Warn, $($arg)+)
107    );
108    ($($arg:tt)+) => (
109        $crate::log!($crate::Level::Warn, $($arg)+)
110    )
111}
112
113/// Logs a message at the info level.
114#[doc = include_str!("./include/doc/log-macro-nameed-opt-params.md")]
115/// # Examples
116///
117/// ```
118/// use spdlog::info;
119///
120/// # struct Connection { port: u32, speed: f32 }
121/// # let conn_events = spdlog::default_logger();
122/// let conn_info = Connection { port: 40, speed: 3.20 };
123///
124/// // Using the global default logger
125/// info!("Connected to port {} at {} Mb/s", conn_info.port, conn_info.speed);
126///
127/// // Or using the specified logger
128/// info!(logger: conn_events, "Successfull connection, port: {}, speed: {}", conn_info.port, conn_info.speed);
129/// ```
130#[macro_export]
131macro_rules! info {
132    (logger: $logger:expr, $($arg:tt)+) => (
133        $crate::log!(logger: $logger, $crate::Level::Info, $($arg)+)
134    );
135    ($($arg:tt)+) => (
136        $crate::log!($crate::Level::Info, $($arg)+)
137    )
138}
139
140/// Logs a message at the debug level.
141#[doc = include_str!("./include/doc/log-macro-nameed-opt-params.md")]
142/// # Examples
143///
144/// ```
145/// use spdlog::debug;
146///
147/// # struct Position { x: f32, y: f32 }
148/// # let app_events = spdlog::default_logger();
149/// let pos = Position { x: 3.234, y: -1.223 };
150///
151/// // Using the global default logger
152/// debug!("New position: x: {}, y: {}", pos.x, pos.y);
153///
154/// // Or using the specified logger
155/// debug!(logger: app_events, "New position: x: {}, y: {}", pos.x, pos.y);
156/// ```
157#[macro_export]
158macro_rules! debug {
159    (logger: $logger:expr, $($arg:tt)+) => (
160        $crate::log!(logger: $logger, $crate::Level::Debug, $($arg)+)
161    );
162    ($($arg:tt)+) => (
163        $crate::log!($crate::Level::Debug, $($arg)+)
164    )
165}
166
167/// Logs a message at the trace level.
168#[doc = include_str!("./include/doc/log-macro-nameed-opt-params.md")]
169/// # Examples
170///
171/// ```
172/// use spdlog::trace;
173///
174/// # struct Position { x: f32, y: f32 }
175/// # let app_events = spdlog::default_logger();
176/// let pos = Position { x: 3.234, y: -1.223 };
177///
178/// // Using the global default logger
179/// trace!("Position is: x: {}, y: {}", pos.x, pos.y);
180///
181/// // Or using the specified logger
182/// trace!(logger: app_events, "x is {} and y is {}",
183///        if pos.x >= 0.0 { "positive" } else { "negative" },
184///        if pos.y >= 0.0 { "positive" } else { "negative" });
185/// ```
186#[macro_export]
187macro_rules! trace {
188    (logger: $logger:expr, $($arg:tt)+) => (
189        $crate::log!(logger: $logger, $crate::Level::Trace, $($arg)+)
190    );
191    ($($arg:tt)+) => (
192        $crate::log!($crate::Level::Trace, $($arg)+)
193    )
194}