-
Notifications
You must be signed in to change notification settings - Fork 102
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
Use RCUTILS_SET_ERROR_MSG in logging config #65
Conversation
e080db6
to
c51a37c
Compare
Ready for review. Follow up from the suggestion in https://github.com/ros2/rcutils/pull/57 to use
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I know some of these comments apply to existing code, but I guess I missed them when they were added.
include/rcutils/logging.h
Outdated
@@ -58,6 +58,8 @@ extern bool g_rcutils_logging_initialized; | |||
* \return `RCUTILS_RET_LOGGING_SEVERITY_MAP_INVALID` if the internal logger | |||
* severity map cannot be initialized, in which case logger severity | |||
* thresholds will not be configurable. | |||
* \return `RCUTILS_RET_MULTIPLE_ERRORS` if multiple errors occur, in which | |||
* case the error message will contain that of the last error to occur. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmm, I don't think this makes sense. Consider a case where an invalid argument error also produces a bad alloc while formatting the error message for instance... It would be better to receive either ret code than this one imo. With this error code you only know that something went wrong, but not what went wrong without parsing the error string (which is slow and might change over time). Part of the point of the ret codes is so the calling code can react to the issue within the called function, but this return code doesn't give you any useful information in that respect. I'd rather have one of the errors printed to stderr, or added to the error message, and the other one returned normally (with error message set and the proper return code).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I see now that "return an error return code if there are any accumulated" in ros2/rmw_connext#265 doesn't necessarily mean it is a "special" error code. I follow your reasoning and will change it to be one of the existing codes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking, for that other comment, that it would return multiple return codes, maybe a list of them or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok, thanks for clarifying (I can't find the full context of that quote of yours so I'm inventing a lot of context! )
src/logging.c
Outdated
if (!rcutils_allocator_is_valid(&allocator)) { | ||
allocator = rcutils_get_default_allocator(); | ||
RCUTILS_SET_ERROR_MSG( | ||
"Provided allocator is invalid. Using the default allocator.", allocator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually feel like this should just case the function to fail, not fallback to the default allocator. If the allocator object itself is invalid, then that is a logic error in the program.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the "fallback" behaviour in general was put there because I was worried that the logging macros repeatedly calling RCUTILS_LOGGING_AUTOINIT
and ignoring the return code would cause hard failures to go unnoticed. However, that argument doesn't apply here since the autoinit macro won't pass a custom allocator anyway, so I will remove this fallback to the default allocator and it is the caller's responsibility to check the return code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ac66ece removes the fallback
src/logging.c
Outdated
ret_str); | ||
ret = RCUTILS_RET_INVALID_ARGUMENT; | ||
if (rcutils_error_is_set()) { | ||
fprintf(stderr, "Overwriting error message: %s\n", rcutils_get_error_string_safe()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RCUTILS_SET_ERROR_MSG
does this already when you override an error state without clearing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but it gives a help-like output suggesting you should reset the error message, so to me it seems more user friendly to do it manually where possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understood this bit, do you mean that places invoking RCUTILS_SET_ERROR_MSG
should add this print when they need to reset the error message because the one printed by RCUTILS_SET_ERROR_MSG
is not clear ?
Would making RCUTILS_SET_ERROR_MSG
overwrite message clearer solve this without having to add printfs manually wherever we reset the error message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would making RCUTILS_SET_ERROR_MSG overwrite message clearer solve this without having to add printfs manually wherever we reset the error message?
we could change it, yeah, but to me the issue is that it's a little too helpful at the moment, so it would actually have to be made what can be considered less clear:
Line 157 in f9a0537
"rcutils_reset_error() should be called after error handling to avoid this.\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the previous error now causes a return (ac66ece), this code has gone away naturally 😄
src/logging.c
Outdated
g_rcutils_logging_severities_map = rcutils_get_zero_initialized_string_map(); | ||
rcutils_ret_t string_map_ret = rcutils_string_map_init( | ||
&g_rcutils_logging_severities_map, 0, g_rcutils_logging_allocator); | ||
if (string_map_ret != RCUTILS_RET_OK) { | ||
fprintf( | ||
stderr, | ||
"Failed to initialize map for logger severities. Severities will not be configurable.\n"); | ||
"Failed to initialize logging severities map: %s\n", rcutils_get_error_string_safe()); | ||
rcutils_reset_error(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would incorporate this into your new error state but not print anything here. Consider it being something like this:
try:
init_map(...)
except SomeMapError as exc:
raise MyError("could not setup logging, because: %s" % str(exc))
Basically you're catching the string map error, and "re throwing" you're own more applicable error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
10373ef does this 👍
src/logging.c
Outdated
fprintf( | ||
stderr, | ||
"Failed to finalize logging severities map: %s\n", rcutils_get_error_string_safe()); | ||
rcutils_reset_error(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing here, I'd prefer to integrate this error into the new error message and not print anything to stderr.
src/logging.c
Outdated
"Error setting severity for logger named '%s': %s\n", name, rcutils_get_error_string_safe()); | ||
rcutils_reset_error(); | ||
RCUTILS_SET_ERROR_MSG( | ||
"Error setting severity for logger", g_rcutils_logging_allocator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
This reverts commit a81295a.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new macro for setting the error state with a format string might be of interest: https://github.com/ros2/rcutils/pull/63/files#diff-1b06d4a1ccca0f0dff66d961923143a1R119
It's not yet merged.
src/logging.c
Outdated
"Failed to get output format from env. variable: %s. Using default output format.\n", | ||
ret_str); | ||
RCUTILS_SET_ERROR_MSG( | ||
"Failed to get output format from env. variable. Using default output format.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good if this error message could include the ret_str
in some way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in 2d5f39b
src/logging.c
Outdated
"Failed to initialize map for logger severities [%s]. Severities will not be configurable.", | ||
rcutils_get_error_string_safe()); | ||
rcutils_reset_error(); | ||
RCUTILS_SET_ERROR_MSG(msg, g_rcutils_logging_allocator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if msg
is NULL
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this line could also set the error state: https://github.com/ros2/rcutils/blob/master/src/format_string.c#L41
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've marked this PR as requiring #63 and swapped to use RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING
in a088188 which will take care of the NULL case.
I think I've missed your point regarding the format_string allocator check: I don't think that RCUTILS_CHECK_ALLOCATOR
sets the error state or error message (RCUTILS_CHECK_ALLOCATOR_WITH_MSG
would though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, w.r.t. the check allocator call 👍
src/logging.c
Outdated
"Failed to finalize map for logger severities: %s", | ||
rcutils_get_error_string_safe()); | ||
rcutils_reset_error(); | ||
RCUTILS_SET_ERROR_MSG(msg, g_rcutils_logging_allocator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, what if msg
is NULL
?
src/logging.c
Outdated
g_rcutils_logging_allocator, | ||
"Error setting severity for logger named '%s': %s", name, rcutils_get_error_string_safe()); | ||
rcutils_reset_error(); | ||
RCUTILS_SET_ERROR_MSG(msg, g_rcutils_logging_allocator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, thanks for iterating
I'll try to get #63 merged asap
no worries, moving back into "in progress" in the meantime (but no work planned) |
CI now that #63 been merged: |
rcutils logging sets error messages on failure as of ros2/rcutils#65
rcutils logging sets error messages on failure as of ros2/rcutils#65
* Clear rcutils error on failure rcutils logging sets error messages on failure as of ros2/rcutils#65 * logging_macros.h isn't needed anymore RCUTILS_LOGGING_AUTOINIT was moved to rcutils/logging.h
requires #63
currently contains commits from #57 but they'll go away once it's merged