Skip to content

Instantly share code, notes, and snippets.

@UsairimIsani
Created December 15, 2020 11:30
Show Gist options
  • Save UsairimIsani/db6a6c951e82ecb88b3bf632a3ae674a to your computer and use it in GitHub Desktop.
Save UsairimIsani/db6a6c951e82ecb88b3bf632a3ae674a to your computer and use it in GitHub Desktop.
// For tests, we only initialize logging once.
#[cfg(test)]
use std::sync::Once;
#[cfg(test)]
static LOGGER_INIT: Once = Once::new();
// Rust runs the tests concurrently, so unless we synchronize logging access
// it will crash when attempting to run `cargo test` with some logging facilities.
#[cfg(test)]
pub fn ensure_env_logger_initialized() {
use std::io::Write;
LOGGER_INIT.call_once(|| {
let mut builder = env_logger::Builder::from_default_env();
builder
.format(|buf, record| writeln!(buf, "[{}] - {}", record.level(), record.args()))
.init();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment