ckb_logger/lib.rs
1//! CKB logging facade.
2//!
3//! This crate is a wrapper of the crate [`log`].
4//!
5//! [`log`]: https://docs.rs/log/*/log/index.html
6//!
7//! The major issue of the crate `log` is that the macro like
8//! `trace!(target: "global", "message")` is unfriendly to `cargo fmt`. So this
9//! crate disallow using `target: ` in the basic logging macros and add another
10//! group of macros to support both target and message, for example,
11//! `trace_target!("global", "message")`.
12pub use log::{self as internal, Level, SetLoggerError};
13
14#[doc(hidden)]
15#[macro_export]
16macro_rules! env {
17 ($($inner:tt)*) => {
18 env!($($inner)*)
19 }
20}
21
22/// Logs a message at the trace level using the default target.
23///
24/// This macro logs the message using the default target, the module path of
25/// the location of the log request. See [`trace_target!`] which can override the
26/// target.
27///
28/// [`trace_target!`]: macro.trace_target.html
29///
30/// # Examples
31///
32/// ```
33/// use ckb_logger::trace;
34///
35/// # struct Position { x: f32, y: f32 }
36/// let pos = Position { x: 3.234, y: -1.223 };
37///
38/// trace!("Position is: x: {}, y: {}", pos.x, pos.y);
39/// ```
40#[macro_export(local_inner_macros)]
41macro_rules! trace {
42 ($( $args:tt )*) => {
43 $crate::internal::trace!($( $args )*);
44 }
45}
46
47/// Logs a message at the debug level using the default target.
48///
49/// This macro logs the message using the default target, the module path of
50/// the location of the log request. See [`debug_target!`] which can override the
51/// target.
52///
53/// [`debug_target!`]: macro.debug_target.html
54///
55/// # Examples
56///
57/// ```
58/// use ckb_logger::debug;
59///
60/// # struct Position { x: f32, y: f32 }
61/// let pos = Position { x: 3.234, y: -1.223 };
62///
63/// debug!("Position is: x: {}, y: {}", pos.x, pos.y);
64/// ```
65#[macro_export(local_inner_macros)]
66macro_rules! debug {
67 ($( $args:tt )*) => {
68 $crate::internal::debug!($( $args )*);
69 }
70}
71
72/// Logs a message at the info level using the default target.
73///
74/// This macro logs the message using the default target, the module path of
75/// the location of the log request. See [`info_target!`] which can override the
76/// target.
77///
78/// [`info_target!`]: macro.info_target.html
79///
80/// # Examples
81///
82/// ```
83/// use ckb_logger::info;
84///
85/// # struct Connection { port: u32, speed: f32 }
86/// let conn_info = Connection { port: 40, speed: 3.20 };
87///
88/// info!("Connected to port {} at {} Mb/s", conn_info.port, conn_info.speed);
89/// info!(target: "connection_events", "Successful connection, port: {}, speed: {}",
90/// conn_info.port, conn_info.speed);
91/// ```
92#[macro_export(local_inner_macros)]
93macro_rules! info {
94 ($( $args:tt )*) => {
95 $crate::internal::info!($( $args )*);
96 }
97}
98
99/// Logs a message at the warn level using the default target.
100///
101/// This macro logs the message using the default target, the module path of
102/// the location of the log request. See [`warn_target!`] which can override the
103/// target.
104///
105/// [`warn_target!`]: macro.warn_target.html
106///
107/// # Examples
108///
109/// ```
110/// use ckb_logger::warn;
111///
112/// let warn_description = "Invalid Input";
113///
114/// warn!("Warning! {}!", warn_description);
115/// ```
116#[macro_export(local_inner_macros)]
117macro_rules! warn {
118 ($( $args:tt )*) => {
119 $crate::internal::warn!($( $args )*);
120 }
121}
122
123/// Logs a message at the error level using the default target.
124///
125/// This macro logs the message using the default target, the module path of
126/// the location of the log request. See [`error_target!`] which can override the
127/// target.
128///
129/// [`error_target!`]: macro.error_target.html
130///
131/// # Examples
132///
133/// ```
134/// use ckb_logger::error;
135///
136/// let (err_info, port) = ("No connection", 22);
137///
138/// error!("Error: {} on port {}", err_info, port);
139/// ```
140#[macro_export(local_inner_macros)]
141macro_rules! error {
142 ($( $args:tt )*) => {
143 $crate::internal::error!($( $args )*);
144 }
145}
146
147/// Determines if a message logged at the specified level and with the default target will be logged.
148///
149/// The default target is the module path of the location of the log request.
150/// See also [`log_enabled_target!`] the version that supports checking arbitrary
151/// target.
152///
153/// [`log_enabled_target!`]: macro.log_enabled_target.html
154///
155/// This can be used to avoid expensive computation of log message arguments if the message would be ignored anyway.
156///
157/// ## Examples
158///
159/// ```
160/// use ckb_logger::Level::Debug;
161/// use ckb_logger::{debug, log_enabled};
162///
163/// # struct Data { x: u32, y: u32 }
164/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
165/// if log_enabled!(Debug) {
166/// let data = expensive_call();
167/// debug!("expensive debug data: {} {}", data.x, data.y);
168/// }
169/// ```
170#[macro_export(local_inner_macros)]
171macro_rules! log_enabled {
172 ($level:expr) => {
173 $crate::internal::log_enabled!($level);
174 };
175}
176
177/// Logs a message at the trace level using the specified target.
178///
179/// This macro logs the message using the specified target. In the most
180/// scenarios, the log message should just use the default target, which is the
181/// module path of the location of the log request. See [`trace!`] which just logs
182/// using the default target.
183///
184/// [`trace!`]: macro.trace.html
185///
186/// # Examples
187///
188/// ```
189/// use ckb_logger::trace_target;
190///
191/// # struct Position { x: f32, y: f32 }
192/// let pos = Position { x: 3.234, y: -1.223 };
193///
194/// trace_target!("app_events", "Position is: x: {}, y: {}", pos.x, pos.y);
195/// ```
196#[macro_export(local_inner_macros)]
197macro_rules! trace_target {
198 ($target:expr, $( $args:tt )*) => {
199 $crate::internal::trace!(target: $target, $( $args )*);
200 }
201}
202
203/// Logs a message at the debug level using the specified target.
204///
205/// This macro logs the message using the specified target. In the most
206/// scenarios, the log message should just use the default target, which is the
207/// module path of the location of the log request. See [`debug!`] which just logs
208/// using the default target.
209///
210/// [`debug!`]: macro.debug.html
211///
212/// # Examples
213///
214/// ```
215/// use ckb_logger::debug_target;
216///
217/// # struct Position { x: f32, y: f32 }
218/// let pos = Position { x: 3.234, y: -1.223 };
219///
220/// debug_target!("app_events", "Position is: x: {}, y: {}", pos.x, pos.y);
221/// ```
222#[macro_export(local_inner_macros)]
223macro_rules! debug_target {
224 ($target:expr, $( $args:tt )*) => {
225 $crate::internal::debug!(target: $target, $( $args )*);
226 }
227}
228
229/// Logs a message at the info level using the specified target.
230///
231/// This macro logs the message using the specified target. In the most
232/// scenarios, the log message should just use the default target, which is the
233/// module path of the location of the log request. See [`info!`] which just logs
234/// using the default target.
235///
236/// [`info!`]: macro.info.html
237///
238/// # Examples
239///
240/// ```
241/// use ckb_logger::info_target;
242///
243/// # struct Connection { port: u32, speed: f32 }
244/// let conn_info = Connection { port: 40, speed: 3.20 };
245///
246/// info_target!("connection_events", "Successful connection, port: {}, speed: {}",
247/// conn_info.port, conn_info.speed);
248/// ```
249#[macro_export(local_inner_macros)]
250macro_rules! info_target {
251 ($target:expr, $( $args:tt )*) => {
252 $crate::internal::info!(target: $target, $( $args )*);
253 }
254}
255
256/// Logs a message at the warn level using the specified target.
257///
258/// This macro logs the message using the specified target. In the most
259/// scenarios, the log message should just use the default target, which is the
260/// module path of the location of the log request. See [`warn!`] which just logs
261/// using the default target.
262///
263/// [`warn!`]: macro.warn.html
264///
265/// # Examples
266///
267/// ```
268/// use ckb_logger::warn_target;
269///
270/// let warn_description = "Invalid Input";
271///
272/// warn_target!("input_events", "App received warning: {}", warn_description);
273/// ```
274#[macro_export(local_inner_macros)]
275macro_rules! warn_target {
276 ($target:expr, $( $args:tt )*) => {
277 $crate::internal::warn!(target: $target, $( $args )*);
278 }
279}
280
281/// Logs a message at the error level using the specified target.
282///
283/// This macro logs the message using the specified target. In the most
284/// scenarios, the log message should just use the default target, which is the
285/// module path of the location of the log request. See [`error!`] which just logs
286/// using the default target.
287///
288/// [`error!`]: macro.error.html
289///
290/// # Examples
291///
292/// ```
293/// use ckb_logger::error_target;
294///
295/// let (err_info, port) = ("No connection", 22);
296///
297/// error_target!("app_events", "App Error: {}, Port: {}", err_info, port);
298/// ```
299#[macro_export(local_inner_macros)]
300macro_rules! error_target {
301 ($target:expr, $( $args:tt )*) => {
302 $crate::internal::error!(target: $target, $( $args )*);
303 }
304}
305
306/// Determines if a message logged at the specified level and with the specified target will be logged.
307///
308/// This can be used to avoid expensive computation of log message arguments if the message would be ignored anyway.
309///
310/// See also [`log_enabled!`] the version that checks with the default target.
311///
312/// [`log_enabled!`]: macro.log_enabled.html
313///
314/// ## Examples
315///
316/// ```
317/// use ckb_logger::Level::Debug;
318/// use ckb_logger::{debug_target, log_enabled_target};
319///
320/// # struct Data { x: u32, y: u32 }
321/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
322/// if log_enabled_target!("Global", Debug) {
323/// let data = expensive_call();
324/// debug_target!("Global", "expensive debug data: {} {}", data.x, data.y);
325/// }
326/// ```
327#[macro_export(local_inner_macros)]
328macro_rules! log_enabled_target {
329 ($target:expr, $level:expr) => {
330 $crate::internal::log_enabled!(target: $target, $level);
331 };
332}