Skip to content

Commit

Permalink
aligh with rcl that a rosout publisher of a node might not exist (ros…
Browse files Browse the repository at this point in the history
…2#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 <lihui.chen@sony.com>
Signed-off-by: Oren Bell <oren.bell@wustl.edu>
  • Loading branch information
Chen Lihui authored and nightduck committed Jan 25, 2024
1 parent eefaeeb commit 7967c12
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 6 additions & 5 deletions rclcpp/src/rclcpp/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <utility>

#include "rcl_logging_interface/rcl_logging_interface.h"
#include "rcl/error_handling.h"
#include "rcl/logging_rosout.h"

#include "rclcpp/exceptions.hpp"
Expand Down Expand Up @@ -80,10 +81,12 @@ Logger::get_child(const std::string & suffix)
{
std::lock_guard<std::recursive_mutex> 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_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);
}
}

Expand All @@ -98,9 +101,7 @@ Logger::get_child(const std::string & suffix)
logger_sublogger_pairname_ptr->second.c_str());
delete logger_sublogger_pairname_ptr;
if (RCL_RET_OK != rcl_ret) {
exceptions::throw_from_rcl_error(
rcl_ret, "failed to call rcl_logging_rosout_remove_sublogger",
rcutils_get_error_state(), rcutils_reset_error);
rcl_reset_error();
}
});
}
Expand Down
10 changes: 10 additions & 0 deletions rclcpp/test/rclcpp/test_rosout_subscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<rclcpp::Node>("my_node", options);
auto log_func = [&node] {
auto logger = node->get_logger().get_child("child");
RCLCPP_INFO(logger, "test");
};
ASSERT_NO_THROW(log_func());
}

0 comments on commit 7967c12

Please sign in to comment.