From 15edc93a5f4c21d66b715c5e91914da9d2dba9f8 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 4 Feb 2022 16:57:44 -0500 Subject: [PATCH] Use parantheses around logging macro parameter (#1820) (#1823) * Use parantheses around logging macro parameter This allows the macro to expand "correctly", i.e. macro argument expression is fully evaluated before use. Signed-off-by: Abrar Rahman Protyasha * Remove redundant parantheses around macro param `decltype(X)` already provides sufficient "scoping" to the macro parameter `X`. Signed-off-by: Abrar Rahman Protyasha * Add test case for expressions as logging param Signed-off-by: Abrar Rahman Protyasha (cherry picked from commit f7bb88fc8fabcaf5008dfe87b5aec7d2269ba147) Co-authored-by: Abrar Rahman Protyasha --- rclcpp/resource/logging.hpp.em | 2 +- rclcpp/test/rclcpp/test_logging.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/rclcpp/resource/logging.hpp.em b/rclcpp/resource/logging.hpp.em index 3e99619c79..9036d9a830 100644 --- a/rclcpp/resource/logging.hpp.em +++ b/rclcpp/resource/logging.hpp.em @@ -149,7 +149,7 @@ def get_rclcpp_suffix_from_features(features): @[ if params]@ @(''.join([' ' + p + ', \\\n' for p in params if p != stream_arg]))@ @[ end if]@ - logger.get_name(), \ + (logger).get_name(), \ @[ if 'stream' not in feature_combination]@ rclcpp::get_c_string(RCLCPP_FIRST_ARG(__VA_ARGS__, "")), \ RCLCPP_ALL_BUT_FIRST_ARGS(__VA_ARGS__,"")); \ diff --git a/rclcpp/test/rclcpp/test_logging.cpp b/rclcpp/test/rclcpp/test_logging.cpp index 87e93f9fe3..4dbb6bdd3a 100644 --- a/rclcpp/test/rclcpp/test_logging.cpp +++ b/rclcpp/test/rclcpp/test_logging.cpp @@ -249,6 +249,12 @@ TEST_F(TestLoggingMacros, test_throttle) { } } +TEST_F(TestLoggingMacros, test_parameter_expression) { + RCLCPP_DEBUG_STREAM(*&g_logger, "message"); + EXPECT_EQ(1u, g_log_calls); + EXPECT_EQ("message", g_last_log_event.message); +} + bool log_function(rclcpp::Logger logger) { RCLCPP_INFO(logger, "successful log");