From c7f116ec261ca0ffe79c527aca14dbcdcb8e22f3 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 15 Jan 2016 20:52:17 -0800 Subject: [PATCH 1/3] Mention that set_logger requires `use_std`. --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index a6cf8eccd..a49d8c978 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -643,6 +643,8 @@ pub fn max_log_level() -> LogLevelFilter { /// This function does not typically need to be called manually. Logger /// implementations should provide an initialization method that calls /// `set_logger` internally. +/// +/// Requires the `use_std` feature (enabled by default). #[cfg(feature = "use_std")] pub fn set_logger(make_logger: M) -> Result<(), SetLoggerError> where M: FnOnce(MaxLogLevelFilter) -> Box { From ec82f77c726dc6bd49bddcc3ddc36d7749d343be Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 15 Jan 2016 20:59:21 -0800 Subject: [PATCH 2/3] Drop Option wrapper around LOGGER Avoids an unwrap --- src/lib.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a49d8c978..6ff01c14b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -228,7 +228,8 @@ mod macros; // refcount does not necessarily monotonically decrease at this point, as new // log calls still increment and decrement it, but the interval in between is // small enough that the wait is really just for the active log calls to finish. -static mut LOGGER: Option<*const Log> = None; + +static mut LOGGER: *const Log = &NopLogger; static STATE: AtomicUsize = ATOMIC_USIZE_INIT; static REFCOUNT: AtomicUsize = ATOMIC_USIZE_INIT; @@ -557,6 +558,15 @@ pub trait Log: Sync+Send { fn log(&self, record: &LogRecord); } +// Just used as a dummy initial value for LOGGER +struct NopLogger; + +impl Log for NopLogger { + fn enabled(&self, _: &LogMetadata) -> bool { false } + + fn log(&self, _: &LogRecord) {} +} + /// The location of a log message. /// /// # Warning @@ -697,7 +707,7 @@ pub unsafe fn set_logger_raw(make_logger: M) -> Result<(), SetLoggerError> return Err(SetLoggerError(())); } - LOGGER = Some(make_logger(MaxLogLevelFilter(()))); + LOGGER = make_logger(MaxLogLevelFilter(())); STATE.store(INITIALIZED, Ordering::SeqCst); Ok(()) } @@ -731,7 +741,11 @@ pub fn shutdown_logger_raw() -> Result<*const Log, ShutdownLoggerError> { // FIXME add a sleep here when it doesn't involve timers } - Ok(unsafe { LOGGER.unwrap() }) + unsafe { + let logger = LOGGER; + LOGGER = &NopLogger; + Ok(logger) + } } /// The type returned by `set_logger` if `set_logger` has already been called. @@ -834,7 +848,7 @@ fn logger() -> Option { REFCOUNT.fetch_sub(1, Ordering::SeqCst); None } else { - Some(LoggerGuard(unsafe { &*LOGGER.unwrap() })) + Some(LoggerGuard(unsafe { &*LOGGER })) } } From 3cdcbd1fa5d2ca143685ffe70a9a6f93204df52a Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 15 Jan 2016 21:13:34 -0800 Subject: [PATCH 3/3] Mention use_std dependency in log_panics docs --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6ff01c14b..86162e049 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -789,7 +789,7 @@ impl error::Error for ShutdownLoggerError { /// The format is the same as the default panic handler. The reporting module is /// `log::panic`. /// -/// Requires the `nightly` feature. +/// Requires the `use_std` (enabled by default) and `nightly` features. #[cfg(all(feature = "nightly", feature = "use_std"))] pub fn log_panics() { std::panic::set_handler(panic::log);