Skip to content

Commit

Permalink
[eclipse-iceoryx#396] Read and set LogLevel from environment variable
Browse files Browse the repository at this point in the history
Signed-off-by: Ziad Mostafa <[email protected]>
  • Loading branch information
zmostafa committed Oct 30, 2024
1 parent e5b44a9 commit e2e818c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions doc/release-notes/iceoryx2-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions iceoryx2-bb/log/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rust_library(
deps = [
"//iceoryx2-pal/concurrency-sync:iceoryx2-pal-concurrency-sync",
"@crate_index//:termsize",
"@crate_index//:once_cell",
],
)

Expand Down
1 change: 1 addition & 0 deletions iceoryx2-bb/log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
48 changes: 39 additions & 9 deletions iceoryx2-bb/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<logger::console::Logger> = 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<LogLevel> = 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)]
Expand All @@ -179,35 +187,57 @@ 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)
}

/// Sets the [`Logger`]. Can be only called once at the beginning of the program. If the
/// [`Logger`] is already set it returns false and does not update it.
pub fn set_logger<T: logger::Logger + 'static>(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() }
}

Expand Down

0 comments on commit e2e818c

Please sign in to comment.