]>
git.proxmox.com Git - rustc.git/blob - vendor/log/src/macros.rs
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.
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.
11 /// The standard logging macro.
13 /// This macro will generically log with the specified `Level` and `format!`
14 /// based argument list.
19 /// use log::{log, Level};
22 /// let data = (42, "Forty-two");
23 /// let private_data = "private";
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);
30 #[macro_export(local_inner_macros)]
32 (target
: $target
:expr
, $lvl
:expr
, $
($arg
:tt
)+) => ({
34 if lvl
<= $
crate::STATIC_MAX_LEVEL
&& lvl
<= $
crate::max_level() {
35 $
crate::__private_api_log(
36 __log_format_args
!($
($arg
)+),
38 &($target
, __log_module_path
!(), __log_file
!(), __log_line
!()),
42 ($lvl
:expr
, $
($arg
:tt
)+) => (log
!(target
: __log_module_path
!(), $lvl
, $
($arg
)+))
45 /// Logs a message at the error level.
53 /// let (err_info, port) = ("No connection", 22);
55 /// error!("Error: {} on port {}", err_info, port);
56 /// error!(target: "app_events", "App Error: {}, Port: {}", err_info, 22);
59 #[macro_export(local_inner_macros)]
61 (target
: $target
:expr
, $
($arg
:tt
)+) => (
62 log
!(target
: $target
, $
crate::Level
::Error
, $
($arg
)+)
65 log
!($
crate::Level
::Error
, $
($arg
)+)
69 /// Logs a message at the warn level.
77 /// let warn_description = "Invalid Input";
79 /// warn!("Warning! {}!", warn_description);
80 /// warn!(target: "input_events", "App received warning: {}", warn_description);
83 #[macro_export(local_inner_macros)]
85 (target
: $target
:expr
, $
($arg
:tt
)+) => (
86 log
!(target
: $target
, $
crate::Level
::Warn
, $
($arg
)+)
89 log
!($
crate::Level
::Warn
, $
($arg
)+)
93 /// Logs a message at the info level.
101 /// # struct Connection { port: u32, speed: f32 }
102 /// let conn_info = Connection { port: 40, speed: 3.20 };
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);
109 #[macro_export(local_inner_macros)]
111 (target
: $target
:expr
, $
($arg
:tt
)+) => (
112 log
!(target
: $target
, $
crate::Level
::Info
, $
($arg
)+)
115 log
!($
crate::Level
::Info
, $
($arg
)+)
119 /// Logs a message at the debug level.
127 /// # struct Position { x: f32, y: f32 }
128 /// let pos = Position { x: 3.234, y: -1.223 };
130 /// debug!("New position: x: {}, y: {}", pos.x, pos.y);
131 /// debug!(target: "app_events", "New position: x: {}, y: {}", pos.x, pos.y);
134 #[macro_export(local_inner_macros)]
136 (target
: $target
:expr
, $
($arg
:tt
)+) => (
137 log
!(target
: $target
, $
crate::Level
::Debug
, $
($arg
)+)
140 log
!($
crate::Level
::Debug
, $
($arg
)+)
144 /// Logs a message at the trace level.
152 /// # struct Position { x: f32, y: f32 }
153 /// let pos = Position { x: 3.234, y: -1.223 };
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" });
161 #[macro_export(local_inner_macros)]
163 (target
: $target
:expr
, $
($arg
:tt
)+) => (
164 log
!(target
: $target
, $
crate::Level
::Trace
, $
($arg
)+)
167 log
!($
crate::Level
::Trace
, $
($arg
)+)
171 /// Determines if a message logged at the specified level in that module will
174 /// This can be used to avoid expensive computation of log message arguments if
175 /// the message would be ignored anyway.
180 /// use log::Level::Debug;
181 /// use log::{debug, log_enabled};
184 /// if log_enabled!(Debug) {
185 /// let data = expensive_call();
186 /// debug!("expensive debug data: {} {}", data.x, data.y);
188 /// if log_enabled!(target: "Global", Debug) {
189 /// let data = expensive_call();
190 /// debug!(target: "Global", "expensive debug data: {} {}", data.x, data.y);
193 /// # struct Data { x: u32, y: u32 }
194 /// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
197 #[macro_export(local_inner_macros)]
198 macro_rules
! log_enabled
{
199 (target
: $target
:expr
, $lvl
:expr
) => {{
201 lvl
<= $
crate::STATIC_MAX_LEVEL
202 && lvl
<= $
crate::max_level()
203 && $
crate::__private_api_enabled(lvl
, $target
)
206 log_enabled
!(target
: __log_module_path
!(), $lvl
)
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
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.
222 macro_rules
! __log_format_args
{
224 format_args
!($
($args
)*)
230 macro_rules
! __log_module_path
{
238 macro_rules
! __log_file
{
246 macro_rules
! __log_line
{