mirror of
https://gitee.com/openharmony/third_party_rust_log
synced 2024-11-27 09:50:34 +00:00
Merge pull request #214 from sfackler/pub-logger
Replace log and enabled functions with logger
This commit is contained in:
commit
7e37f6fb89
35
src/lib.rs
35
src/lib.rs
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user