use std::sync::Arc;
use proxima_telemetry::error::Error;
use proxima_telemetry::export::Formatter;
use proxima_telemetry::recorder::Recorder;
#[cfg(not(feature = "tracing-init"))]
use proxima_telemetry::export::install_console_recorder_with;
#[cfg(feature = "tracing-init")]
use tracing_subscriber::EnvFilter;
#[cfg(feature = "tracing-init")]
use tracing_subscriber::layer::SubscriberExt;
#[cfg(feature = "tracing-init")]
use proxima_telemetry::export::{install_console_logging_with, set_default_recorder};
#[cfg(feature = "tracing-init")]
use proxima_telemetry::tracing_bridge::TracingLayer;
#[derive(Debug, Clone, Copy, Default)]
pub enum LogFormat {
#[default]
Human,
Json,
}
impl LogFormat {
const fn to_formatter(self) -> Formatter {
match self {
Self::Human => Formatter::Text,
Self::Json => Formatter::Json,
}
}
}
pub fn init_telemetry() -> Result<Arc<Recorder>, Error> {
init_telemetry_with(LogFormat::Human)
}
#[cfg(feature = "tracing-init")]
pub fn init_telemetry_with(format: LogFormat) -> Result<Arc<Recorder>, Error> {
install_console_logging_with(format.to_formatter())
}
#[cfg(not(feature = "tracing-init"))]
pub fn init_telemetry_with(format: LogFormat) -> Result<Arc<Recorder>, Error> {
install_console_recorder_with(format.to_formatter())
}
#[deprecated(note = "renamed: use proxima::init_telemetry_with (or init_telemetry() for defaults)")]
pub fn init_tracing_default(format: LogFormat) -> Result<Arc<Recorder>, Error> {
init_telemetry_with(format)
}
#[cfg(feature = "tracing-init")]
pub fn init_tracing(recorder: Arc<Recorder>, _format: LogFormat) -> Result<(), Error> {
set_default_recorder(Arc::clone(&recorder));
let filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn,proxima=info"));
let layer = TracingLayer::new(Arc::clone(&recorder));
let subscriber = tracing_subscriber::registry().with(filter).with(layer);
tracing::subscriber::set_global_default(subscriber)
.map_err(|error| Error::GlobalSubscriberAlreadySet(error.to_string()))?;
let pump = Arc::clone(&recorder);
std::thread::Builder::new()
.name("proxima-tracing-init-drain".to_string())
.spawn(move || pump.run_drain_loop())
.map_err(|error| Error::ThreadSpawn(error.to_string()))?;
Ok(())
}
#[cfg(not(feature = "tracing-init"))]
pub fn init_tracing(_recorder: Arc<Recorder>, _format: LogFormat) -> Result<(), Error> {
eprintln!(
"proxima::init_tracing called without the `tracing-init` feature; enable it (--features tracing-init) to install the bridge"
);
Err(Error::InvalidInput)
}