Skip to content

Commit

Permalink
[#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 Nov 8, 2024
1 parent 810fb87 commit 307b151
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 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 @@ -40,6 +40,7 @@
* 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)
* Publish Subscribe Header contains number of elements contained in a `Sample` [#498](https://github.com/eclipse-iceoryx/iceoryx2/issues/498)
* 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 @@ -37,5 +37,6 @@ rust_library(
deps = [
"//iceoryx2-pal/concurrency-sync:iceoryx2-pal-concurrency-sync",
"@crate_index//:termsize",
"@crate_index//:once_cell",
],
)
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 }
47 changes: 40 additions & 7 deletions iceoryx2-bb/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,30 @@ use std::{
sync::{atomic::Ordering, Once},
};

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();

#[cfg(feature = "logger_log")]
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 Log> = None;
static LOG_LEVEL: IoxAtomicU8 = IoxAtomicU8::new(DEFAULT_LOG_LEVEL);
static INIT: Once = Once::new();
static mut LOGGER: Option<&'static dyn logger::Logger> = None;
static LOG_LEVEL: IoxAtomicU8 = IoxAtomicU8::new(DEFAULT_LOG_LEVEL as u8);
static INIT_LOGGER: Once = Once::new();
static INIT_LOG_LEVEL: Once = Once::new();

pub trait Log: Send + Sync {
/// logs a message
Expand All @@ -182,26 +192,49 @@ 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 [`Log`]ger. Can be only called once at the beginning of the program. If the
/// [`Log`]ger is already set it returns false and does not update it.
pub fn set_logger<T: Log + '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
}

Expand Down

0 comments on commit 307b151

Please sign in to comment.