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

Force a write of all user-space buffered data for stdout in Formatter::print #1012

Merged
merged 2 commits into from
Mar 15, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tools/rosconsole/src/rosconsole/rosconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ std::string g_last_error_message = "Unknown Error";
#endif
const char* g_format_string = "[${severity}] [${time}]: ${message}";

bool g_force_stdout_line_buffered = false;
bool g_stdout_flush_failure_reported = false;

typedef std::map<std::string, std::string> M_string;
M_string g_extra_fixed_tokens;

Expand Down Expand Up @@ -393,6 +396,14 @@ void Formatter::print(void* logger_handle, ::ros::console::Level level, const ch
ss << COLOR_NORMAL;

fprintf(f, "%s\n", ss.str().c_str());

if (g_force_stdout_line_buffered && f == stdout) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have similar functionality for flushing stderr? Granted, stdout should be more common than stderr, but it seems odd to treat them differently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@IanTheEngineer It seems like stderr has no buffering at all and does not need to be flushed, as far as I know.

int flush_result = fflush(f);
if (flush_result != 0 && !g_stdout_flush_failure_reported) {
g_stdout_flush_failure_reported = true;
fprintf(stderr, "Error: failed to perform fflush on stdout, fflush return code is %d\n", flush_result);
}
}
}

Formatter g_formatter;
Expand Down Expand Up @@ -425,6 +436,19 @@ void initialize()
backend::function_notifyLoggerLevelsChanged = notifyLoggerLevelsChanged;
backend::function_print = _print;

std::string line_buffered;
if (get_environment_variable(line_buffered, "ROSCONSOLE_STDOUT_LINE_BUFFERED"))
{
if (line_buffered == "1") {
g_force_stdout_line_buffered = true;
} else {
if (line_buffered != "0") {
Copy link
Member

Choose a reason for hiding this comment

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

Last nitpick (sorry, didn't see it before): can you use else if for these two lines instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I will do it tomorrow. Currently I am quite far from my PC.

Copy link
Member

Choose a reason for hiding this comment

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

If you are ok with it I can just apply the change to your branch (thanks to the latest features in GitHub 😉).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure :)

Copy link
Member

Choose a reason for hiding this comment

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

Great, I changed it to else if in 52d9671 (plus some style adjustment to match the rest of the file since I was already in the editor 😉). Once CI has passed again this can be merged.

fprintf(stderr, "Warning: unexpected value %s specified for ROSCONSOLE_STDOUT_LINE_BUFFERED. Default value 0 "
"will be used. Valid values are 1 or 0.\n", line_buffered.c_str());
}
}
}

::ros::console::impl::initialize();
g_initialized = true;
}
Expand Down