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            // disable info (and below) logs from symphonia
26            .add_directive("symphonia=warn".parse().unwrap());
27        tracing_subscriber::fmt().with_env_filter(filter).init();
28    });
29}
30
31pub(crate) trait DeviceRepr {
32    fn device_pretty_repr(&self) -> String;
33}
34
35impl DeviceRepr for Device {
36    fn device_pretty_repr(&self) -> String {
37        match self.location() {
38            DeviceLocation::Cpu => "cpu".to_string(),
39            DeviceLocation::Cuda { gpu_id } => format!("cuda[{gpu_id}]"),
40            DeviceLocation::Metal { gpu_id } => format!("metal[{gpu_id}]"),
41        }
42    }
43}