use arc_writer::ArcWriter;
use once_cell::sync::Lazy;
use std::sync::{Arc, Mutex};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
static JSON_LOGS: Lazy<ArcWriter<Vec<u8>>> = Lazy::new(|| ArcWriter::new(Vec::new()));
pub fn init_logging() -> anyhow::Result<()> {
let json_writer = JSON_LOGS.clone();
tracing_subscriber::registry()
.with(fmt::layer().compact().with_writer(std::io::stderr))
.with(fmt::layer().json().with_writer(move || json_writer.clone()))
.with(
EnvFilter::builder()
.with_default_directive("geph=debug".parse()?)
.from_env_lossy(),
)
.init();
Ok(())
}
pub fn get_json_logs() -> String {
let logs = JSON_LOGS.lock();
let log_string = String::from_utf8_lossy(&logs);
log_string.to_string()
}