Skip to content

Commit

Permalink
[eclipse-iceoryx#396] Apply reviewers' comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ziad Mostafa <[email protected]>
  • Loading branch information
zmostafa committed Oct 21, 2024
1 parent cd5b050 commit 41fbf0d
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions iceoryx2-bb/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,17 @@ 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);

const DEFAULT_LOG_LEVEL: u8 = LogLevel::Info as u8;
pub static ENV_LOG_LEVEL: Lazy<u8> = Lazy::new(|| {
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 as u8)
.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 @@ -187,16 +188,17 @@ pub enum LogLevel {
}

impl LogLevel {
fn from_str_fuzzy(s: &str) -> u8 {
fn from_str_fuzzy(s: &str) -> LogLevel {
match s.to_lowercase().as_str() {
"trace" => LogLevel::Trace as u8,
"tebug" => LogLevel::Debug as u8,
"info" => LogLevel::Info as u8,
"warn" => LogLevel::Warn as u8,
"error" => LogLevel::Error as u8,
"fatal" => LogLevel::Fatal as u8,
"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
}
}
Expand All @@ -218,19 +220,25 @@ pub fn get_log_level() -> u8 {
/// [`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;
});
INIT_LOG_LEVEL.call_once(|| {
LOG_LEVEL.store(*ENV_LOG_LEVEL as u8, Ordering::Relaxed);
});

set_logger_success
}

/// Returns a reference to the [`Logger`].
pub fn get_logger() -> &'static dyn Logger {
INIT.call_once(|| {
INIT_LOGGER.call_once(|| {
unsafe { LOGGER = Some(&*DEFAULT_LOGGER) };
LOG_LEVEL.store(*ENV_LOG_LEVEL, Ordering::Relaxed);
});

INIT_LOG_LEVEL.call_once(|| {
LOG_LEVEL.store(*ENV_LOG_LEVEL as u8, Ordering::Relaxed);
});

unsafe { *LOGGER.as_ref().unwrap() }
Expand Down

0 comments on commit 41fbf0d

Please sign in to comment.