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
Ziad Mostafa committed Oct 16, 2024
1 parent e5b44a9 commit 31f8933
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 6 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 }
34 changes: 32 additions & 2 deletions iceoryx2-bb/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ use std::{
};

use logger::Logger;
use std::env;
use once_cell::sync::Lazy;
use std::str::FromStr;

#[cfg(feature = "logger_tracing")]
static DEFAULT_LOGGER: logger::tracing::Logger = logger::tracing::Logger::new();
Expand All @@ -159,9 +162,15 @@ 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).unwrap_or(LogLevel::Info) // 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 +188,27 @@ pub enum LogLevel {
Fatal = 5,
}

impl FromStr for LogLevel {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Trace" => Ok(LogLevel::Trace),
"Debug" => Ok(LogLevel::Debug),
"Info" => Ok(LogLevel::Info),
"Warn" => Ok(LogLevel::Warn),
"Error" => Ok(LogLevel::Error),
"Fatal" => Ok(LogLevel::Fatal),
_ => Ok(LogLevel::Info),
}
}
}
// Immediately set LOG_LEVEL based on ENV_LOG_LEVEL
pub fn init_log_level() {
// Setting the value during static initialization
LOG_LEVEL.store(*ENV_LOG_LEVEL as u8, Ordering::Relaxed);
}

/// 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 @@ -205,7 +235,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
6 changes: 4 additions & 2 deletions iceoryx2-bb/log/src/logger/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{io::IsTerminal, sync::atomic::Ordering};

use termsize::Size;

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

pub enum ConsoleLogOrder {
Time,
Expand All @@ -36,7 +36,9 @@ impl Default for Logger {
}

impl Logger {
pub const fn new() -> Self {
pub fn new() -> Self {
init_log_level();

Self {
counter: IoxAtomicU64::new(0),
ordering_mode: ConsoleLogOrder::Counter,
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 31f8933

Please sign in to comment.