Not creating rosout publisher instance unless required. (#514)

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>
This commit is contained in:
tomoya 2019-10-17 22:30:08 +09:00 committed by Ivan Santiago Paunovic
parent 1a840d1219
commit f92e52a2bb
3 changed files with 39 additions and 9 deletions

View file

@ -71,6 +71,25 @@ RCL_PUBLIC
RCL_WARN_UNUSED RCL_WARN_UNUSED
rcl_ret_t rcl_logging_fini(); rcl_ret_t rcl_logging_fini();
/// See if logging rosout is enabled.
/**
* This function is meant to be used to check if logging rosout is enabled.
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | Yes
* Uses Atomics | No
* Lock-Free | Yes
*
* \return `TRUE` if logging rosout is enabled, or
* \return `FALSE` if logging rosout is disabled.
*/
RCL_PUBLIC
RCL_WARN_UNUSED
bool rcl_logging_rosout_enabled();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -119,6 +119,11 @@ rcl_ret_t rcl_logging_fini()
return status; return status;
} }
bool rcl_logging_rosout_enabled()
{
return g_rcl_logging_rosout_enabled;
}
static static
void void
rcl_logging_multiple_output_handler( rcl_logging_multiple_output_handler(

View file

@ -26,6 +26,7 @@ extern "C"
#include "rcl/arguments.h" #include "rcl/arguments.h"
#include "rcl/error_handling.h" #include "rcl/error_handling.h"
#include "rcl/logging.h"
#include "rcl/logging_rosout.h" #include "rcl/logging_rosout.h"
#include "rcl/rcl.h" #include "rcl/rcl.h"
#include "rcl/remap.h" #include "rcl/remap.h"
@ -355,17 +356,19 @@ rcl_node_init(
} }
// The initialization for the rosout publisher requires the node to be in initialized to a point // The initialization for the rosout publisher requires the node to be in initialized to a point
// that it can create new topic publishers // that it can create new topic publishers
ret = rcl_logging_rosout_init_publisher_for_node(node); if (rcl_logging_rosout_enabled()) {
if (ret != RCL_RET_OK) { ret = rcl_logging_rosout_init_publisher_for_node(node);
// error message already set if (ret != RCL_RET_OK) {
goto fail; // error message already set
goto fail;
}
} }
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Node initialized"); RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Node initialized");
ret = RCL_RET_OK; ret = RCL_RET_OK;
goto cleanup; goto cleanup;
fail: fail:
if (node->impl) { if (node->impl) {
if (node->impl->logger_name) { if (rcl_logging_rosout_enabled() && node->impl->logger_name) {
ret = rcl_logging_rosout_fini_publisher_for_node(node); ret = rcl_logging_rosout_fini_publisher_for_node(node);
RCUTILS_LOG_ERROR_EXPRESSION_NAMED((ret != RCL_RET_OK && ret != RCL_RET_NOT_INIT), RCUTILS_LOG_ERROR_EXPRESSION_NAMED((ret != RCL_RET_OK && ret != RCL_RET_NOT_INIT),
ROS_PACKAGE_NAME, "Failed to fini publisher for node: %i", ret); ROS_PACKAGE_NAME, "Failed to fini publisher for node: %i", ret);
@ -431,10 +434,13 @@ rcl_node_fini(rcl_node_t * node)
} }
rcl_allocator_t allocator = node->impl->options.allocator; rcl_allocator_t allocator = node->impl->options.allocator;
rcl_ret_t result = RCL_RET_OK; rcl_ret_t result = RCL_RET_OK;
rcl_ret_t rcl_ret = rcl_logging_rosout_fini_publisher_for_node(node); rcl_ret_t rcl_ret = RCL_RET_OK;
if (rcl_ret != RCL_RET_OK && rcl_ret != RCL_RET_NOT_INIT) { if (rcl_logging_rosout_enabled()) {
RCL_SET_ERROR_MSG("Unable to fini publisher for node."); rcl_ret = rcl_logging_rosout_fini_publisher_for_node(node);
result = RCL_RET_ERROR; if (rcl_ret != RCL_RET_OK && rcl_ret != RCL_RET_NOT_INIT) {
RCL_SET_ERROR_MSG("Unable to fini publisher for node.");
result = RCL_RET_ERROR;
}
} }
rmw_ret_t rmw_ret = rmw_destroy_node(node->impl->rmw_node_handle); rmw_ret_t rmw_ret = rmw_destroy_node(node->impl->rmw_node_handle);
if (rmw_ret != RMW_RET_OK) { if (rmw_ret != RMW_RET_OK) {