Skip to content

Commit 2446fd0

Browse files
author
Chen Lihui
authored
aligh with rcl that a rosout publisher of a node might not exist (ros2#2357)
* aligh with rcl * 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 * test for a node with rosout disabled Signed-off-by: Chen Lihui <[email protected]>
1 parent 411dbe8 commit 2446fd0

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

rclcpp/src/rclcpp/logger.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <utility>
1818

1919
#include "rcl_logging_interface/rcl_logging_interface.h"
20+
#include "rcl/error_handling.h"
2021
#include "rcl/logging_rosout.h"
2122

2223
#include "rclcpp/exceptions.hpp"
@@ -80,10 +81,12 @@ Logger::get_child(const std::string & suffix)
8081
{
8182
std::lock_guard<std::recursive_mutex> guard(*logging_mutex);
8283
rcl_ret = rcl_logging_rosout_add_sublogger((*name_).c_str(), suffix.c_str());
83-
if (RCL_RET_OK != rcl_ret) {
84+
if (RCL_RET_NOT_FOUND == rcl_ret) {
85+
rcl_reset_error();
86+
} else if (RCL_RET_OK != rcl_ret) {
8487
exceptions::throw_from_rcl_error(
8588
rcl_ret, "failed to call rcl_logging_rosout_add_sublogger",
86-
rcutils_get_error_state(), rcutils_reset_error);
89+
rcl_get_error_state(), rcl_reset_error);
8790
}
8891
}
8992

@@ -98,9 +101,7 @@ Logger::get_child(const std::string & suffix)
98101
logger_sublogger_pairname_ptr->second.c_str());
99102
delete logger_sublogger_pairname_ptr;
100103
if (RCL_RET_OK != rcl_ret) {
101-
exceptions::throw_from_rcl_error(
102-
rcl_ret, "failed to call rcl_logging_rosout_remove_sublogger",
103-
rcutils_get_error_state(), rcutils_reset_error);
104+
rcl_reset_error();
104105
}
105106
});
106107
}

rclcpp/test/rclcpp/test_rosout_subscription.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,13 @@ TEST_F(TestRosoutSubscription, test_rosoutsubscription_getchild_hierarchy) {
178178
EXPECT_TRUE(future.get());
179179
received_msg_promise = {};
180180
}
181+
182+
TEST_F(TestRosoutSubscription, test_rosoutsubscription_node_rosout_disabled) {
183+
rclcpp::NodeOptions options = rclcpp::NodeOptions().enable_rosout(false);
184+
auto node = std::make_shared<rclcpp::Node>("my_node", options);
185+
auto log_func = [&node] {
186+
auto logger = node->get_logger().get_child("child");
187+
RCLCPP_INFO(logger, "test");
188+
};
189+
ASSERT_NO_THROW(log_func());
190+
}

0 commit comments

Comments
 (0)