datex_core/
logger.rs

1use std::sync::Once;
2
3use cfg_if::cfg_if;
4
5static INIT: Once = Once::new();
6
7/// Initializes the logger with debug mode, logging all messages including debug messages.
8pub fn init_logger_debug() {
9    INIT.call_once(|| {
10        init(true);
11    });
12}
13
14/// Initializes the logger with default mode, only logging errors and above.
15pub fn init_logger() {
16    INIT.call_once(|| {
17        init(false);
18    });
19}
20
21cfg_if! {
22    if #[cfg(feature = "flexi_logger")] {
23        use flexi_logger;
24        fn init(debug: bool) {
25            let env = if debug {
26                "datex_core=trace,r#mod=trace"
27            } else {
28                "datex_core=error,r#mod=error"
29            };
30            flexi_logger::Logger::try_with_env_or_str(env).expect("Failed to initialize logger")
31                .start()
32                .expect("Failed to start logger");
33        }
34    }
35
36    else if #[cfg(feature = "wasm_logger")]
37    {
38        fn init(debug: bool) {
39            use log::Level;
40            use console_error_panic_hook;
41
42            console_log::init_with_level(
43                if debug {
44                    Level::Debug
45                } else {
46                    Level::Error
47                },
48            ).expect("Failed to initialize logger");
49            console_error_panic_hook::set_once();
50        }
51    }
52
53    else if #[cfg(feature = "env_logger")] {
54        use env_logger;
55        fn init(debug: bool) {
56            env_logger::init();
57            info!("Logger initialized! (Using env_logger)");
58        }
59    }
60
61    else if #[cfg(feature = "esp_logger")] {
62        use esp_idf_svc::log::EspLogger;
63        fn init(debug: bool) {
64            EspLogger::initialize_default();
65        }
66    }
67
68    else {
69        fn init(debug: bool) {
70            println!("No logger enabled. Logs will not be recorded.");
71        }
72    }
73}