2017-06-14 16:26:09 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2015-10-02 19:11:46 +00:00
|
|
|
|
|
|
|
use std::sync::{Arc, Mutex};
|
2017-05-21 19:46:12 +00:00
|
|
|
use log::{Level, LevelFilter, Log, Record, Metadata};
|
2015-10-02 19:11:46 +00:00
|
|
|
|
2017-12-06 04:53:30 +00:00
|
|
|
#[cfg(feature = "std")]
|
2017-10-01 04:02:37 +00:00
|
|
|
use log::set_boxed_logger;
|
2017-12-06 04:53:30 +00:00
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
fn set_boxed_logger(logger: Box<Log>) -> Result<(), log::SetLoggerError> {
|
2019-12-10 10:36:36 +00:00
|
|
|
log::set_logger(Box::leak(logger))
|
2016-01-12 19:33:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 19:11:46 +00:00
|
|
|
struct State {
|
2017-05-21 19:46:12 +00:00
|
|
|
last_log: Mutex<Option<Level>>,
|
2015-10-02 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Logger(Arc<State>);
|
|
|
|
|
|
|
|
impl Log for Logger {
|
2017-05-21 19:46:12 +00:00
|
|
|
fn enabled(&self, _: &Metadata) -> bool {
|
2015-10-02 19:11:46 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2017-05-21 19:46:12 +00:00
|
|
|
fn log(&self, record: &Record) {
|
2015-10-02 19:11:46 +00:00
|
|
|
*self.0.last_log.lock().unwrap() = Some(record.level());
|
|
|
|
}
|
2017-06-20 17:30:49 +00:00
|
|
|
|
|
|
|
fn flush(&self) {}
|
2015-10-02 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2017-12-06 04:53:30 +00:00
|
|
|
let me = Arc::new(State { last_log: Mutex::new(None) });
|
|
|
|
let a = me.clone();
|
|
|
|
set_boxed_logger(Box::new(Logger(me))).unwrap();
|
2015-10-02 19:11:46 +00:00
|
|
|
|
2017-05-21 19:46:12 +00:00
|
|
|
test(&a, LevelFilter::Off);
|
|
|
|
test(&a, LevelFilter::Error);
|
|
|
|
test(&a, LevelFilter::Warn);
|
|
|
|
test(&a, LevelFilter::Info);
|
|
|
|
test(&a, LevelFilter::Debug);
|
|
|
|
test(&a, LevelFilter::Trace);
|
2015-10-02 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 19:46:12 +00:00
|
|
|
fn test(a: &State, filter: LevelFilter) {
|
2017-12-06 04:53:30 +00:00
|
|
|
log::set_max_level(filter);
|
2015-10-02 19:11:46 +00:00
|
|
|
error!("");
|
2017-05-21 19:46:12 +00:00
|
|
|
last(&a, t(Level::Error, filter));
|
2015-10-02 19:11:46 +00:00
|
|
|
warn!("");
|
2017-05-21 19:46:12 +00:00
|
|
|
last(&a, t(Level::Warn, filter));
|
2015-10-02 19:11:46 +00:00
|
|
|
info!("");
|
2017-05-21 19:46:12 +00:00
|
|
|
last(&a, t(Level::Info, filter));
|
2015-10-02 19:11:46 +00:00
|
|
|
|
|
|
|
debug!("");
|
|
|
|
if cfg!(debug_assertions) {
|
2017-05-21 19:46:12 +00:00
|
|
|
last(&a, t(Level::Debug, filter));
|
2015-10-02 19:11:46 +00:00
|
|
|
} else {
|
|
|
|
last(&a, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
trace!("");
|
|
|
|
last(&a, None);
|
|
|
|
|
2017-05-21 19:46:12 +00:00
|
|
|
fn t(lvl: Level, filter: LevelFilter) -> Option<Level> {
|
2015-10-02 19:11:46 +00:00
|
|
|
if lvl <= filter {Some(lvl)} else {None}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 19:46:12 +00:00
|
|
|
fn last(state: &State, expected: Option<Level>) {
|
2015-10-02 19:11:46 +00:00
|
|
|
let mut lvl = state.last_log.lock().unwrap();
|
|
|
|
assert_eq!(*lvl, expected);
|
|
|
|
*lvl = None;
|
|
|
|
}
|