Skip to content

Commit

Permalink
[#464] Rename Logger trait into Log; rename LoggerInterface into Log
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Oct 15, 2024
1 parent e5544bc commit 98e1f21
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 33 deletions.
13 changes: 8 additions & 5 deletions iceoryx2-bb/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ use std::{
sync::{atomic::Ordering, Once},
};

use logger::Logger;

#[cfg(feature = "logger_tracing")]
static DEFAULT_LOGGER: logger::tracing::Logger = logger::tracing::Logger::new();

Expand All @@ -163,10 +161,15 @@ static DEFAULT_LOGGER: logger::console::Logger = logger::console::Logger::new();

const DEFAULT_LOG_LEVEL: u8 = LogLevel::Info as u8;

static mut LOGGER: Option<&'static dyn logger::Logger> = None;
static mut LOGGER: Option<&'static dyn Log> = None;
static LOG_LEVEL: IoxAtomicU8 = IoxAtomicU8::new(DEFAULT_LOG_LEVEL);
static INIT: Once = Once::new();

pub trait Log: Send + Sync {
/// logs a message
fn log(&self, log_level: LogLevel, origin: Arguments, formatted_message: Arguments);
}

/// Describes the log level.
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
Expand All @@ -192,7 +195,7 @@ pub fn get_log_level() -> u8 {

/// Sets the [`Logger`]. Can be only called once at the beginning of the program. If the
/// [`Logger`] is already set it returns false and does not update it.
pub fn set_logger<T: logger::Logger + 'static>(value: &'static T) -> bool {
pub fn set_logger<T: Log + 'static>(value: &'static T) -> bool {
let mut set_logger_success = false;
INIT.call_once(|| {
unsafe { LOGGER = Some(value) };
Expand All @@ -203,7 +206,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 {
pub fn get_logger() -> &'static dyn Log {
INIT.call_once(|| {
unsafe { LOGGER = Some(&DEFAULT_LOGGER) };
});
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2-bb/log/src/logger/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Logger {
}
}

impl crate::logger::Logger for Logger {
impl crate::Log for Logger {
fn log(
&self,
log_level: LogLevel,
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2-bb/log/src/logger/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Logger {
}
}

impl crate::logger::Logger for Logger {
impl crate::Log for Logger {
fn log(
&self,
log_level: crate::LogLevel,
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2-bb/log/src/logger/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Drop for Logger {
}
}

impl crate::Logger for Logger {
impl crate::Log for Logger {
fn log(
&self,
log_level: LogLevel,
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 @@ -22,7 +22,7 @@ impl Logger {
}
}

impl crate::logger::Logger for Logger {
impl crate::Log for Logger {
fn log(
&self,
log_level: crate::LogLevel,
Expand Down
9 changes: 0 additions & 9 deletions iceoryx2-bb/log/src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,3 @@ pub mod file;
pub mod log;
#[cfg(feature = "logger_tracing")]
pub mod tracing;

use std::fmt::Arguments;

use crate::LogLevel;

pub trait Logger: Send + Sync {
/// logs a message
fn log(&self, log_level: LogLevel, origin: Arguments, formatted_message: Arguments);
}
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 @@ -22,7 +22,7 @@ impl Logger {
}
}

impl crate::logger::Logger for Logger {
impl crate::Log for Logger {
fn log(
&self,
log_level: crate::LogLevel,
Expand Down
18 changes: 9 additions & 9 deletions iceoryx2-ffi/cxx/include/iox2/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace iox2 {
/// # Example
///
/// @code
/// class ConsoleLogger : public LoggerInterface {
/// class ConsoleLogger : public Log {
/// public:
/// void log(LogLevel log_level, const char* origin, const char* message) override {
/// std::cout << "origin = " << origin << ", message = " << message << std::endl;
Expand All @@ -33,14 +33,14 @@ namespace iox2 {
///
/// set_logger(CUSTOM_LOGGER);
/// @endcode
class LoggerInterface {
class Log {
public:
LoggerInterface() = default;
LoggerInterface(const LoggerInterface&) = default;
LoggerInterface(LoggerInterface&&) = default;
auto operator=(const LoggerInterface&) -> LoggerInterface& = default;
auto operator=(LoggerInterface&&) -> LoggerInterface& = default;
virtual ~LoggerInterface() = default;
Log() = default;
Log(const Log&) = default;
Log(Log&&) = default;
auto operator=(const Log&) -> Log& = default;
auto operator=(Log&&) -> Log& = default;
virtual ~Log() = default;

/// The actual log method. The system provides the log level, the origin of the message and
/// the actual message.
Expand All @@ -53,7 +53,7 @@ void log(LogLevel log_level, const char* origin, const char* message);
/// Sets the logger that shall be used. This function can only be called once and must be called
/// before any log message was created.
/// It returns true if the logger was set, otherwise false.
auto set_logger(LoggerInterface& logger) -> bool;
auto set_logger(Log& logger) -> bool;

/// Sets the global log level for the application
auto set_log_level(LogLevel level) -> void;
Expand Down
4 changes: 2 additions & 2 deletions iceoryx2-ffi/cxx/src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
namespace iox2 {
namespace {
//NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): it is in an anonymous namespace and therefore only accessible in this compilation unit
iox::optional<LoggerInterface*> global_logger = iox::nullopt;
iox::optional<Log*> global_logger = iox::nullopt;
} // namespace

void internal_log_callback(iox2_log_level_e log_level, const char* origin, const char* message) {
(*global_logger)->log(iox::into<LogLevel>(static_cast<int>(log_level)), origin, message);
}

auto set_logger(LoggerInterface& logger) -> bool {
auto set_logger(Log& logger) -> bool {
auto success = iox2_set_logger(internal_log_callback);
if (success) {
global_logger.emplace(&logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Entry {
}
};

class TestLogger : public LoggerInterface {
class TestLogger : public Log {
public:
static auto set_global_logger() {
auto& instance = get_instance();
Expand Down
4 changes: 2 additions & 2 deletions iceoryx2-ffi/ffi/src/api/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// BEGIN type definition

use iceoryx2_bb_log::{
get_log_level, logger::Logger, set_log_level, set_logger, LogLevel, __internal_print_log_msg,
get_log_level, set_log_level, set_logger, Log, LogLevel, __internal_print_log_msg,
};
use std::{
ffi::{c_char, CStr},
Expand Down Expand Up @@ -87,7 +87,7 @@ impl CLogger {
}
}

impl Logger for CLogger {
impl Log for CLogger {
fn log(
&self,
log_level: LogLevel,
Expand Down

0 comments on commit 98e1f21

Please sign in to comment.