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 17, 2024
1 parent e5b44a9 commit 11c89b3
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 4 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 }
26 changes: 24 additions & 2 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,9 +161,14 @@ 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;
pub static ENV_LOG_LEVEL: Lazy<LogLevel> = Lazy::new(|| {
let log_level_str = env::var("IOX2_LOG_LEVEL").unwrap_or_else(|_| "Info".to_string());
LogLevel::from_str(&log_level_str) // Default to Info
});

static mut LOGGER: Option<&'static dyn logger::Logger> = None;
static LOG_LEVEL: IoxAtomicU8 = IoxAtomicU8::new(DEFAULT_LOG_LEVEL);
Expand All @@ -179,6 +186,20 @@ pub enum LogLevel {
Fatal = 5,
}

impl LogLevel {
fn from_str(s: &str) -> LogLevel {
match s {
"Trace" => LogLevel::Trace,
"Debug" => LogLevel::Debug,
"Info" => LogLevel::Info,
"Warn" => LogLevel::Warn,
"Error" => LogLevel::Error,
"Fatal" => LogLevel::Fatal,
_ => LogLevel::Info,
}
}
}

/// 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) {
Expand All @@ -187,6 +208,7 @@ pub fn set_log_level(v: LogLevel) {

/// Returns the current log level
pub fn get_log_level() -> u8 {
LOG_LEVEL.store(*ENV_LOG_LEVEL as u8, Ordering::Relaxed);
LOG_LEVEL.load(Ordering::Relaxed)
}

Expand All @@ -205,7 +227,7 @@ pub fn set_logger<T: logger::Logger + 'static>(value: &'static T) -> bool {
/// Returns a reference to the [`Logger`].
pub fn get_logger() -> &'static dyn Logger {
INIT.call_once(|| {
unsafe { LOGGER = Some(&DEFAULT_LOGGER) };
unsafe { LOGGER = Some(&*DEFAULT_LOGGER) };
});

unsafe { *LOGGER.as_ref().unwrap() }
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2-bb/log/src/logger/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::LogLevel;
use crate::{init_log_level, LogLevel};

pub struct Logger {
_priv: (),
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2-bb/log/src/logger/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::LogLevel;
use crate::{init_log_level, LogLevel};

pub struct Logger {
_priv: (),
Expand Down

0 comments on commit 11c89b3

Please sign in to comment.