Logging: fix dangling pointers(!) and cleanup
This commit is contained in:
parent
f27b90effb
commit
0bb844c773
|
|
@ -8,24 +8,23 @@ use failure::Error;
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
r2r::log_debug!("before_init", "debug msg");
|
r2r::log_debug!("before_init", "debug msg");
|
||||||
let ctx = r2r::Context::create()?;
|
let ctx = r2r::Context::create()?;
|
||||||
|
|
||||||
|
|
||||||
let node = r2r::Node::create(ctx, "logger_node", "")?;
|
let node = r2r::Node::create(ctx, "logger_node", "")?;
|
||||||
|
let nl = node.logger();
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
loop {
|
loop {
|
||||||
r2r::log_debug!(node.logger(), "debug msg: {}", i as f64 / 2.5);
|
match i % 5 {
|
||||||
std::thread::sleep_ms(10);
|
0 => r2r::log_debug!(nl, "debug msg: {}", i as f64 / 2.5),
|
||||||
r2r::log_info!(node.logger(), "info msg {}", i % 2);
|
1 => r2r::log_info!(nl, "info msg {}", i % 2),
|
||||||
std::thread::sleep_ms(10);
|
2 => r2r::log_warn!(nl, "warn msg {:?}", i.to_string()),
|
||||||
r2r::log_warn!(node.logger(), "warn msg {:?}", i.to_string());
|
3 => r2r::log_error!(nl, "error msg {:?}",
|
||||||
std::thread::sleep_ms(10);
|
i.to_string().as_bytes()),
|
||||||
r2r::log_error!(node.logger(), "error msg {:?}", i.to_string().as_bytes());
|
_ => r2r::log_fatal!(nl, "fatal msg {:#X}", i),
|
||||||
std::thread::sleep_ms(10);
|
}
|
||||||
r2r::log_fatal!(node.logger(), "fatal msg {}", i);
|
|
||||||
|
|
||||||
std::thread::sleep_ms(1000);
|
std::thread::sleep(std::time::Duration::from_millis(1000));
|
||||||
|
|
||||||
|
// non-node logger only outputs to stdout
|
||||||
r2r::log_debug!("other_logger", "i = {}", i);
|
r2r::log_debug!("other_logger", "i = {}", i);
|
||||||
i+=1;
|
i+=1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
40
src/utils.rs
40
src/utils.rs
|
|
@ -1,5 +1,5 @@
|
||||||
use std::ffi::{CString};
|
use std::ffi::{CString};
|
||||||
use std::sync::Mutex;
|
use std::sync::{Mutex,MutexGuard};
|
||||||
use rcl::*;
|
use rcl::*;
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
@ -8,54 +8,40 @@ lazy_static! {
|
||||||
static ref LOG_GUARD: Mutex<()> = Mutex::new(());
|
static ref LOG_GUARD: Mutex<()> = Mutex::new(());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn log_guard() -> std::sync::MutexGuard<'static, ()> {
|
pub(crate) fn log_guard() -> MutexGuard<'static, ()> {
|
||||||
LOG_GUARD.lock().unwrap()
|
LOG_GUARD.lock().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_init() -> bool {
|
|
||||||
let _guard = log_guard();
|
|
||||||
unsafe { g_rcutils_logging_initialized }
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: cleanup
|
|
||||||
// fn shutdown() {
|
|
||||||
// if check_init() {
|
|
||||||
// unsafe { rcutils_logging_shutdown(); }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
/// Don't call this directly, use the macros below instead.
|
/// Don't call this directly, use the macros below instead.
|
||||||
pub fn log(msg: &str, logger_name: &str, file: &str, line: u32, severity: LogSeverity) {
|
pub fn log(msg: &str, logger_name: &str, file: &str, line: u32, severity: LogSeverity) {
|
||||||
if !check_init() {
|
let _guard = log_guard();
|
||||||
let ret = unsafe {
|
let is_init = unsafe { g_rcutils_logging_initialized };
|
||||||
let _guard = log_guard();
|
if !is_init {
|
||||||
rcutils_logging_initialize()
|
let ret = unsafe { rcutils_logging_initialize() };
|
||||||
};
|
|
||||||
if ret != RCL_RET_OK as i32 {
|
if ret != RCL_RET_OK as i32 {
|
||||||
eprintln!("could not create logging system (Err: {})", ret);
|
eprintln!("could not create logging system (Err: {})", ret);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// currently not possible to get function name in rust.
|
// currently not possible to get function name in rust.
|
||||||
let function_name = CString::new("").unwrap().as_ptr();
|
// see https://github.com/rust-lang/rfcs/pull/2818
|
||||||
let file_name = CString::new(file).unwrap().as_ptr();
|
let function = CString::new("").unwrap();
|
||||||
|
let file = CString::new(file).unwrap();
|
||||||
let location = rcutils_log_location_t {
|
let location = rcutils_log_location_t {
|
||||||
function_name,
|
function_name: function.as_ptr(),
|
||||||
file_name,
|
file_name: file.as_ptr(),
|
||||||
line_number: line as usize,
|
line_number: line as usize,
|
||||||
};
|
};
|
||||||
let logger_name = CString::new(logger_name).unwrap();
|
|
||||||
let format = CString::new("%s").unwrap();
|
let format = CString::new("%s").unwrap();
|
||||||
|
let logger_name = CString::new(logger_name).unwrap();
|
||||||
let message = CString::new(msg).unwrap();
|
let message = CString::new(msg).unwrap();
|
||||||
let severity = severity.to_native();
|
let severity = severity.to_native();
|
||||||
unsafe {
|
unsafe {
|
||||||
let _guard = log_guard();
|
|
||||||
rcutils_log(&location,
|
rcutils_log(&location,
|
||||||
severity as i32,
|
severity as i32,
|
||||||
logger_name.as_ptr(),
|
logger_name.as_ptr(),
|
||||||
format.as_ptr(),
|
format.as_ptr(),
|
||||||
message.as_ptr(),
|
message.as_ptr());
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue