Skip to content

[WIN32] Only set the foreground console colour #129

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 7 additions & 2 deletions llvm/utils/benchmark/src/colorprint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,18 @@ void ColorPrintf(std::ostream& out, LogColor color, const char* fmt,
CONSOLE_SCREEN_BUFFER_INFO buffer_info;
GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
const WORD old_color_attrs = buffer_info.wAttributes;
WORD new_color_attrs;

// We need to flush the stream buffers into the console before each
// SetConsoleTextAttribute call lest it affect the text that is already
// printed but has not yet reached the console.
fflush(stdout);
SetConsoleTextAttribute(stdout_handle,
GetPlatformColorCode(color) | FOREGROUND_INTENSITY);

// Do not assume the console background color is black.
// Hence just mask off the foreground bits before adding the new value
new_color_attrs = (old_color_attrs & ~7) | FOREGROUND_INTENSITY |
GetPlatformColorCode(color);
SetConsoleTextAttribute(stdout_handle, new_color_attrs);
vprintf(fmt, args);

fflush(stdout);
Expand Down