-
Notifications
You must be signed in to change notification settings - Fork 912
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
Changes from 1 commit
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 |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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) { | ||
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; | ||
|
@@ -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") { | ||
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. Last nitpick (sorry, didn't see it before): can you use 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. Sure, I will do it tomorrow. Currently I am quite far from my PC. 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 you are ok with it I can just apply the change to your branch (thanks to the latest features in GitHub 😉). 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. Sure :) 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. Great, I changed it to |
||
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; | ||
} | ||
|
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.
Should we have similar functionality for flushing
stderr
? Granted,stdout
should be more common thanstderr
, but it seems odd to treat them differently.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.
@IanTheEngineer It seems like stderr has no buffering at all and does not need to be flushed, as far as I know.