1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-19 22:11:34 +00:00

Merge pull request #2064 from sthalik/pr/win32-debug-parent-console

enable win32 debug console when launching from console
This commit is contained in:
Bret Curtis 2018-12-12 21:15:07 +01:00 committed by GitHub
commit 97aa6f58e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 5 deletions

View file

@ -2,8 +2,42 @@
#include <components/crashcatcher/crashcatcher.hpp> #include <components/crashcatcher/crashcatcher.hpp>
#ifdef _WIN32
# undef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
namespace Debug 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) std::streamsize DebugOutputBase::write(const char *str, std::streamsize size)
{ {
// Skip debug level marker // 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) 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 // Some objects used to redirect cout and cerr
// Scope must be here, so this still works inside the catch block for logging exceptions // Scope must be here, so this still works inside the catch block for logging exceptions
std::streambuf* cout_rdbuf = std::cout.rdbuf (); std::streambuf* cout_rdbuf = std::cout.rdbuf ();

View file

@ -10,6 +10,12 @@
#include "debuglog.hpp" #include "debuglog.hpp"
#if defined _WIN32 && defined _DEBUG
# undef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
namespace Debug namespace Debug
{ {
// ANSI colors for terminal // ANSI colors for terminal
@ -43,11 +49,11 @@ namespace Debug
} }
}; };
#if defined(_WIN32) && defined(_DEBUG) #ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN bool attachParentConsole();
#define WIN32_LEAN_AND_MEAN 1 #endif
#endif // !WIN32_LEAN_AND_MEAN
#include <Windows.h> #if defined _WIN32 && defined _DEBUG
class DebugOutput : public DebugOutputBase class DebugOutput : public DebugOutputBase
{ {
public: public: