Skip to content

Commit

Permalink
fix(web): make sure to not init logger twice
Browse files Browse the repository at this point in the history
  • Loading branch information
CBenoit committed Dec 21, 2023
1 parent 9aa0e15 commit fded9f6
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions crates/ironrdp-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@ mod websocket;

use wasm_bindgen::prelude::*;

// NOTE: #[wasm_bindgen(start)] didn’t work last time I tried
#[wasm_bindgen]
pub fn ironrdp_init(log_level: &str) {
use tracing::Level;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::fmt::time::UtcTime;
use tracing_subscriber::prelude::*;
use tracing_web::MakeConsoleWriter;

// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
Expand All @@ -39,7 +32,20 @@ pub fn ironrdp_init(log_level: &str) {
#[cfg(feature = "panic_hook")]
console_error_panic_hook::set_once();

if let Ok(level) = log_level.parse::<Level>() {
if let Ok(level) = log_level.parse::<tracing::Level>() {
set_logger_once(level);
}
}

fn set_logger_once(level: tracing::Level) {
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::fmt::time::UtcTime;
use tracing_subscriber::prelude::*;
use tracing_web::MakeConsoleWriter;

static INIT: std::sync::Once = std::sync::Once::new();

INIT.call_once(|| {
let fmt_layer = tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_timer(UtcTime::rfc_3339()) // std::time is not available in browsers
Expand All @@ -50,7 +56,7 @@ pub fn ironrdp_init(log_level: &str) {
tracing_subscriber::registry().with(fmt_layer).with(level_filter).init();

debug!("IronRDP is ready");
}
})
}

#[wasm_bindgen]
Expand Down

0 comments on commit fded9f6

Please sign in to comment.