1// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11/// The standard logging macro.
12///
13/// This macro will generically log with the specified `Level` and `format!`
14/// based argument list.
15///
16/// # Examples
17///
18/// ```edition2018
19/// use log::{log, Level};
20///
21/// # fn main() {
22/// let data = (42, "Forty-two");
23/// let private_data = "private";
24///
25/// log!(Level::Error, "Received errors: {}, {}", data.0, data.1);
26/// log!(target: "app_events", Level::Warn, "App warning: {}, {}, {}",
27/// data.0, data.1, private_data);
28/// # }
29/// ```
30#[macro_export]
31macro_rules! log {
32 // log!(target: "my_target", Level::Info, key1 = 42, key2 = true; "a {} event", "log");
33 (target: $target:expr, $lvl:expr, $($key:tt = $value:expr),+; $($arg:tt)+) => ({
34 let lvl = $lvl;
35 if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
36 $crate::__private_api::log(
37 $crate::__private_api::format_args!($($arg)+),
38 lvl,
39 &($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
40 $crate::__private_api::line!(),
41 $crate::__private_api::Option::Some(&[$(($crate::__log_key!($key), &$value)),+])
42 );
43 }
44 });
45
46 // log!(target: "my_target", Level::Info, "a {} event", "log");
47 (target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
48 let lvl = $lvl;
49 if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
50 $crate::__private_api::log(
51 $crate::__private_api::format_args!($($arg)+),
52 lvl,
53 &($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()),
54 $crate::__private_api::line!(),
55 $crate::__private_api::Option::None,
56 );
57 }
58 });
59
60 // log!(Level::Info, "a log event")
61 ($lvl:expr, $($arg:tt)+) => ($crate::log!(target: $crate::__private_api::module_path!(), $lvl, $($arg)+));
62}
63
64/// Logs a message at the error level.
65///
66/// # Examples
67///
68/// ```edition2018
69/// use log::error;
70///
71/// # fn main() {
72/// let (err_info, port) = ("No connection", 22);
73///
74/// error!("Error: {} on port {}", err_info, port);
75/// error!(target: "app_events", "App Error: {}, Port: {}", err_info, 22);
76/// # }
77/// ```
78#[macro_export]
79macro_rules! error {
80 // error!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
81 // error!(target: "my_target", "a {} event", "log")
82 (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Error, $($arg)+));
83
84 // error!("a {} event", "log")
85 ($($arg:tt)+) => ($crate::log!($crate::Level::Error, $($arg)+))
86}
87
88/// Logs a message at the warn level.
89///
90/// # Examples
91///
92/// ```edition2018
93/// use log::warn;
94///
95/// # fn main() {
96/// let warn_description = "Invalid Input";
97///
98/// warn!("Warning! {}!", warn_description);
99/// warn!(target: "input_events", "App received warning: {}", warn_description);
100/// # }
101/// ```
102#[macro_export]
103macro_rules! warn {
104 // warn!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
105 // warn!(target: "my_target", "a {} event", "log")
106 (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Warn, $($arg)+));
107
108 // warn!("a {} event", "log")
109 ($($arg:tt)+) => ($crate::log!($crate::Level::Warn, $($arg)+))
110}
111
112/// Logs a message at the info level.
113///
114/// # Examples
115///
116/// ```edition2018
117/// use log::info;
118///
119/// # fn main() {
120/// # struct Connection { port: u32, speed: f32 }
121/// let conn_info = Connection { port: 40, speed: 3.20 };
122///
123/// info!("Connected to port {} at {} Mb/s", conn_info.port, conn_info.speed);
124/// info!(target: "connection_events", "Successful connection, port: {}, speed: {}",
125/// conn_info.port, conn_info.speed);
126/// # }
127/// ```
128#[macro_export]
129macro_rules! info {
130 // info!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
131 // info!(target: "my_target", "a {} event", "log")
132 (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Info, $($arg)+));
133
134 // info!("a {} event", "log")
135 ($($arg:tt)+) => ($crate::log!($crate::Level::Info, $($arg)+))
136}
137
138/// Logs a message at the debug level.
139///
140/// # Examples
141///
142/// ```edition2018
143/// use log::debug;
144///
145/// # fn main() {
146/// # struct Position { x: f32, y: f32 }
147/// let pos = Position { x: 3.234, y: -1.223 };
148///
149/// debug!("New position: x: {}, y: {}", pos.x, pos.y);
150/// debug!(target: "app_events", "New position: x: {}, y: {}", pos.x, pos.y);
151/// # }
152/// ```
153#[macro_export]
154macro_rules! debug {
155 // debug!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
156 // debug!(target: "my_target", "a {} event", "log")
157 (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Debug, $($arg)+));
158
159 // debug!("a {} event", "log")
160 ($($arg:tt)+) => ($crate::log!($crate::Level::Debug, $($arg)+))
161}
162
163/// Logs a message at the trace level.
164///
165/// # Examples
166///
167/// ```edition2018
168/// use log::trace;
169///
170/// # fn main() {
171/// # struct Position { x: f32, y: f32 }
172/// let pos = Position { x: 3.234, y: -1.223 };
173///
174/// trace!("Position is: x: {}, y: {}", pos.x, pos.y);
175/// trace!(target: "app_events", "x is {} and y is {}",
176/// if pos.x >= 0.0 { "positive" } else { "negative" },
177/// if pos.y >= 0.0 { "positive" } else { "negative" });
178/// # }
179/// ```
180#[macro_export]
181macro_rules! trace {
182 // trace!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
183 // trace!(target: "my_target", "a {} event", "log")
184 (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Trace, $($arg)+));
185
186 // trace!("a {} event", "log")
187 ($($arg:tt)+) => ($crate::log!($crate::Level::Trace, $($arg)+))
188}
189
190/// Determines if a message logged at the specified level in that module will
191/// be logged.
192///
193/// This can be used to avoid expensive computation of log message arguments if
194/// the message would be ignored anyway.
195///
196/// # Examples
197///
198/// ```edition2018
199/// use log::Level::Debug;
200/// use log::{debug, log_enabled};
201///
202/// # fn foo() {
203/// if log_enabled!(Debug) {
204/// let data = expensive_call();
205/// debug!("expensive debug data: {} {}", data.x, data.y);
206/// }
207/// if log_enabled!(target: "Global", Debug) {
208/// let data = expensive_call();
209/// debug!(target: "Global", "expensive debug data: {} {}", data.x, data.y);
210/// }
211/// # }
212/// # struct Data { x: u32, y: u32 }
213/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
214/// # fn main() {}
215/// ```
216#[macro_export]
217macro_rules! log_enabled {
218 (target: $target:expr, $lvl:expr) => {{
219 let lvl = $lvl;
220 lvl <= $crate::STATIC_MAX_LEVEL
221 && lvl <= $crate::max_level()
222 && $crate::__private_api::enabled(lvl, $target)
223 }};
224 ($lvl:expr) => {
225 $crate::log_enabled!(target: $crate::__private_api::module_path!(), $lvl)
226 };
227}
228
229#[doc(hidden)]
230#[macro_export]
231macro_rules! __log_key {
232 // key1 = 42
233 ($($args:ident)*) => {
234 $crate::__private_api::stringify!($($args)*)
235 };
236 // "key1" = 42
237 ($($args:expr)*) => {
238 $($args)*
239 };
240}
241