Skip to content
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

Merged
merged 12 commits into from
Nov 16, 2017
Merged

Conversation

dhood
Copy link
Member

@dhood dhood commented Nov 9, 2017

requires #63

currently contains commits from #57 but they'll go away once it's merged

@dhood dhood added the in progress Actively being worked on (Kanban column) label Nov 9, 2017
@dhood dhood self-assigned this Nov 9, 2017
@dhood dhood force-pushed the logging_config_error_msg branch from e080db6 to c51a37c Compare November 9, 2017 19:35
@dhood
Copy link
Member Author

dhood commented Nov 9, 2017

Ready for review.

Follow up from the suggestion in https://github.com/ros2/rcutils/pull/57 to use RCUTILS_SET_ERROR_MSG:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@dhood dhood added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Nov 9, 2017
Copy link
Member

@wjwwood wjwwood left a 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.

@@ -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.
Copy link
Member

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).

Copy link
Member Author

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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.

Copy link
Member Author

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);
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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());
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member Author

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:

"rcutils_reset_error() should be called after error handling to avoid this.\n"
To me it looks like a bug if that gets output, so I avoided it since in this case overwriting the error message is intentional.

Copy link
Member Author

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();
Copy link
Member

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.

Copy link
Member Author

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();
Copy link
Member

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@dhood dhood added in progress Actively being worked on (Kanban column) and removed in review Waiting for review (Kanban column) labels Nov 9, 2017
@dhood dhood added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Nov 10, 2017
Copy link
Member

@wjwwood wjwwood left a 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.",
Copy link
Member

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.

Copy link
Member Author

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)
Copy link
Member

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?

Copy link
Member

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

Copy link
Member Author

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)

Copy link
Member

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)
Copy link
Member

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Member

@wjwwood wjwwood left a 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

@dhood dhood added in progress Actively being worked on (Kanban column) and removed in review Waiting for review (Kanban column) labels Nov 10, 2017
@dhood
Copy link
Member Author

dhood commented Nov 10, 2017

no worries, moving back into "in progress" in the meantime (but no work planned)

@dhood
Copy link
Member Author

dhood commented Nov 16, 2017

CI now that #63 been merged:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@dhood dhood merged commit 21b78ff into master Nov 16, 2017
@dhood dhood deleted the logging_config_error_msg branch November 16, 2017 18:38
@dhood dhood removed the in progress Actively being worked on (Kanban column) label Nov 16, 2017
dhood added a commit to ros2/rclpy that referenced this pull request Dec 1, 2017
rcutils logging sets error messages on failure as of ros2/rcutils#65
dhood added a commit to ros2/rclpy that referenced this pull request Dec 1, 2017
rcutils logging sets error messages on failure as of ros2/rcutils#65
dhood added a commit to ros2/rclpy that referenced this pull request Dec 1, 2017
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants