From 08a3b8a323441fd03919a042944aeaedb4aead17 Mon Sep 17 00:00:00 2001 From: Gisle Vanem Date: Wed, 12 Feb 2020 18:34:34 +0100 Subject: [PATCH] [WIN32] Only set the foreground console colour Do not simply assume the console background colour is black. --- llvm/utils/benchmark/src/colorprint.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/llvm/utils/benchmark/src/colorprint.cc b/llvm/utils/benchmark/src/colorprint.cc index fff6a98818b84..e2d29400eea78 100644 --- a/llvm/utils/benchmark/src/colorprint.cc +++ b/llvm/utils/benchmark/src/colorprint.cc @@ -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);