blob: a234e0412f6583af494af9fc162e9bcaff3de758 [file] [log] [blame]
Haibo Huang029e5972020-07-15 21:35:42 -07001// 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(local_inner_macros)]
31macro_rules! log {
Haibo Huang029e5972020-07-15 21:35:42 -070032 (target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
33 let lvl = $lvl;
34 if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
35 $crate::__private_api_log(
36 __log_format_args!($($arg)+),
37 lvl,
38 &($target, __log_module_path!(), __log_file!(), __log_line!()),
39 );
40 }
41 });
42 ($lvl:expr, $($arg:tt)+) => (log!(target: __log_module_path!(), $lvl, $($arg)+))
43}
44
45/// Logs a message at the error level.
46///
47/// # Examples
48///
49/// ```edition2018
50/// use log::error;
51///
52/// # fn main() {
53/// let (err_info, port) = ("No connection", 22);
54///
55/// error!("Error: {} on port {}", err_info, port);
56/// error!(target: "app_events", "App Error: {}, Port: {}", err_info, 22);
57/// # }
58/// ```
59#[macro_export(local_inner_macros)]
60macro_rules! error {
61 (target: $target:expr, $($arg:tt)+) => (
Joel Galenson30e3e1c2021-04-02 16:02:24 -070062 log!(target: $target, $crate::Level::Error, $($arg)+)
Haibo Huang029e5972020-07-15 21:35:42 -070063 );
64 ($($arg:tt)+) => (
Joel Galenson30e3e1c2021-04-02 16:02:24 -070065 log!($crate::Level::Error, $($arg)+)
Haibo Huang029e5972020-07-15 21:35:42 -070066 )
67}
68
69/// Logs a message at the warn level.
70///
71/// # Examples
72///
73/// ```edition2018
74/// use log::warn;
75///
76/// # fn main() {
77/// let warn_description = "Invalid Input";
78///
79/// warn!("Warning! {}!", warn_description);
80/// warn!(target: "input_events", "App received warning: {}", warn_description);
81/// # }
82/// ```
83#[macro_export(local_inner_macros)]
84macro_rules! warn {
85 (target: $target:expr, $($arg:tt)+) => (
Joel Galenson30e3e1c2021-04-02 16:02:24 -070086 log!(target: $target, $crate::Level::Warn, $($arg)+)
Haibo Huang029e5972020-07-15 21:35:42 -070087 );
88 ($($arg:tt)+) => (
Joel Galenson30e3e1c2021-04-02 16:02:24 -070089 log!($crate::Level::Warn, $($arg)+)
Haibo Huang029e5972020-07-15 21:35:42 -070090 )
91}
92
93/// Logs a message at the info level.
94///
95/// # Examples
96///
97/// ```edition2018
98/// use log::info;
99///
100/// # fn main() {
101/// # struct Connection { port: u32, speed: f32 }
102/// let conn_info = Connection { port: 40, speed: 3.20 };
103///
104/// info!("Connected to port {} at {} Mb/s", conn_info.port, conn_info.speed);
105/// info!(target: "connection_events", "Successfull connection, port: {}, speed: {}",
106/// conn_info.port, conn_info.speed);
107/// # }
108/// ```
109#[macro_export(local_inner_macros)]
110macro_rules! info {
111 (target: $target:expr, $($arg:tt)+) => (
Joel Galenson30e3e1c2021-04-02 16:02:24 -0700112 log!(target: $target, $crate::Level::Info, $($arg)+)
Haibo Huang029e5972020-07-15 21:35:42 -0700113 );
114 ($($arg:tt)+) => (
Joel Galenson30e3e1c2021-04-02 16:02:24 -0700115 log!($crate::Level::Info, $($arg)+)
Haibo Huang029e5972020-07-15 21:35:42 -0700116 )
117}
118
119/// Logs a message at the debug level.
120///
121/// # Examples
122///
123/// ```edition2018
124/// use log::debug;
125///
126/// # fn main() {
127/// # struct Position { x: f32, y: f32 }
128/// let pos = Position { x: 3.234, y: -1.223 };
129///
130/// debug!("New position: x: {}, y: {}", pos.x, pos.y);
131/// debug!(target: "app_events", "New position: x: {}, y: {}", pos.x, pos.y);
132/// # }
133/// ```
134#[macro_export(local_inner_macros)]
135macro_rules! debug {
136 (target: $target:expr, $($arg:tt)+) => (
Joel Galenson30e3e1c2021-04-02 16:02:24 -0700137 log!(target: $target, $crate::Level::Debug, $($arg)+)
Haibo Huang029e5972020-07-15 21:35:42 -0700138 );
139 ($($arg:tt)+) => (
Joel Galenson30e3e1c2021-04-02 16:02:24 -0700140 log!($crate::Level::Debug, $($arg)+)
Haibo Huang029e5972020-07-15 21:35:42 -0700141 )
142}
143
144/// Logs a message at the trace level.
145///
146/// # Examples
147///
148/// ```edition2018
149/// use log::trace;
150///
151/// # fn main() {
152/// # struct Position { x: f32, y: f32 }
153/// let pos = Position { x: 3.234, y: -1.223 };
154///
155/// trace!("Position is: x: {}, y: {}", pos.x, pos.y);
156/// trace!(target: "app_events", "x is {} and y is {}",
157/// if pos.x >= 0.0 { "positive" } else { "negative" },
158/// if pos.y >= 0.0 { "positive" } else { "negative" });
159/// # }
160/// ```
161#[macro_export(local_inner_macros)]
162macro_rules! trace {
163 (target: $target:expr, $($arg:tt)+) => (
Joel Galenson30e3e1c2021-04-02 16:02:24 -0700164 log!(target: $target, $crate::Level::Trace, $($arg)+)
Haibo Huang029e5972020-07-15 21:35:42 -0700165 );
166 ($($arg:tt)+) => (
Joel Galenson30e3e1c2021-04-02 16:02:24 -0700167 log!($crate::Level::Trace, $($arg)+)
Haibo Huang029e5972020-07-15 21:35:42 -0700168 )
169}
170
171/// Determines if a message logged at the specified level in that module will
172/// be logged.
173///
174/// This can be used to avoid expensive computation of log message arguments if
175/// the message would be ignored anyway.
176///
177/// # Examples
178///
179/// ```edition2018
180/// use log::Level::Debug;
181/// use log::{debug, log_enabled};
182///
183/// # fn foo() {
184/// if log_enabled!(Debug) {
185/// let data = expensive_call();
186/// debug!("expensive debug data: {} {}", data.x, data.y);
187/// }
188/// if log_enabled!(target: "Global", Debug) {
189/// let data = expensive_call();
190/// debug!(target: "Global", "expensive debug data: {} {}", data.x, data.y);
191/// }
192/// # }
193/// # struct Data { x: u32, y: u32 }
194/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
195/// # fn main() {}
196/// ```
197#[macro_export(local_inner_macros)]
198macro_rules! log_enabled {
199 (target: $target:expr, $lvl:expr) => {{
200 let lvl = $lvl;
201 lvl <= $crate::STATIC_MAX_LEVEL
202 && lvl <= $crate::max_level()
203 && $crate::__private_api_enabled(lvl, $target)
204 }};
205 ($lvl:expr) => {
206 log_enabled!(target: __log_module_path!(), $lvl)
207 };
208}
209
210// The log macro above cannot invoke format_args directly because it uses
211// local_inner_macros. A format_args invocation there would resolve to
212// $crate::format_args which does not exist. Instead invoke format_args here
213// outside of local_inner_macros so that it resolves (probably) to
214// core::format_args or std::format_args. Same for the several macros that
215// follow.
216//
217// This is a workaround until we drop support for pre-1.30 compilers. At that
218// point we can remove use of local_inner_macros, use $crate:: when invoking
219// local macros, and invoke format_args directly.
220#[doc(hidden)]
221#[macro_export]
222macro_rules! __log_format_args {
223 ($($args:tt)*) => {
224 format_args!($($args)*)
225 };
226}
227
228#[doc(hidden)]
229#[macro_export]
230macro_rules! __log_module_path {
231 () => {
232 module_path!()
233 };
234}
235
236#[doc(hidden)]
237#[macro_export]
238macro_rules! __log_file {
239 () => {
240 file!()
241 };
242}
243
244#[doc(hidden)]
245#[macro_export]
246macro_rules! __log_line {
247 () => {
248 line!()
249 };
250}