Only pop a log level if one was previously pushed.

This commit is contained in:
Sergio Benitez 2018-01-29 13:25:33 -08:00
parent 3a8d0e8a9a
commit 6de76c51bc
1 changed files with 6 additions and 2 deletions

View File

@ -149,8 +149,9 @@ pub(crate) fn try_init(level: LoggingLevel, verbose: bool) {
}
}
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering};
static PUSHED: AtomicBool = AtomicBool::new(false);
static LAST_LOG_FILTER: AtomicUsize = AtomicUsize::new(filter_to_usize(log::LevelFilter::Off));
const fn filter_to_usize(filter: log::LevelFilter) -> usize {
@ -163,11 +164,14 @@ fn usize_to_filter(num: usize) -> log::LevelFilter {
pub(crate) fn push_max_level(level: LoggingLevel) {
LAST_LOG_FILTER.store(filter_to_usize(log::max_level()), Ordering::Release);
PUSHED.store(true, Ordering::Release);
log::set_max_level(level.max_log_level().to_level_filter());
}
pub(crate) fn pop_max_level() {
log::set_max_level(usize_to_filter(LAST_LOG_FILTER.load(Ordering::Acquire)));
if PUSHED.load(Ordering::Acquire) {
log::set_max_level(usize_to_filter(LAST_LOG_FILTER.load(Ordering::Acquire)));
}
}
#[doc(hidden)]