From c1b49008df72121d9158e7aaef2fc727ecfbbc60 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Thu, 11 May 2023 16:05:52 +0200 Subject: [PATCH] allow log macros as expression This allows using them in e.g. a match statement without a block, and it matches the behavior of std's `print!()` macros. ```rust match true { true => ros_info!("hello"), false => unreachable!(), } ``` --- rosrust/src/log_macros.rs | 67 +++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/rosrust/src/log_macros.rs b/rosrust/src/log_macros.rs index 80383a5..c1f32c9 100644 --- a/rosrust/src/log_macros.rs +++ b/rosrust/src/log_macros.rs @@ -1,170 +1,181 @@ #[macro_export] macro_rules! ros_log { - ($level:expr, $($arg:tt)+) => { + ($level:expr, $($arg:tt)+) => {{ let msg = format!($($arg)*); $crate::log($level, msg, file!(), line!()); - } + }} } #[macro_export] macro_rules! ros_debug { ($($arg:tt)*) => { - $crate::ros_log!($crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*); + $crate::ros_log!($crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*) } } #[macro_export] macro_rules! ros_info { ($($arg:tt)*) => { - $crate::ros_log!($crate::msg::rosgraph_msgs::Log::INFO, $($arg)*); + $crate::ros_log!($crate::msg::rosgraph_msgs::Log::INFO, $($arg)*) } } #[macro_export] macro_rules! ros_warn { ($($arg:tt)*) => { - $crate::ros_log!($crate::msg::rosgraph_msgs::Log::WARN, $($arg)*); + $crate::ros_log!($crate::msg::rosgraph_msgs::Log::WARN, $($arg)*) } } #[macro_export] macro_rules! ros_err { ($($arg:tt)*) => { - $crate::ros_log!($crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*); + $crate::ros_log!($crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*) } } #[macro_export] macro_rules! ros_fatal { ($($arg:tt)*) => { - $crate::ros_log!($crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*); + $crate::ros_log!($crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*) } } #[macro_export] macro_rules! ros_log_once { - ($level:expr, $($arg:tt)+) => { + ($level:expr, $($arg:tt)+) => {{ let msg = format!($($arg)*); $crate::log_once($level, msg, file!(), line!()); - } + }} } #[macro_export] macro_rules! ros_debug_once { ($($arg:tt)*) => { - $crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*); + $crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*) } } #[macro_export] macro_rules! ros_info_once { ($($arg:tt)*) => { - $crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::INFO, $($arg)*); + $crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::INFO, $($arg)*) } } #[macro_export] macro_rules! ros_warn_once { ($($arg:tt)*) => { - $crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::WARN, $($arg)*); + $crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::WARN, $($arg)*) } } #[macro_export] macro_rules! ros_err_once { ($($arg:tt)*) => { - $crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*); + $crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*) } } #[macro_export] macro_rules! ros_fatal_once { ($($arg:tt)*) => { - $crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*); + $crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*) } } #[macro_export] macro_rules! ros_log_throttle { - ($period:expr, $level:expr, $($arg:tt)+) => { + ($period:expr, $level:expr, $($arg:tt)+) => {{ let msg = format!($($arg)*); $crate::log_throttle($period, $level, msg, file!(), line!()); - } + }} } #[macro_export] macro_rules! ros_debug_throttle { ($period:expr, $($arg:tt)*) => { - $crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*); + $crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*) } } #[macro_export] macro_rules! ros_info_throttle { ($period:expr, $($arg:tt)*) => { - $crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::INFO, $($arg)*); + $crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::INFO, $($arg)*) } } #[macro_export] macro_rules! ros_warn_throttle { ($period:expr, $($arg:tt)*) => { - $crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::WARN, $($arg)*); + $crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::WARN, $($arg)*) } } #[macro_export] macro_rules! ros_err_throttle { ($period:expr, $($arg:tt)*) => { - $crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*); + $crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*) } } #[macro_export] macro_rules! ros_fatal_throttle { ($period:expr, $($arg:tt)*) => { - $crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*); + $crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*) } } #[macro_export] macro_rules! ros_log_throttle_identical { - ($period:expr, $level:expr, $($arg:tt)+) => { + ($period:expr, $level:expr, $($arg:tt)+) => {{ let msg = format!($($arg)*); $crate::log_throttle_identical($period, $level, msg, file!(), line!()); - } + }} } #[macro_export] macro_rules! ros_debug_throttle_identical { ($period:expr, $($arg:tt)*) => { - $crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*); + $crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*) } } #[macro_export] macro_rules! ros_info_throttle_identical { ($period:expr, $($arg:tt)*) => { - $crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::INFO, $($arg)*); + $crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::INFO, $($arg)*) } } #[macro_export] macro_rules! ros_warn_throttle_identical { ($period:expr, $($arg:tt)*) => { - $crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::WARN, $($arg)*); + $crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::WARN, $($arg)*) } } #[macro_export] macro_rules! ros_err_throttle_identical { ($period:expr, $($arg:tt)*) => { - $crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*); + $crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*) } } #[macro_export] macro_rules! ros_fatal_throttle_identical { ($period:expr, $($arg:tt)*) => { - $crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*); + $crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*) + } +} + +#[cfg(test)] +fn _macro_in_expr_position() { + // Only tests compilation + match true { + true => ros_info!("hello"), + false => unreachable!(), } + ros_info! {"hello"} + ros_info!("hello"); }