diff --git a/rclrust/src/context.rs b/rclrust/src/context.rs index f083c1eb..5bace373 100644 --- a/rclrust/src/context.rs +++ b/rclrust/src/context.rs @@ -7,6 +7,7 @@ use anyhow::{Context as _, Result}; use crate::error::ToRclRustResult; use crate::init_options::InitOptions; use crate::log::Logger; +use crate::log::{logging_output_handler, LOGGER_MUTEX}; use crate::node::Node; use crate::node_options::NodeOptions; use crate::rclrust_error; @@ -41,6 +42,17 @@ impl RclContext { ) .to_result() .with_context(|| "rcl_sys::rcl_init in RclContext::new")?; + + { + let _guard = LOGGER_MUTEX.lock(); + rcl_sys::rcl_logging_configure_with_output_handler( + &handle.global_arguments, + &rcl_sys::rcutils_get_default_allocator(), + Some(logging_output_handler), + ) + .to_result()? + } + Ok(Self(handle)) } } @@ -179,19 +191,6 @@ impl Context { } } -impl Drop for Context { - fn drop(&mut self) { - let ret = unsafe { rcl_sys::rcl_logging_fini().to_result() }; - if let Err(e) = ret { - rclrust_error!( - Logger::new("rclrust"), - "Failed to tear down the logging setup: {}", - e - ) - } - } -} - #[cfg(test)] mod test { use super::*; diff --git a/rclrust/src/log.rs b/rclrust/src/log.rs index 06fca1b2..acb3e7cd 100644 --- a/rclrust/src/log.rs +++ b/rclrust/src/log.rs @@ -54,6 +54,19 @@ impl TryFrom for LogSeverity { } } +#[no_mangle] +pub(crate) unsafe extern "C" fn logging_output_handler( + location: *const rcl_sys::rcutils_log_location_t, + severity: c_int, + name: *const c_char, + timestamp: rcl_sys::rcutils_time_point_value_t, + format: *const c_char, + args: *mut rcl_sys::va_list, +) { + let _guard = LOGGER_MUTEX.lock(); + rcl_sys::rcl_logging_multiple_output_handler(location, severity, name, timestamp, format, args) +} + pub(crate) static LOGGER_MUTEX: Lazy> = Lazy::new(|| ReentrantMutex::new(())); #[derive(Debug)]