Skip to content

Commit

Permalink
Make logger setup callable twice, to simplify life for callers
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 1, 2022
1 parent 2844863 commit ad99823
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions abstutil/src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::sync::Once;

static SETUP: Once = Once::new();

/// ## On native: uses env_log
///
/// You can adjust the log level without recompiling with the RUST_LOG env variable.
Expand Down Expand Up @@ -35,15 +39,19 @@
/// ```
///
/// ## On web: uses console_log
///
/// Can be called multiple times
pub fn setup() {
#[cfg(target_arch = "wasm32")]
{
console_log::init_with_level(log::Level::Info).unwrap();
}
SETUP.call_once(|| {
#[cfg(target_arch = "wasm32")]
{
console_log::init_with_level(log::Level::Info).unwrap();
}

#[cfg(not(target_arch = "wasm32"))]
{
use env_logger::{Builder, Env};
Builder::from_env(Env::default().default_filter_or("info")).init();
}
#[cfg(not(target_arch = "wasm32"))]
{
use env_logger::{Builder, Env};
Builder::from_env(Env::default().default_filter_or("info")).init();
}
});
}

0 comments on commit ad99823

Please sign in to comment.