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
// Copyright 2017 Dmytro Milinevskyi <dmilinevskyi@gmail.com>

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// The main log entry.
///
/// Prepares and emits a log record if requested log [level](levels/enum.LogLevel.html) is greater or equal
/// according to the log level.
///
/// If log level is not specified then the log is emitted unconditionally.
///
/// If, for example, the hierarchy rules deduce that the log level at the current position is
/// [WARN](levels/enum.LogLevel.html) then the logs for
/// the levels [WARN](levels/enum.LogLevel.html) and above([ERROR](levels/enum.LogLevel.html) and
/// [CRITICAL](levels/enum.LogLevel.html)) are emitted.
/// The logs for the levels below [WARN](levels/enum.LogLevel.html)
/// (such as [NOTICE](levels/enum.LogLevel.html), [INFO](levels/enum.LogLevel.html), etc.) are dropped.
///
/// See documentation for the [wp_get_level](macro.wp_get_level.html)
/// for more details on the log level hierarchy.
///
/// # Example
///
/// ```rust
/// #[macro_use]
/// extern crate woodpecker;
/// use woodpecker as wp;
///
/// use std::sync::{Arc, Mutex};
/// use std::ops::Deref;
///
/// fn main() {
///     wp_init!();
///     wp_set_level!(wp::LogLevel::CRITICAL).unwrap();
///
///     let msg = "I'm always visible";
///
///     let out = Arc::new(Mutex::new(String::new()));
///     {
///         let out = out.clone();
///         wp_register_handler!(Box::new(move |record| {
///             out.lock().unwrap().push_str(record.msg().deref());
///         }));
///
///         log!(">{}<", msg);
///         warn!("foo");
///         in_trace!({
///             log!("Not seen though");
///         });
///     }
///
///     if cfg!(feature = "test-thread-log") {
///         wp::sync();
///     }
///
///     assert_eq!(*out.lock().unwrap(), format!(">{}<", msg));
/// }
///
/// ```
#[macro_export]
macro_rules! log {
    ($level:expr => $($arg:tt)*) => {{
        use $crate::record::imp::RecordMeta;
        static RECORD: RecordMeta = RecordMeta {
            level: $level,
            module: this_module!(),
            file: file!(),
            line: line!(),
        };
        if $crate::global::has_loggers() {
            let path = this_file!();
            let root = $crate::logger::ROOT.read();
            if root.get_level(path, line!()) <= $level {
                root.log(&RECORD, format_args!($($arg)*));
            }
        } else {
            if $crate::global::get_level() <= $level {
                __wp_read_root!(log(&RECORD, format_args!($($arg)*)));
            }
        }
    }};

    ($($arg:tt)*) => {{
        use $crate::record::imp::RecordMeta;
        static RECORD: RecordMeta = RecordMeta {
            level: $crate::LogLevel::LOG,
            module: this_module!(),
            file: file!(),
            line: line!(),
        };
        __wp_read_root!(log(&RECORD, format_args!($($arg)*)));
    }};
}

/// Produces log record for the `trace` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! trace {
    ($($arg:tt)*) => {
        log!($crate::LogLevel::TRACE => $($arg)*);
    };
}

/// Executes the code only for the `trace` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! in_trace {
    ($block:block) => {
        if wp_get_level!() <= $crate::LogLevel::TRACE {
            $block;
        }
    }
}

/// Produces log record for the `debug` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! debug {
    ($($arg:tt)*) => {
        log!($crate::LogLevel::DEBUG  => $($arg)*);
    };
}

/// Executes the code only for the `debug` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! in_debug {
    ($block:block) => {
        if wp_get_level!() <= $crate::LogLevel::DEBUG {
            $block;
        }
    }
}

/// Produces log for the `verbose` level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! verbose {
    ($($arg:tt)*) => {
        log!($crate::LogLevel::VERBOSE => $($arg)*);
    };
}

/// Executes the code only for the `verbose` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! in_verbose {
    ($block:block) => {
        if wp_get_level!() <= $crate::LogLevel::VERBOSE {
            $block;
        }
    }
}

/// Produces log record for the `info` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! info {
    ($($arg:tt)*) => {
        log!($crate::LogLevel::INFO => $($arg)*);
    };
}

/// Executes the code only for the `info` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! in_info {
    ($block:block) => {
        if wp_get_level!() <= $crate::LogLevel::INFO {
            $block;
        }
    }
}

/// Produces log record for the `notice` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! notice {
    ($($arg:tt)*) => {
        log!($crate::LogLevel::NOTICE => $($arg)*);
    };
}

/// Executes the code only for the `notice` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! in_notice {
    ($block:block) => {
        if wp_get_level!() <= $crate::LogLevel::NOTICE {
            $block;
        }
    }
}

/// Produces log record for the `warn` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! warn {
    ($($arg:tt)*) => {
        log!($crate::LogLevel::WARN => $($arg)*);
    };
}

/// Executes the code only for the `warn` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! in_warn {
    ($block:block) => {
        if wp_get_level!() <= $crate::LogLevel::WARN {
            $block;
        }
    }
}

/// Produces log record for the `error` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! error {
    ($($arg:tt)*) => {
        log!($crate::LogLevel::ERROR => $($arg)*);
    };
}

/// Executes the code only for the `error` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! in_error {
    ($block:block) => {
        if wp_get_level!() <= $crate::LogLevel::ERROR {
            $block;
        }
    }
}

/// Produces log record for the `critical` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! critical {
    ($($arg:tt)*) => {
        log!($crate::LogLevel::CRITICAL => $($arg)*);
    };
}

/// Executes the code only for the `critical` log level.
///
/// See the [log](macro.log.html) macro for the details.
#[macro_export]
macro_rules! in_critical {
    ($block:block) => {
        if wp_get_level!() <= $crate::LogLevel::CRITICAL {
            $block;
        }
    }
}