diff --git a/Cargo.lock b/Cargo.lock index 3e5da2f0c..7d6b2d24e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -622,6 +622,7 @@ version = "0.4.1" dependencies = [ "iceoryx2-pal-concurrency-sync", "log", + "once_cell", "termsize", "tracing", ] diff --git a/doc/release-notes/iceoryx2-unreleased.md b/doc/release-notes/iceoryx2-unreleased.md index ca3ef937b..fce729b4f 100644 --- a/doc/release-notes/iceoryx2-unreleased.md +++ b/doc/release-notes/iceoryx2-unreleased.md @@ -36,6 +36,7 @@ * Rename `NodeEvent` into `WaitEvent` [#390](https://github.com/eclipse-iceoryx/iceoryx2/issues/390) * Bazel support for the Rust crates [#349](https://github.com/eclipse-iceoryx/iceoryx2/issues/349) * Remove ACL dependency [#457](https://github.com/eclipse-iceoryx/iceoryx2/issues/457) +* Read LogLevel from environment variable [#396](https://github.com/eclipse-iceoryx/iceoryx2/issues/396) ### Workflow diff --git a/iceoryx2-bb/log/BUILD.bazel b/iceoryx2-bb/log/BUILD.bazel index 3c452b0a4..143e0be33 100644 --- a/iceoryx2-bb/log/BUILD.bazel +++ b/iceoryx2-bb/log/BUILD.bazel @@ -25,6 +25,7 @@ rust_library( deps = [ "//iceoryx2-pal/concurrency-sync:iceoryx2-pal-concurrency-sync", "@crate_index//:termsize", + "@crate_index//:once_cell", ], ) diff --git a/iceoryx2-bb/log/Cargo.toml b/iceoryx2-bb/log/Cargo.toml index 1e6c88e0f..a937be523 100644 --- a/iceoryx2-bb/log/Cargo.toml +++ b/iceoryx2-bb/log/Cargo.toml @@ -21,3 +21,4 @@ iceoryx2-pal-concurrency-sync = { workspace = true } termsize = { workspace = true } log = { workspace = true, optional = true } tracing = { workspace = true, optional = true } +once_cell = { workspace = true } diff --git a/iceoryx2-bb/log/src/lib.rs b/iceoryx2-bb/log/src/lib.rs index db9e2eef8..ee0021fb7 100644 --- a/iceoryx2-bb/log/src/lib.rs +++ b/iceoryx2-bb/log/src/lib.rs @@ -151,6 +151,8 @@ use std::{ }; use logger::Logger; +use once_cell::sync::Lazy; +use std::env; #[cfg(feature = "logger_tracing")] static DEFAULT_LOGGER: logger::tracing::Logger = logger::tracing::Logger::new(); @@ -159,13 +161,19 @@ static DEFAULT_LOGGER: logger::tracing::Logger = logger::tracing::Logger::new(); static DEFAULT_LOGGER: logger::log::Logger = logger::log::Logger::new(); #[cfg(not(any(feature = "logger_log", feature = "logger_tracing")))] -static DEFAULT_LOGGER: logger::console::Logger = logger::console::Logger::new(); +pub static DEFAULT_LOGGER: Lazy = Lazy::new(logger::console::Logger::new); -const DEFAULT_LOG_LEVEL: u8 = LogLevel::Info as u8; +const DEFAULT_LOG_LEVEL: LogLevel = LogLevel::Info; +pub static ENV_LOG_LEVEL: Lazy = Lazy::new(|| { + env::var("IOX2_LOG_LEVEL") + .map(|log_level| LogLevel::from_str_fuzzy(&log_level)) + .unwrap_or(LogLevel::Info) +}); static mut LOGGER: Option<&'static dyn logger::Logger> = None; -static LOG_LEVEL: IoxAtomicU8 = IoxAtomicU8::new(DEFAULT_LOG_LEVEL); -static INIT: Once = Once::new(); +static LOG_LEVEL: IoxAtomicU8 = IoxAtomicU8::new(DEFAULT_LOG_LEVEL as u8); +static INIT_LOGGER: Once = Once::new(); +static INIT_LOG_LEVEL: Once = Once::new(); /// Describes the log level. #[repr(u8)] @@ -179,14 +187,38 @@ pub enum LogLevel { Fatal = 5, } +impl LogLevel { + fn from_str_fuzzy(s: &str) -> LogLevel { + match s.to_lowercase().as_str() { + "trace" => LogLevel::Trace, + "tebug" => LogLevel::Debug, + "info" => LogLevel::Info, + "warn" => LogLevel::Warn, + "error" => LogLevel::Error, + "fatal" => LogLevel::Fatal, + _ => { + println!("Error: you are using unknown logging level {:?}", s); + println!("Warning: setting log level as : Info"); + DEFAULT_LOG_LEVEL + } + } + } +} + /// Sets the current log level. This is ignored for external frameworks like `log` or `tracing`. /// Here you have to use the log-level settings of that framework. pub fn set_log_level(v: LogLevel) { + INIT_LOG_LEVEL.call_once(|| { + LOG_LEVEL.store(*ENV_LOG_LEVEL as u8, Ordering::Relaxed); + }); LOG_LEVEL.store(v as u8, Ordering::Relaxed); } /// Returns the current log level pub fn get_log_level() -> u8 { + INIT_LOG_LEVEL.call_once(|| { + LOG_LEVEL.store(*ENV_LOG_LEVEL as u8, Ordering::Relaxed); + }); LOG_LEVEL.load(Ordering::Relaxed) } @@ -194,20 +226,18 @@ pub fn get_log_level() -> u8 { /// [`Logger`] is already set it returns false and does not update it. pub fn set_logger(value: &'static T) -> bool { let mut set_logger_success = false; - INIT.call_once(|| { + INIT_LOGGER.call_once(|| { unsafe { LOGGER = Some(value) }; set_logger_success = true; }); - set_logger_success } /// Returns a reference to the [`Logger`]. pub fn get_logger() -> &'static dyn Logger { - INIT.call_once(|| { - unsafe { LOGGER = Some(&DEFAULT_LOGGER) }; + INIT_LOGGER.call_once(|| { + unsafe { LOGGER = Some(&*DEFAULT_LOGGER) }; }); - unsafe { *LOGGER.as_ref().unwrap() } }