Macro woodpecker::wp_set_formatter [] [src]

macro_rules! wp_set_formatter {
    ($formatter:expr) => { ... };
}

Sets a log record formatter.

A default formatter is used if not set explicitly.

The formatter function takes a log record as an argument and returns a string that will be used as a return value of Record::formatted function.

See the definition of the Formatter type for the details.

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_formatter!(Box::new(|record| {
        format!("{}:{}", record.level(), record.msg())
    }));
    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.formatted().deref());
        }));

        warn!("foo");
    }
    if cfg!(feature = "test-thread-log") {
        wp::sync();
    }
    assert_eq!(*out.lock().unwrap(), "WARN:foo".to_string());
}