Skip to content

Commit

Permalink
Fail if allocator invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
dhood committed Nov 9, 2017
1 parent 13cebe7 commit ac66ece
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
10 changes: 6 additions & 4 deletions include/rcutils/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ extern bool g_rcutils_logging_initialized;

/// Initialize the logging system using the specified allocator.
/**
* This function will always set the internal state to initialized even if an
* error occurs, to avoid repeated failing initialization attempts since this
* function is called automatically from logging macros.
* Initialize the logging system only if it was not in an initialized state.
* If an invalid allocator is passed, the initialization will fail.
* Otherwise, this function will still set the internal state to initialized
* even if an error occurs, to avoid repeated failing initialization attempts
* since this function is called automatically from logging macros.
* To re-attempt initialization after failure, call rcutils_logging_shutdown()
* before re-calling this function.
*
Expand All @@ -51,7 +53,7 @@ extern bool g_rcutils_logging_initialized;
* \param allocator rcutils_allocator_t to be used.
* \return `RCUTILS_RET_OK` if successful.
* \retrun `RCUTILS_RET_INVALID_ARGUMENT` if the allocator is invalid, in which
* case the default allocator will be used.
* case initialization will fail.
* \return `RCUTILS_RET_INVALID_ARGUMENT` if an error occurs reading the output
* format from the `RCUTILS_CONSOLE_OUTPUT_FORMAT` environment variable, in
* which case the default format will be used.
Expand Down
9 changes: 2 additions & 7 deletions src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allo
rcutils_ret_t ret = RCUTILS_RET_OK;
if (!g_rcutils_logging_initialized) {
if (!rcutils_allocator_is_valid(&allocator)) {
allocator = rcutils_get_default_allocator();
RCUTILS_SET_ERROR_MSG(
"Provided allocator is invalid. Using the default allocator.", allocator);
ret = RCUTILS_RET_INVALID_ARGUMENT;
"Provided allocator is invalid.", rcutils_get_default_allocator());
return RCUTILS_RET_INVALID_ARGUMENT;
}
g_rcutils_logging_allocator = allocator;

Expand All @@ -87,10 +86,6 @@ rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allo
g_rcutils_logging_output_format_string[chars_to_copy] = '\0';
} else {
if (NULL != ret_str) {
if (rcutils_error_is_set()) {
fprintf(stderr, "Overwriting error message: %s\n", rcutils_get_error_string_safe());
rcutils_reset_error();
}
RCUTILS_SET_ERROR_MSG(
"Failed to get output format from env. variable. Using default output format.",
g_rcutils_logging_allocator);
Expand Down

0 comments on commit ac66ece

Please sign in to comment.