third_party_rust_log/test_max_level_features/main.rs

74 lines
1.6 KiB
Rust
Raw Normal View History

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};
use log::{Level, LevelFilter, Log, Record, Metadata};
2015-10-02 19:11:46 +00:00
#[cfg(feature = "std")]
use log::set_boxed_logger;
#[cfg(not(feature = "std"))]
fn set_boxed_logger(logger: Box<Log>) -> Result<(), log::SetLoggerError> {
log::set_logger(Box::leak(logger))
2016-01-12 19:33:20 +00:00
}
2015-10-02 19:11:46 +00:00
struct State {
last_log: Mutex<Option<Level>>,
2015-10-02 19:11:46 +00:00
}
struct Logger(Arc<State>);
impl Log for Logger {
fn enabled(&self, _: &Metadata) -> bool {
2015-10-02 19:11:46 +00:00
true
}
fn log(&self, record: &Record) {
2015-10-02 19:11:46 +00:00
*self.0.last_log.lock().unwrap() = Some(record.level());
}
fn flush(&self) {}
2015-10-02 19:11:46 +00:00
}
fn main() {
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
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
}
fn test(a: &State, filter: LevelFilter) {
log::set_max_level(filter);
2015-10-02 19:11:46 +00:00
error!("");
last(&a, t(Level::Error, filter));
2015-10-02 19:11:46 +00:00
warn!("");
last(&a, t(Level::Warn, filter));
2015-10-02 19:11:46 +00:00
info!("");
last(&a, t(Level::Info, filter));
2015-10-02 19:11:46 +00:00
debug!("");
if cfg!(debug_assertions) {
last(&a, t(Level::Debug, filter));
2015-10-02 19:11:46 +00:00
} else {
last(&a, None);
}
trace!("");
last(&a, None);
fn t(lvl: Level, filter: LevelFilter) -> Option<Level> {
2015-10-02 19:11:46 +00:00
if lvl <= filter {Some(lvl)} else {None}
}
}
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;
}