diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index e89a65956..2d05fbc55 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -2,8 +2,42 @@ #include +#ifdef _WIN32 +# undef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# include +#endif + namespace Debug { +#ifdef _WIN32 + bool attachParentConsole() + { + if (GetConsoleWindow() != nullptr) + return true; + + if (AttachConsole(ATTACH_PARENT_PROCESS)) + { + fflush(stdout); + fflush(stderr); + std::cout.flush(); + std::cerr.flush(); + + // this looks dubious but is really the right way + _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", "r", stdin); + + return true; + } + + return false; + } +#endif + std::streamsize DebugOutputBase::write(const char *str, std::streamsize size) { // Skip debug level marker @@ -52,6 +86,10 @@ namespace Debug int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[], const std::string& appName) { +#if defined _WIN32 + (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..81b01d055 100644 --- a/components/debug/debugging.hpp +++ b/components/debug/debugging.hpp @@ -10,6 +10,12 @@ #include "debuglog.hpp" +#if defined _WIN32 && defined _DEBUG +# undef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# include +#endif + namespace Debug { // ANSI colors for terminal @@ -43,11 +49,11 @@ namespace Debug } }; -#if defined(_WIN32) && defined(_DEBUG) -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN 1 -#endif // !WIN32_LEAN_AND_MEAN -#include +#ifdef _WIN32 + bool attachParentConsole(); +#endif + +#if defined _WIN32 && defined _DEBUG class DebugOutput : public DebugOutputBase { public: