Skip to content

Commit

Permalink
Use parantheses around logging macro parameter (#1820)
Browse files Browse the repository at this point in the history
* 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 <aprotyas@u.rochester.edu>

* Remove redundant parantheses around macro param

`decltype(X)` already provides sufficient "scoping" to the macro
parameter `X`.

Signed-off-by: Abrar Rahman Protyasha <aprotyas@u.rochester.edu>

* Add test case for expressions as logging param

Signed-off-by: Abrar Rahman Protyasha <aprotyas@u.rochester.edu>
(cherry picked from commit f7bb88f)
  • Loading branch information
aprotyas authored and clalancette committed Feb 4, 2022
1 parent 1ea2530 commit 99a2b43
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rclcpp/resource/logging.hpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -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__,"")); \
Expand Down
6 changes: 6 additions & 0 deletions rclcpp/test/rclcpp/test_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 99a2b43

Please sign in to comment.