Skip to content

Commit c94ee67

Browse files
committed
[#464] Add convencience function to set console or file logger
1 parent 77f4230 commit c94ee67

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

iceoryx2-bb/log/src/logger/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,27 @@
1313
//! Trait which can be implemented by logger, see [`crate::logger::console::Logger`]
1414
//! for instance.
1515
16+
use std::sync::LazyLock;
17+
1618
pub mod buffer;
1719
pub mod console;
1820
pub mod file;
1921
#[cfg(feature = "logger_log")]
2022
pub mod log;
2123
#[cfg(feature = "logger_tracing")]
2224
pub mod tracing;
25+
26+
/// Sets the [`console::Logger`] as default logger
27+
pub fn use_console_logger() -> bool {
28+
static LOGGER: LazyLock<console::Logger> = LazyLock::new(|| console::Logger::new());
29+
crate::set_logger(&*LOGGER)
30+
}
31+
32+
/// Sets the [`file::Logger`] as default logger
33+
pub fn use_file_logger(log_file_name: &str) -> bool {
34+
// we cannot capture non const parameters in a LazyLock and since static values are not
35+
// dropped in Rust we can also use Box::leak
36+
let logger = Box::leak(Box::new(file::Logger::new(log_file_name)));
37+
38+
crate::set_logger(logger)
39+
}

iceoryx2-ffi/cxx/include/iox2/log.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ class Log {
5050
/// Adds a log message to the logger.
5151
void log(LogLevel log_level, const char* origin, const char* message);
5252

53+
/// Sets the console logger as default logger. Returns true if the logger was set, otherwise false.
54+
auto use_console_logger() -> bool;
55+
56+
/// Sets the file logger as default logger. Returns true if the logger was set, otherwise false.
57+
auto use_file_logger(const char* log_file) -> bool;
58+
5359
/// Sets the logger that shall be used. This function can only be called once and must be called
5460
/// before any log message was created.
5561
/// It returns true if the logger was set, otherwise false.

iceoryx2-ffi/cxx/src/log.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,11 @@ auto get_log_level() -> LogLevel {
4545
return LogLevel(iox2_get_log_level());
4646
}
4747

48+
auto use_console_logger() -> bool {
49+
return iox2_use_console_logger();
50+
}
51+
52+
auto use_file_logger(const char* log_file) -> bool {
53+
return iox2_use_file_logger(log_file);
54+
}
4855
} // namespace iox2

iceoryx2-ffi/ffi/src/api/log.rs

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use iceoryx2_bb_log::{
1919
get_log_level, set_log_level, set_logger, Log, LogLevel, __internal_print_log_msg,
20+
logger::{use_console_logger, use_file_logger},
2021
};
2122
use std::{
2223
ffi::{c_char, CStr},
@@ -131,6 +132,8 @@ pub unsafe extern "C" fn iox2_log(
131132
origin: *const c_char,
132133
message: *const c_char,
133134
) {
135+
debug_assert!(!message.is_null());
136+
134137
let empty_origin = b"\0";
135138
let origin = if origin.is_null() {
136139
CStr::from_bytes_with_nul(empty_origin).unwrap()
@@ -146,6 +149,25 @@ pub unsafe extern "C" fn iox2_log(
146149
);
147150
}
148151

152+
/// Sets the console logger as default logger. Returns true if the logger was set, otherwise false.
153+
#[no_mangle]
154+
pub extern "C" fn iox2_use_console_logger() -> bool {
155+
use_console_logger()
156+
}
157+
158+
/// Sets the file logger as default logger. Returns true if the logger was set, otherwise false.
159+
///
160+
/// # Safety
161+
///
162+
/// * log_file must be a valid pointer to a string
163+
#[no_mangle]
164+
pub unsafe extern "C" fn iox2_use_file_logger(log_file: *const c_char) -> bool {
165+
debug_assert!(!log_file.is_null());
166+
167+
let log_file = CStr::from_ptr(log_file).to_string_lossy();
168+
use_file_logger(&log_file)
169+
}
170+
149171
/// Sets the log level.
150172
#[no_mangle]
151173
pub unsafe extern "C" fn iox2_set_log_level(v: iox2_log_level_e) {

0 commit comments

Comments
 (0)