Merge pull request #214 from sfackler/pub-logger

Replace log and enabled functions with logger
This commit is contained in:
Steven Fackler 2017-08-13 16:35:44 -07:00 committed by GitHub
commit 7e37f6fb89
2 changed files with 14 additions and 27 deletions

View File

@ -1211,32 +1211,17 @@ impl error::Error for ParseLevelError {
}
}
fn logger() -> Option<&'static Log> {
if STATE.load(Ordering::SeqCst) != INITIALIZED {
None
} else {
Some(unsafe { &*LOGGER })
}
}
/// Determines if `Record`s with the provided metadata would be logged or not.
/// Returns a reference to the logger.
///
/// This can be used to avoid expensive computation of log data that would just
/// be discarded. It is called by the `log_enabled!()` macro.
pub fn enabled(metadata: &Metadata) -> bool {
if let Some(logger) = logger() {
logger.enabled(metadata)
} else {
false
}
}
/// Logs the `Record` with the registered logger.
///
/// It is called by the `log!`, `error!`, `warn!`, etc macros.
pub fn log(record: &Record) {
if let Some(logger) = logger() {
logger.log(record)
/// If a logger has not been set, a no-op implementation is returned.
pub fn logger() -> &'static Log {
unsafe {
if STATE.load(Ordering::SeqCst) != INITIALIZED {
static NOP: NopLogger = NopLogger;
&NOP
} else {
&*LOGGER
}
}
}

View File

@ -42,7 +42,8 @@ macro_rules! log {
(target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
let lvl = $lvl;
if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
$crate::log(
$crate::Log::log(
$crate::logger(),
&$crate::RecordBuilder::new()
.args(format_args!($($arg)+))
.level(lvl)
@ -251,7 +252,8 @@ macro_rules! log_enabled {
(target: $target:expr, $lvl:expr) => ({
let lvl = $lvl;
lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() &&
$crate::enabled(
$crate::Log::enabled(
$crate::logger(),
&$crate::MetadataBuilder::new()
.level(lvl)
.target($target)