Skip to content

Commit

Permalink
[#396] Update from_str implementation and where to set LOG_LEVEL
Browse files Browse the repository at this point in the history
Signed-off-by: Ziad Mostafa <[email protected]>
  • Loading branch information
zmostafa authored and Ziad Mostafa committed Oct 16, 2024
1 parent 624cc45 commit 56705a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
35 changes: 14 additions & 21 deletions iceoryx2-bb/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ use std::{
};

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

#[cfg(feature = "logger_tracing")]
Expand All @@ -162,14 +162,13 @@ 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")))]
pub static DEFAULT_LOGGER: Lazy<logger::console::Logger> = Lazy::new(|| {
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
LogLevel::from_str(&log_level_str) // Default to Info
});

static mut LOGGER: Option<&'static dyn logger::Logger> = None;
Expand All @@ -188,26 +187,19 @@ pub enum LogLevel {
Fatal = 5,
}

impl FromStr for LogLevel {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
impl LogLevel {
fn from_str(s: &str) -> LogLevel {
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),
"Trace" => LogLevel::Trace,
"Debug" => LogLevel::Debug,
"Info" => LogLevel::Info,
"Warn" => LogLevel::Warn,
"Error" => LogLevel::Error,
"Fatal" => LogLevel::Fatal,
_ => 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.
Expand All @@ -217,6 +209,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 Down
6 changes: 2 additions & 4 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, init_log_level, LogLevel};
use crate::{get_log_level, LogLevel};

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

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

pub const fn new() -> Self {
Self {
counter: IoxAtomicU64::new(0),
ordering_mode: ConsoleLogOrder::Counter,
Expand Down

0 comments on commit 56705a0

Please sign in to comment.