From 8851e779cf43e206c8eeb24229e26d8f6608e3cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Je=C5=99=C3=A1bek?= Date: Fri, 4 Sep 2020 12:27:16 +0200 Subject: [PATCH] console colour: fix unintended colouring of user's stderr on POSIX At some places, the colour reset code is printed after a newline. Since the default output buffering to console is line-based, the reset code is not actually written out. If messages from user code are printed to stderr (different stream, same console), they are printed before the colour reset code, and thus they are coloured. Explicitly flushing the stream after writing the colour escape code solves this. --- src/catch2/internal/catch_console_colour.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/catch2/internal/catch_console_colour.cpp b/src/catch2/internal/catch_console_colour.cpp index 93b5473e..e524b0ba 100644 --- a/src/catch2/internal/catch_console_colour.cpp +++ b/src/catch2/internal/catch_console_colour.cpp @@ -157,8 +157,10 @@ namespace { private: void setColour( const char* _escapeCode ) { + // The escape sequence must be flushed to console, otherwise if + // stdin and stderr are intermixed, we'd get accidentally coloured output. getCurrentContext().getConfig()->stream() - << '\033' << _escapeCode; + << '\033' << _escapeCode << std::flush; } };