From 11092ab43cab5c2c572de75c4a398b954ab36ea3 Mon Sep 17 00:00:00 2001 From: Chen Lihui Date: Mon, 6 Nov 2023 09:32:22 +0800 Subject: [PATCH 1/3] aligh with rcl Signed-off-by: Chen Lihui --- rclcpp/src/rclcpp/logger.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rclcpp/src/rclcpp/logger.cpp b/rclcpp/src/rclcpp/logger.cpp index 874858b180..98a0ec9a8c 100644 --- a/rclcpp/src/rclcpp/logger.cpp +++ b/rclcpp/src/rclcpp/logger.cpp @@ -80,7 +80,7 @@ Logger::get_child(const std::string & suffix) { std::lock_guard guard(*logging_mutex); rcl_ret = rcl_logging_rosout_add_sublogger((*name_).c_str(), suffix.c_str()); - if (RCL_RET_OK != rcl_ret) { + if (RCL_RET_OK != rcl_ret && RCL_RET_NOT_FOUND != rcl_ret) { exceptions::throw_from_rcl_error( rcl_ret, "failed to call rcl_logging_rosout_add_sublogger", rcutils_get_error_state(), rcutils_reset_error); @@ -97,7 +97,7 @@ Logger::get_child(const std::string & suffix) logger_sublogger_pairname_ptr->first.c_str(), logger_sublogger_pairname_ptr->second.c_str()); delete logger_sublogger_pairname_ptr; - if (RCL_RET_OK != rcl_ret) { + if (RCL_RET_OK != rcl_ret && RCL_RET_NOT_FOUND != rcl_ret) { exceptions::throw_from_rcl_error( rcl_ret, "failed to call rcl_logging_rosout_remove_sublogger", rcutils_get_error_state(), rcutils_reset_error); From fe74b5a8c7f09eef04c9523c4821317bbe7010f7 Mon Sep 17 00:00:00 2001 From: Chen Lihui Date: Thu, 9 Nov 2023 15:58:37 +0800 Subject: [PATCH 2/3] keep same behavior with rclpy 1. to not throw exception durning rcl_logging_rosout_remove_sublogger 2. reset error message for RCL_RET_NOT_FOUND Signed-off-by: Chen Lihui --- rclcpp/src/rclcpp/logger.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/rclcpp/src/rclcpp/logger.cpp b/rclcpp/src/rclcpp/logger.cpp index 98a0ec9a8c..2e83c07013 100644 --- a/rclcpp/src/rclcpp/logger.cpp +++ b/rclcpp/src/rclcpp/logger.cpp @@ -17,6 +17,7 @@ #include #include "rcl_logging_interface/rcl_logging_interface.h" +#include "rcl/error_handling.h" #include "rcl/logging_rosout.h" #include "rclcpp/exceptions.hpp" @@ -80,10 +81,12 @@ Logger::get_child(const std::string & suffix) { std::lock_guard guard(*logging_mutex); rcl_ret = rcl_logging_rosout_add_sublogger((*name_).c_str(), suffix.c_str()); - if (RCL_RET_OK != rcl_ret && RCL_RET_NOT_FOUND != rcl_ret) { + if (RCL_RET_NOT_FOUND == rcl_ret) { + rcl_reset_error(); + } else if (RCL_RET_OK != rcl_ret) { exceptions::throw_from_rcl_error( rcl_ret, "failed to call rcl_logging_rosout_add_sublogger", - rcutils_get_error_state(), rcutils_reset_error); + rcl_get_error_state(), rcl_reset_error); } } @@ -97,10 +100,8 @@ Logger::get_child(const std::string & suffix) logger_sublogger_pairname_ptr->first.c_str(), logger_sublogger_pairname_ptr->second.c_str()); delete logger_sublogger_pairname_ptr; - if (RCL_RET_OK != rcl_ret && RCL_RET_NOT_FOUND != rcl_ret) { - exceptions::throw_from_rcl_error( - rcl_ret, "failed to call rcl_logging_rosout_remove_sublogger", - rcutils_get_error_state(), rcutils_reset_error); + if (RCL_RET_OK != rcl_ret) { + rcl_reset_error(); } }); } From d16123e4c8985dba0eeaea09d819bb15c11b9eb1 Mon Sep 17 00:00:00 2001 From: Chen Lihui Date: Tue, 14 Nov 2023 08:46:47 +0800 Subject: [PATCH 3/3] test for a node with rosout disabled Signed-off-by: Chen Lihui --- rclcpp/test/rclcpp/test_rosout_subscription.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rclcpp/test/rclcpp/test_rosout_subscription.cpp b/rclcpp/test/rclcpp/test_rosout_subscription.cpp index c5f00bb26d..08786a3fcb 100644 --- a/rclcpp/test/rclcpp/test_rosout_subscription.cpp +++ b/rclcpp/test/rclcpp/test_rosout_subscription.cpp @@ -178,3 +178,13 @@ TEST_F(TestRosoutSubscription, test_rosoutsubscription_getchild_hierarchy) { EXPECT_TRUE(future.get()); received_msg_promise = {}; } + +TEST_F(TestRosoutSubscription, test_rosoutsubscription_node_rosout_disabled) { + rclcpp::NodeOptions options = rclcpp::NodeOptions().enable_rosout(false); + auto node = std::make_shared("my_node", options); + auto log_func = [&node] { + auto logger = node->get_logger().get_child("child"); + RCLCPP_INFO(logger, "test"); + }; + ASSERT_NO_THROW(log_func()); +}