ckb_logger/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//! CKB logging facade.
//!
//! This crate is a wrapper of the crate [`log`].
//!
//! [`log`]: https://docs.rs/log/*/log/index.html
//!
//! The major issue of the crate `log` is that the macro like
//! `trace!(target: "global", "message")` is unfriendly to `cargo fmt`. So this
//! crate disallow using `target: ` in the basic logging macros and add another
//! group of macros to support both target and message, for example,
//! `trace_target!("global", "message")`.
pub use log::{self as internal, Level, SetLoggerError};

#[doc(hidden)]
#[macro_export]
macro_rules! env {
    ($($inner:tt)*) => {
        env!($($inner)*)
    }
}

/// Logs a message at the trace level using the default target.
///
/// This macro logs the message using the default target, the module path of
/// the location of the log request. See [`trace_target!`] which can override the
/// target.
///
/// [`trace_target!`]: macro.trace_target.html
///
/// # Examples
///
/// ```
/// use ckb_logger::trace;
///
/// # struct Position { x: f32, y: f32 }
/// let pos = Position { x: 3.234, y: -1.223 };
///
/// trace!("Position is: x: {}, y: {}", pos.x, pos.y);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! trace {
    ($( $args:tt )*) => {
        $crate::internal::trace!($( $args )*);
    }
}

/// Logs a message at the debug level using the default target.
///
/// This macro logs the message using the default target, the module path of
/// the location of the log request. See [`debug_target!`] which can override the
/// target.
///
/// [`debug_target!`]: macro.debug_target.html
///
/// # Examples
///
/// ```
/// use ckb_logger::debug;
///
/// # struct Position { x: f32, y: f32 }
/// let pos = Position { x: 3.234, y: -1.223 };
///
/// debug!("Position is: x: {}, y: {}", pos.x, pos.y);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! debug {
    ($( $args:tt )*) => {
        $crate::internal::debug!($( $args )*);
    }
}

/// Logs a message at the info level using the default target.
///
/// This macro logs the message using the default target, the module path of
/// the location of the log request. See [`info_target!`] which can override the
/// target.
///
/// [`info_target!`]: macro.info_target.html
///
/// # Examples
///
/// ```
/// use ckb_logger::info;
///
/// # struct Connection { port: u32, speed: f32 }
/// let conn_info = Connection { port: 40, speed: 3.20 };
///
/// info!("Connected to port {} at {} Mb/s", conn_info.port, conn_info.speed);
/// info!(target: "connection_events", "Successful connection, port: {}, speed: {}",
///       conn_info.port, conn_info.speed);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! info {
    ($( $args:tt )*) => {
        $crate::internal::info!($( $args )*);
    }
}

/// Logs a message at the warn level using the default target.
///
/// This macro logs the message using the default target, the module path of
/// the location of the log request. See [`warn_target!`] which can override the
/// target.
///
/// [`warn_target!`]: macro.warn_target.html
///
/// # Examples
///
/// ```
/// use ckb_logger::warn;
///
/// let warn_description = "Invalid Input";
///
/// warn!("Warning! {}!", warn_description);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! warn {
    ($( $args:tt )*) => {
        $crate::internal::warn!($( $args )*);
    }
}

/// Logs a message at the error level using the default target.
///
/// This macro logs the message using the default target, the module path of
/// the location of the log request. See [`error_target!`] which can override the
/// target.
///
/// [`error_target!`]: macro.error_target.html
///
/// # Examples
///
/// ```
/// use ckb_logger::error;
///
/// let (err_info, port) = ("No connection", 22);
///
/// error!("Error: {} on port {}", err_info, port);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! error {
    ($( $args:tt )*) => {
        $crate::internal::error!($( $args )*);
    }
}

/// Determines if a message logged at the specified level and with the default target will be logged.
///
/// The default target is the module path of the location of the log request.
/// See also [`log_enabled_target!`] the version that supports checking arbitrary
/// target.
///
/// [`log_enabled_target!`]: macro.log_enabled_target.html
///
/// This can be used to avoid expensive computation of log message arguments if the message would be ignored anyway.
///
/// ## Examples
///
/// ```
/// use ckb_logger::Level::Debug;
/// use ckb_logger::{debug, log_enabled};
///
/// # struct Data { x: u32, y: u32 }
/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
/// if log_enabled!(Debug) {
///     let data = expensive_call();
///     debug!("expensive debug data: {} {}", data.x, data.y);
/// }
/// ```
#[macro_export(local_inner_macros)]
macro_rules! log_enabled {
    ($level:expr) => {
        $crate::internal::log_enabled!($level);
    };
}

/// Logs a message at the trace level using the specified target.
///
/// This macro logs the message using the specified target. In the most
/// scenarios, the log message should just use the default target, which is the
/// module path of the location of the log request. See [`trace!`] which just logs
/// using the default target.
///
/// [`trace!`]: macro.trace.html
///
/// # Examples
///
/// ```
/// use ckb_logger::trace_target;
///
/// # struct Position { x: f32, y: f32 }
/// let pos = Position { x: 3.234, y: -1.223 };
///
/// trace_target!("app_events", "Position is: x: {}, y: {}", pos.x, pos.y);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! trace_target {
    ($target:expr, $( $args:tt )*) => {
        $crate::internal::trace!(target: $target, $( $args )*);
    }
}

/// Logs a message at the debug level using the specified target.
///
/// This macro logs the message using the specified target. In the most
/// scenarios, the log message should just use the default target, which is the
/// module path of the location of the log request. See [`debug!`] which just logs
/// using the default target.
///
/// [`debug!`]: macro.debug.html
///
/// # Examples
///
/// ```
/// use ckb_logger::debug_target;
///
/// # struct Position { x: f32, y: f32 }
/// let pos = Position { x: 3.234, y: -1.223 };
///
/// debug_target!("app_events", "Position is: x: {}, y: {}", pos.x, pos.y);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! debug_target {
    ($target:expr, $( $args:tt )*) => {
        $crate::internal::debug!(target: $target, $( $args )*);
    }
}

/// Logs a message at the info level using the specified target.
///
/// This macro logs the message using the specified target. In the most
/// scenarios, the log message should just use the default target, which is the
/// module path of the location of the log request. See [`info!`] which just logs
/// using the default target.
///
/// [`info!`]: macro.info.html
///
/// # Examples
///
/// ```
/// use ckb_logger::info_target;
///
/// # struct Connection { port: u32, speed: f32 }
/// let conn_info = Connection { port: 40, speed: 3.20 };
///
/// info_target!("connection_events", "Successful connection, port: {}, speed: {}",
///       conn_info.port, conn_info.speed);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! info_target {
    ($target:expr, $( $args:tt )*) => {
        $crate::internal::info!(target: $target, $( $args )*);
    }
}

/// Logs a message at the warn level using the specified target.
///
/// This macro logs the message using the specified target. In the most
/// scenarios, the log message should just use the default target, which is the
/// module path of the location of the log request. See [`warn!`] which just logs
/// using the default target.
///
/// [`warn!`]: macro.warn.html
///
/// # Examples
///
/// ```
/// use ckb_logger::warn_target;
///
/// let warn_description = "Invalid Input";
///
/// warn_target!("input_events", "App received warning: {}", warn_description);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! warn_target {
    ($target:expr, $( $args:tt )*) => {
        $crate::internal::warn!(target: $target, $( $args )*);
    }
}

/// Logs a message at the error level using the specified target.
///
/// This macro logs the message using the specified target. In the most
/// scenarios, the log message should just use the default target, which is the
/// module path of the location of the log request. See [`error!`] which just logs
/// using the default target.
///
/// [`error!`]: macro.error.html
///
/// # Examples
///
/// ```
/// use ckb_logger::error_target;
///
/// let (err_info, port) = ("No connection", 22);
///
/// error_target!("app_events", "App Error: {}, Port: {}", err_info, port);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! error_target {
    ($target:expr, $( $args:tt )*) => {
        $crate::internal::error!(target: $target, $( $args )*);
    }
}

/// Determines if a message logged at the specified level and with the specified target will be logged.
///
/// This can be used to avoid expensive computation of log message arguments if the message would be ignored anyway.
///
/// See also [`log_enabled!`] the version that checks with the default target.
///
/// [`log_enabled!`]: macro.log_enabled.html
///
/// ## Examples
///
/// ```
/// use ckb_logger::Level::Debug;
/// use ckb_logger::{debug_target, log_enabled_target};
///
/// # struct Data { x: u32, y: u32 }
/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
/// if log_enabled_target!("Global", Debug) {
///     let data = expensive_call();
///     debug_target!("Global", "expensive debug data: {} {}", data.x, data.y);
/// }
/// ```
#[macro_export(local_inner_macros)]
macro_rules! log_enabled_target {
    ($target:expr, $level:expr) => {
        $crate::internal::log_enabled!(target: $target, $level);
    };
}