Skip to content

Commit

Permalink
Merge pull request #71 from sfackler/master
Browse files Browse the repository at this point in the history
Couple minor tweaks to stuff from use_std PR
  • Loading branch information
alexcrichton committed Jan 16, 2016
2 parents cb09c6e + 3cdcbd1 commit 3cebcbf
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -643,6 +653,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<M>(make_logger: M) -> Result<(), SetLoggerError>
where M: FnOnce(MaxLogLevelFilter) -> Box<Log> {
Expand Down Expand Up @@ -695,7 +707,7 @@ pub unsafe fn set_logger_raw<M>(make_logger: M) -> Result<(), SetLoggerError>
return Err(SetLoggerError(()));
}

LOGGER = Some(make_logger(MaxLogLevelFilter(())));
LOGGER = make_logger(MaxLogLevelFilter(()));
STATE.store(INITIALIZED, Ordering::SeqCst);
Ok(())
}
Expand Down Expand Up @@ -729,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.
Expand Down Expand Up @@ -773,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);
Expand Down Expand Up @@ -832,7 +848,7 @@ fn logger() -> Option<LoggerGuard> {
REFCOUNT.fetch_sub(1, Ordering::SeqCst);
None
} else {
Some(LoggerGuard(unsafe { &*LOGGER.unwrap() }))
Some(LoggerGuard(unsafe { &*LOGGER }))
}
}

Expand Down

0 comments on commit 3cebcbf

Please sign in to comment.