From 64e960aa22d2bdc1a6d589b344010408c7cc735c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 8 Dec 2018 01:29:14 +0100 Subject: [PATCH] enable win32 debug console when launching from console --- components/CMakeLists.txt | 2 +- components/debug/debugging.cpp | 5 +++ components/debug/debugging.hpp | 3 ++ components/debug/win32.cpp | 56 ++++++++++++++++++++++++++++++++++ components/debug/win32.hpp | 5 +++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 components/debug/win32.cpp create mode 100644 components/debug/win32.hpp diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index 7a88dc18e..e34a95396 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -90,7 +90,7 @@ add_component_dir (misc ) add_component_dir (debug - debugging debuglog + debugging debuglog win32 ) IF(NOT WIN32 AND NOT APPLE) diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index e89a65956..9798950b9 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -52,6 +52,11 @@ namespace Debug int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[], const std::string& appName) { +#if defined _WIN32 + // grab a console window if we don't have one + (void)Debug::attachParentConsole(); +#endif + // Some objects used to redirect cout and cerr // Scope must be here, so this still works inside the catch block for logging exceptions std::streambuf* cout_rdbuf = std::cout.rdbuf (); diff --git a/components/debug/debugging.hpp b/components/debug/debugging.hpp index 361f321cb..440e8fa47 100644 --- a/components/debug/debugging.hpp +++ b/components/debug/debugging.hpp @@ -9,6 +9,9 @@ #include #include "debuglog.hpp" +#if defined _WIN32 +# include "win32.hpp" +#endif namespace Debug { diff --git a/components/debug/win32.cpp b/components/debug/win32.cpp new file mode 100644 index 000000000..9b038343c --- /dev/null +++ b/components/debug/win32.cpp @@ -0,0 +1,56 @@ +#include "win32.hpp" + +#undef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS + +#include +#include + +#include + +namespace Debug { + +bool attachParentConsole() +{ + // we already have a console window + if (GetConsoleWindow() != nullptr) + return true; + + // our parent window has a console we can use + if (AttachConsole(ATTACH_PARENT_PROCESS)) + { + // start with consistent state + fflush(stdin); + fflush(stderr); + std::cout.flush(); + std::cerr.flush(); + + // fix fprintf(3) and fwprintf(3) + // this looks strange, but nothing is ever simple on Windows. + _wfreopen(L"CON", L"w", stdout); + _wfreopen(L"CON", L"w", stderr); + _wfreopen(L"CON", L"r", stdin); + freopen("CON", "w", stdout); + freopen("CON", "w", stderr); + freopen("CON", "w", stderr); + + // it can be verified that input/output works as expected. +#if 0 + fprintf(stdout, "ascii stdout\n"); + fwprintf(stdout, L"wide stdout\n"); + fprintf(stderr, "ascii stderr\n"); + fwprintf(stderr, L"wide stderr\n"); + + std::cout << "ascii cout\n"; + std::cout << L"wide cout\n"; + std::cerr << "ascii cerr\n"; + std::cerr << L"wide cerr\n"; +#endif + + return true; + } + + return false; +} + +} // ns Debug diff --git a/components/debug/win32.hpp b/components/debug/win32.hpp new file mode 100644 index 000000000..5a103cce5 --- /dev/null +++ b/components/debug/win32.hpp @@ -0,0 +1,5 @@ +#pragma once + +namespace Debug { + bool attachParentConsole(); +}