-
Notifications
You must be signed in to change notification settings - Fork 199
Ensure no redundant rcl_logging initialization and finalization. #560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
2a141c2
6602f6c
1bf89b8
894e883
3df75c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 __rcl_logging_init_count = ATOMIC_VAR_INIT(0); | ||
|
|
||
| /** | ||
| * An output function that sends to multiple output appenders. | ||
| */ | ||
|
|
@@ -64,10 +67,11 @@ 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); | ||
|
|
||
|
fujitatomoya marked this conversation as resolved.
Outdated
|
||
| RCUTILS_LOGGING_AUTOINIT | ||
| g_logging_allocator = *allocator; | ||
| int default_level = global_args->impl->log_level; | ||
|
|
@@ -77,6 +81,7 @@ rcl_logging_configure(const rcl_arguments_t * global_args, const rcl_allocator_t | |
| g_rcl_logging_ext_lib_enabled = !global_args->impl->log_ext_lib_disabled; | ||
| rcl_ret_t status = RCL_RET_OK; | ||
| g_rcl_logging_num_out_handlers = 0; | ||
| (void) rcutils_atomic_fetch_add_uint64_t(&__rcl_logging_init_count, 1); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if multiple contexts exist, this will be called more than once. so that there should be no warning.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that the return value should be used. if(rcutils_atomic_fetch_add_uint64_t(&__rcl_logging_init_count, 1)) {
return RMW_RET_OK; // already inited, just return.
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think this should be the first thing done in the function.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are not sure rcl_logging_init is actually done without any error, what if argument is missing? |
||
|
|
||
| if (default_level >= 0) { | ||
| rcutils_logging_set_default_logger_level(default_level); | ||
|
|
@@ -107,6 +112,12 @@ 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; | ||
| uint64_t current_count = rcutils_atomic_fetch_add_uint64_t(&__rcl_logging_init_count, -1); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using atomic is not exactly thread safe, but expecting good enough for this case. besides we do not really want to introduce mutex in here. |
||
|
|
||
| if (current_count != 1) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't be
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, current_count is fetched before addition(subtraction), confirmed this with simple test program.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, sorry you're right. But |
||
| return status; | ||
| } | ||
|
|
||
| rcutils_logging_set_output_handler(rcutils_logging_console_output_handler); | ||
|
|
||
| if (g_rcl_logging_rosout_enabled) { | ||
|
|
@@ -115,7 +126,6 @@ rcl_ret_t rcl_logging_fini() | |
| if (RCL_RET_OK == status && g_rcl_logging_ext_lib_enabled) { | ||
| status = rcl_logging_external_shutdown(); | ||
| } | ||
|
|
||
|
fujitatomoya marked this conversation as resolved.
|
||
| return status; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.