diff --git a/rcl/include/rcl/logging.h b/rcl/include/rcl/logging.h index 9175c6f95..0dd476950 100644 --- a/rcl/include/rcl/logging.h +++ b/rcl/include/rcl/logging.h @@ -49,7 +49,7 @@ extern "C" RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t -rcl_logging_configure( +rcl_logging_init( const rcl_arguments_t * global_args, const rcl_allocator_t * allocator); diff --git a/rcl/src/rcl/init.c b/rcl/src/rcl/init.c index 38c85a5c4..14f005cb3 100644 --- a/rcl/src/rcl/init.c +++ b/rcl/src/rcl/init.c @@ -122,7 +122,7 @@ rcl_init( goto fail; } - ret = rcl_logging_configure(&context->global_arguments, &allocator); + ret = rcl_logging_init(&context->global_arguments, &allocator); if (RCL_RET_OK != ret) { fail_ret = ret; RCUTILS_LOG_ERROR_NAMED( diff --git a/rcl/src/rcl/logging.c b/rcl/src/rcl/logging.c index 96c625dcd..d2bb83419 100644 --- a/rcl/src/rcl/logging.c +++ b/rcl/src/rcl/logging.c @@ -31,6 +31,7 @@ extern "C" #include "rcl/macros.h" #include "rcutils/logging.h" #include "rcutils/time.h" +#include "rcutils/stdatomic_helper.h" #define RCL_LOGGING_MAX_OUTPUT_FUNCS (4) @@ -43,6 +44,8 @@ static bool g_rcl_logging_stdout_enabled = false; static bool g_rcl_logging_rosout_enabled = false; static bool g_rcl_logging_ext_lib_enabled = false; +static atomic_uint_least64_t g_rcl_logging_init_count = ATOMIC_VAR_INIT(0); + /** * An output function that sends to multiple output appenders. */ @@ -64,10 +67,13 @@ rcl_logging_ext_lib_output_handler( const char * format, va_list * args); rcl_ret_t -rcl_logging_configure(const rcl_arguments_t * global_args, const rcl_allocator_t * allocator) +rcl_logging_init(const rcl_arguments_t * global_args, const rcl_allocator_t * allocator) { RCL_CHECK_ARGUMENT_FOR_NULL(global_args, RCL_RET_INVALID_ARGUMENT); RCL_CHECK_ARGUMENT_FOR_NULL(allocator, RCL_RET_INVALID_ARGUMENT); + if (rcutils_atomic_fetch_add_uint64_t(&g_rcl_logging_init_count, 1)) { + return RCL_RET_OK; + } RCUTILS_LOGGING_AUTOINIT g_logging_allocator = *allocator; int default_level = global_args->impl->log_level; @@ -107,6 +113,16 @@ rcl_logging_configure(const rcl_arguments_t * global_args, const rcl_allocator_t rcl_ret_t rcl_logging_fini() { rcl_ret_t status = RCL_RET_OK; + + if (rcutils_atomic_load_uint64_t(&g_rcl_logging_init_count) > 0) { + uint64_t current_count = rcutils_atomic_fetch_add_uint64_t(&g_rcl_logging_init_count, -1); + if (current_count != 1) { + return RCL_RET_OK; + } + } else { + return RCL_RET_OK; + } + rcutils_logging_set_output_handler(rcutils_logging_console_output_handler); if (g_rcl_logging_rosout_enabled) {