|
| 1 | +// Copyright (c) 2019 Sequence Planner |
| 2 | +// SPDX-License-Identifier: Apache-2.0 AND MIT |
| 3 | +// Adapted from https://github.com/sequenceplanner/r2r/blob/89cec03d07a1496a225751159cbc7bfb529d9dd1/r2r/src/utils.rs |
| 4 | + |
| 5 | +use std::{ |
| 6 | + ffi::CString, |
| 7 | + sync::{Mutex, Once}, |
| 8 | +}; |
| 9 | + |
| 10 | +use crate::error; |
| 11 | +use crate::rcl_bindings::*; |
| 12 | + |
| 13 | +static INIT: Once = Once::new(); |
| 14 | +static LOG_GUARD: Mutex<()> = Mutex::new(()); |
| 15 | + |
| 16 | +/// Don't call this directly, use the logging macros instead. |
| 17 | +#[doc(hidden)] |
| 18 | +pub fn log(msg: &str, logger_name: &str, file: &str, line: u32, severity: LogSeverity) { |
| 19 | + // currently not possible to get function name in rust. |
| 20 | + // see https://github.com/rust-lang/rfcs/pull/2818 |
| 21 | + let function = CString::new("").unwrap(); |
| 22 | + let file = CString::new(file).unwrap(); |
| 23 | + let location = rcutils_log_location_t { |
| 24 | + function_name: function.as_ptr(), |
| 25 | + file_name: file.as_ptr(), |
| 26 | + line_number: line as usize, |
| 27 | + }; |
| 28 | + let format = CString::new("%s").unwrap(); |
| 29 | + let logger_name = CString::new(logger_name).unwrap(); |
| 30 | + let message = CString::new(msg).unwrap(); |
| 31 | + let severity = severity.to_native(); |
| 32 | + |
| 33 | + INIT.call_once(|| { |
| 34 | + let ret = unsafe { rcutils_logging_initialize() }; |
| 35 | + if let Err(code) = error::to_rclrs_result(ret) { |
| 36 | + panic!("Failed to initialize logging: {:?}", code); |
| 37 | + } |
| 38 | + }); |
| 39 | + let _guard = LOG_GUARD.lock().unwrap(); |
| 40 | + unsafe { |
| 41 | + rcutils_log( |
| 42 | + &location, |
| 43 | + severity as i32, |
| 44 | + logger_name.as_ptr(), |
| 45 | + format.as_ptr(), |
| 46 | + message.as_ptr(), |
| 47 | + ); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// Logging severity |
| 52 | +#[doc(hidden)] |
| 53 | +pub enum LogSeverity { |
| 54 | + Unset, |
| 55 | + Debug, |
| 56 | + Info, |
| 57 | + Warn, |
| 58 | + Error, |
| 59 | + Fatal, |
| 60 | +} |
| 61 | + |
| 62 | +impl LogSeverity { |
| 63 | + fn to_native(&self) -> RCUTILS_LOG_SEVERITY { |
| 64 | + use crate::rcl_bindings::rcl_log_severity_t::*; |
| 65 | + match self { |
| 66 | + LogSeverity::Unset => RCUTILS_LOG_SEVERITY_UNSET, |
| 67 | + LogSeverity::Debug => RCUTILS_LOG_SEVERITY_DEBUG, |
| 68 | + LogSeverity::Info => RCUTILS_LOG_SEVERITY_INFO, |
| 69 | + LogSeverity::Warn => RCUTILS_LOG_SEVERITY_WARN, |
| 70 | + LogSeverity::Error => RCUTILS_LOG_SEVERITY_ERROR, |
| 71 | + LogSeverity::Fatal => RCUTILS_LOG_SEVERITY_FATAL, |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// A helper macro to log the message. |
| 77 | +#[macro_export] |
| 78 | +macro_rules! __impl_log { |
| 79 | + ($logger_name:expr, $msg:expr, $file:expr, $line:expr, $severity:expr) => {{ |
| 80 | + $crate::log(&std::fmt::format($msg), $logger_name, $file, $line, $severity); |
| 81 | + }}; |
| 82 | +} |
| 83 | + |
| 84 | +/// Debug log message. |
| 85 | +#[macro_export] |
| 86 | +macro_rules! log_debug { |
| 87 | + ($logger_name:expr, $($args:tt)*) => {{ |
| 88 | + $crate::__impl_log!($logger_name, format_args!($($args)*), |
| 89 | + file!(), line!(), $crate::LogSeverity::Debug) |
| 90 | + }} |
| 91 | +} |
| 92 | + |
| 93 | +/// Info log message. |
| 94 | +#[macro_export] |
| 95 | +macro_rules! log_info { |
| 96 | + ($logger_name:expr, $($args:tt)*) => {{ |
| 97 | + $crate::__impl_log!($logger_name, format_args!($($args)*), |
| 98 | + file!(), line!(), $crate::LogSeverity::Info) |
| 99 | + }} |
| 100 | +} |
| 101 | + |
| 102 | +/// Warning log message. |
| 103 | +#[macro_export] |
| 104 | +macro_rules! log_warn { |
| 105 | + ($logger_name:expr, $($args:tt)*) => {{ |
| 106 | + $crate::__impl_log!($logger_name, format_args!($($args)*), |
| 107 | + file!(), line!(), $crate::LogSeverity::Warn) |
| 108 | + }} |
| 109 | +} |
| 110 | + |
| 111 | +/// Error log message. |
| 112 | +#[macro_export] |
| 113 | +macro_rules! log_error { |
| 114 | + ($logger_name:expr, $($args:tt)*) => {{ |
| 115 | + $crate::__impl_log!($logger_name, format_args!($($args)*), |
| 116 | + file!(), line!(), $crate::LogSeverity::Error) |
| 117 | + }} |
| 118 | +} |
| 119 | + |
| 120 | +/// Fatal log message. |
| 121 | +#[macro_export] |
| 122 | +macro_rules! log_fatal { |
| 123 | + ($logger_name:expr, $($args:tt)*) => {{ |
| 124 | + $crate::__impl_log!($logger_name, format_args!($($args)*), |
| 125 | + file!(), line!(), $crate::LogSeverity::Fatal) |
| 126 | + }} |
| 127 | +} |
0 commit comments