From 1930bfeabb522781d5825bdee123a07216e62b13 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Sat, 13 Apr 2024 17:09:48 +0100 Subject: [PATCH 1/6] Support coloured terminal output on Windows First try the modern Windowsy way, where we can directly query if escape sequences will be processed. The function is available as far back as Windows 2000, but it just won't return the right flag until the Windows version is new enough. If that fails, fall back to the Unixy way, as not all colour-supporting terminal emulators for Windows use the Win32 API to declare that capability. The implementation isn't identical as isatty wasn't available without adding more headers, and we already have Windows.h in this file, so I might as well use the Win32 API instead of its POSIX-compatibility layer. --- components/debug/debugging.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index e9e50ff8367..bfde558c859 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -256,9 +256,17 @@ namespace Debug private: static bool useColoredOutput() { - // Note: cmd.exe in Win10 should support ANSI colors, but in its own way. #if defined(_WIN32) - return 0; + if (getenv("NO_COLOR")) + return false; + + DWORD mode; + if (GetConsoleMode(GetStdHandle(STD_ERROR_HANDLE), &mode) && mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) + return true; + + // some console emulators may not use the Win32 API, so try the Unixy approach + char* term = getenv("TERM"); + return term && GetFileType(GetStdHandle(STD_ERROR_HANDLE)) == FILE_TYPE_CHAR; #else char* term = getenv("TERM"); bool useColor = term && !getenv("NO_COLOR") && isatty(fileno(stderr)); From ea029b06eabc98d620ac63b9f6c60730708e601a Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Sat, 13 Apr 2024 18:51:45 +0100 Subject: [PATCH 2/6] Remove unused define --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 93da5feec4a..9e6e788c05e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -710,7 +710,6 @@ if (WIN32) if (USE_DEBUG_CONSOLE AND BUILD_OPENMW) set_target_properties(openmw PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") set_target_properties(openmw PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE") - set_target_properties(openmw PROPERTIES COMPILE_DEFINITIONS $<$:_CONSOLE>) elseif (BUILD_OPENMW) # Turn off debug console, debug output will be written to visual studio output instead set_target_properties(openmw PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:WINDOWS") From 901a17ab81a4814d51dc7b5531cfef3cbdcabec3 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Sat, 13 Apr 2024 19:01:34 +0100 Subject: [PATCH 3/6] Make comments stop lying --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e6e788c05e..3360ac2e2d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -234,7 +234,7 @@ else() endif(APPLE) if (WIN32) - option(USE_DEBUG_CONSOLE "whether a debug console should be enabled for debug builds, if false debug output is redirected to Visual Studio output" ON) + option(USE_DEBUG_CONSOLE "Whether a console should be displayed if OpenMW isn't launched from the command line. Does not affect the Release configuration." ON) endif() if(MSVC) @@ -711,7 +711,7 @@ if (WIN32) set_target_properties(openmw PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") set_target_properties(openmw PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE") elseif (BUILD_OPENMW) - # Turn off debug console, debug output will be written to visual studio output instead + # Turn off implicit console, you won't see stdout unless launching OpenMW from a command line shell or look at openmw.log set_target_properties(openmw PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:WINDOWS") set_target_properties(openmw PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:WINDOWS") endif() From a7021bf9cc6cfc9111505e95ebb83d2921d2f13e Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Tue, 16 Apr 2024 01:10:39 +0100 Subject: [PATCH 4/6] Clear std stream errors when reopening Prior errors are no longer relevant. Shouldn't make a difference unless you've tried printing something before the streams were set up. --- components/debug/debugging.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index bfde558c859..0699d0912b0 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -73,16 +73,19 @@ namespace Debug { _wfreopen(L"CON", L"r", stdin); freopen("CON", "r", stdin); + std::cin.clear(); } if (!outRedirected) { _wfreopen(L"CON", L"w", stdout); freopen("CON", "w", stdout); + std::cout.clear(); } if (!errRedirected) { _wfreopen(L"CON", L"w", stderr); freopen("CON", "w", stderr); + std::cerr.clear(); } return true; From 61364c874f09f61870b66a6414496f741a2030ac Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Tue, 16 Apr 2024 01:14:20 +0100 Subject: [PATCH 5/6] Warn future me off wasting their time again --- components/debug/debugging.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index 0699d0912b0..e6caf2b09d4 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -61,6 +61,10 @@ namespace Debug bool outRedirected = isRedirected(STD_OUTPUT_HANDLE); bool errRedirected = isRedirected(STD_ERROR_HANDLE); + // Note: Do not spend three days reinvestigating this PowerShell bug thinking its our bug. + // https://gitlab.com/OpenMW/openmw/-/merge_requests/408#note_447467393 + // The handles look valid, but GetFinalPathNameByHandleA can't tell what files they go to and writing to them doesn't work. + if (AttachConsole(ATTACH_PARENT_PROCESS)) { fflush(stdout); From 83e3718bed550faefca66080eb66fb227fcd3f0a Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Tue, 16 Apr 2024 13:14:36 +0100 Subject: [PATCH 6/6] . c l a n g - f o r m a t --- components/debug/debugging.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index e6caf2b09d4..67e7ecaaf35 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -63,7 +63,8 @@ namespace Debug // Note: Do not spend three days reinvestigating this PowerShell bug thinking its our bug. // https://gitlab.com/OpenMW/openmw/-/merge_requests/408#note_447467393 - // The handles look valid, but GetFinalPathNameByHandleA can't tell what files they go to and writing to them doesn't work. + // The handles look valid, but GetFinalPathNameByHandleA can't tell what files they go to and writing to them + // doesn't work. if (AttachConsole(ATTACH_PARENT_PROCESS)) {