From 134c1edae4c7582c1839f3c63296a4a37541ace8 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 27 May 2017 12:24:55 +0200 Subject: main: reopen parent console Running under CLion doesn't produce any output otherwise. --- gui/main.cpp | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/gui/main.cpp b/gui/main.cpp index 16e08b6e..48fe8f74 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -1,11 +1,11 @@ #ifdef _WIN32 -# include "opentrack-library-path.h" +# include # include # include -# include # include # include # include +# include #else # include #endif @@ -46,6 +46,35 @@ void set_qt_style() #ifdef _WIN32 +void qdebug_to_console(QtMsgType, const QMessageLogContext& ctx, const QString &msg) +{ + const unsigned short* const str_ = msg.utf16(); + auto str = reinterpret_cast(str_); + static_assert(sizeof(*str_) == sizeof(*str), ""); + + std::fflush(stderr); + if (ctx.function) + std::fprintf(stderr, "[%s]: %ls\n", ctx.function, str); + else if (ctx.file) + std::fprintf(stderr, "[%s:%d]: %ls\n", ctx.file, ctx.line, str); + else + std::fprintf(stderr, "%ls\n", str); + std::fflush(stderr); +} + +void attach_parent_console() +{ + if (AttachConsole(ATTACH_PARENT_PROCESS)) + { + // XXX c++ iostreams aren't reopened + + _wfreopen(L"CON", L"w", stdout); + _wfreopen(L"CON", L"w", stderr); + _wfreopen(L"CON", L"r", stdin); + qInstallMessageHandler(qdebug_to_console); + } +} + void add_win32_path() { // see https://software.intel.com/en-us/articles/limitation-to-the-length-of-the-system-path-variable @@ -105,6 +134,10 @@ WINAPI #endif main(int argc, char** argv) { +#ifdef _WIN32 + attach_parent_console(); +#endif + #if QT_VERSION >= 0x050600 // flag introduced in QT 5.6. It is non-essential so might as well allow compilation on older systems. QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif -- cgit v1.2.3