Macro woodpecker::log [] [src]

macro_rules! log {
    ($level:expr => $($arg:tt)*) => { ... };
    ($($arg:tt)*) => { ... };
}

The main log entry.

Prepares and emits a log record if requested log level 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 then the logs for the levels WARN and above(ERROR and CRITICAL) are emitted. The logs for the levels below WARN (such as NOTICE, INFO, etc.) are dropped.

See documentation for the wp_get_level for more details on the log level hierarchy.

Example

#[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));
}