From 6550bc252ff89e052fed5a3a3d3ec44d42a0c810 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Tue, 2 Jan 2024 14:32:52 +0100 Subject: [PATCH] Fix logic in porting::attachOrCreateConsole() No functional change but now the comment is actually correct. --- src/porting.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/porting.cpp b/src/porting.cpp index 0165af0298..76c3ae9857 100644 --- a/src/porting.cpp +++ b/src/porting.cpp @@ -737,12 +737,14 @@ void osSpecificInit() void attachOrCreateConsole() { #ifdef _WIN32 - static bool consoleAllocated = false; - const bool redirected = (_fileno(stdout) == -2 || _fileno(stdout) == -1); // If output is redirected to e.g a file - if (!consoleAllocated && redirected && (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())) { - freopen("CONOUT$", "w", stdout); - freopen("CONOUT$", "w", stderr); - consoleAllocated = true; + static bool once = false; + const bool redirected = _fileno(stdout) >= 0; // If output is redirected to e.g a file + if (!once && !redirected) { + if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole()) { + freopen("CONOUT$", "w", stdout); + freopen("CONOUT$", "w", stderr); + } + once = true; } #endif }