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 all commits
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
27 changes: 27 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,16 @@ 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;
Expand Down Expand Up @@ -425,6 +438,20 @@ 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")
{
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