Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ impl fmt::Display for LogStorage {
}
}

/// Whether Rustwide's logging system has been initialized.
///
/// Call `init` or `init_with` before capturing logs.
pub fn is_initialized() -> bool {
INITIALIZED.load(Ordering::SeqCst)
}

/// Capture all log messages emitted inside a closure.
///
/// This function will capture all the message the provided closure emitted **in the current
Expand All @@ -254,8 +261,10 @@ impl fmt::Display for LogStorage {
/// [`init`]: fn.init.html
/// [`init_with`]: fn.init_with.html
pub fn capture<R>(storage: &LogStorage, f: impl FnOnce() -> R) -> R {
if !INITIALIZED.load(Ordering::SeqCst) {
panic!("called capture without initializing rustwide::logging");
if !is_initialized() {
panic!(
"Rustwide logging is not initialized; call rustwide::logging::init() or init_with() before capture()"
);
}

let storage = Box::new(storage.clone());
Expand Down
2 changes: 1 addition & 1 deletion tests/logging_not_initialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use log::{LevelFilter, info};
use rustwide::logging::{self, LogStorage};

#[test]
#[should_panic = "called capture without initializing rustwide::logging"]
#[should_panic = "Rustwide logging is not initialized; call rustwide::logging::init() or init_with() before capture()"]
fn test_not_initialized() {
let storage = LogStorage::new(LevelFilter::Info);
logging::capture(&storage, || {
Expand Down