Browse Source

Configuration of logging

tags/v0.2.5-alpha.2
Yuma Hiramatsu 4 years ago
parent
commit
77d049a44e
2 changed files with 25 additions and 13 deletions
  1. +12
    -13
      rclrust/src/context.rs
  2. +13
    -0
      rclrust/src/log.rs

+ 12
- 13
rclrust/src/context.rs View File

@@ -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::*;


+ 13
- 0
rclrust/src/log.rs View File

@@ -54,6 +54,19 @@ impl TryFrom<c_int> 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<ReentrantMutex<()>> = Lazy::new(|| ReentrantMutex::new(()));

#[derive(Debug)]


Loading…
Cancel
Save