mistralrs_core/utils/
debug.rs

1use candle_core::{Device, DeviceLocation};
2use tracing::level_filters::LevelFilter;
3use tracing_subscriber::EnvFilter;
4
5use crate::DEBUG;
6
7static LOGGER: std::sync::OnceLock<()> = std::sync::OnceLock::new();
8
9/// This should be called to initialize the debug flag and logging.
10/// This should not be called in mistralrs-core code due to Rust usage.
11pub fn initialize_logging() {
12    let is_debug = std::env::var("MISTRALRS_DEBUG")
13        .unwrap_or_default()
14        .contains('1');
15    DEBUG.store(is_debug, std::sync::atomic::Ordering::Relaxed);
16
17    LOGGER.get_or_init(|| {
18        let filter = EnvFilter::builder()
19            .with_default_directive(if is_debug {
20                LevelFilter::DEBUG.into()
21            } else {
22                LevelFilter::INFO.into()
23            })
24            .from_env_lossy();
25        tracing_subscriber::fmt().with_env_filter(filter).init();
26    });
27}
28
29pub(crate) trait DeviceRepr {
30    fn device_pretty_repr(&self) -> String;
31}
32
33impl DeviceRepr for Device {
34    fn device_pretty_repr(&self) -> String {
35        match self.location() {
36            DeviceLocation::Cpu => "cpu".to_string(),
37            DeviceLocation::Cuda { gpu_id } => format!("cuda[{gpu_id}]"),
38            DeviceLocation::Metal { gpu_id } => format!("metal[{gpu_id}]"),
39        }
40    }
41}