From ee3b1100547c0adb72eb237ac4d7dc65af572f5e Mon Sep 17 00:00:00 2001 From: Charlotte McElwain Date: Sun, 12 Nov 2023 16:17:44 -0600 Subject: [PATCH] Fixes for tracing on Windows. --- td-rs-base/src/lib.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/td-rs-base/src/lib.rs b/td-rs-base/src/lib.rs index 2acaefb..8d86e17 100644 --- a/td-rs-base/src/lib.rs +++ b/td-rs-base/src/lib.rs @@ -1,16 +1,21 @@ #![feature(associated_type_defaults)] #![feature(min_specialization)] +#![feature(lazy_cell)] use std::cell::OnceCell; +use std::collections::HashMap; use std::ffi; use std::fmt::Formatter; use std::ops::Index; use std::pin::Pin; +use std::sync::{LazyLock, Mutex}; pub use param::*; #[cfg(feature = "python")] pub use py::*; +pub static HASHMAP: LazyLock>> = LazyLock::new(|| Mutex::new(HashMap::new())); + pub mod chop; pub mod cxx; pub mod dat; @@ -561,12 +566,30 @@ pub fn op_init() { { use tracing_subscriber::fmt; use tracing_subscriber::layer::SubscriberExt; - use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::EnvFilter; - - tracing_subscriber::registry() - .with(fmt::layer()) + use tracing_subscriber::util::{SubscriberInitExt, TryInitError}; + + let fmt_layer = if cfg!(target_os = "windows") { + let mut f = fmt::layer(); + f.set_ansi(false); + f + } else { + fmt::layer() + }; + let init = tracing_subscriber::registry() + .with(fmt_layer) .with(EnvFilter::from_default_env()) - .init(); + .try_init(); + match init { + Ok(_) => {} + Err(err) => { + match err { + TryInitError { .. } => {} + _ => { + eprintln!("Failed to initialize tracing: {}", err); + } + } + } + } } }