From c23ac0545c7828f1b9661f00b83647dbc529669c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 28 Dec 2018 02:41:41 +0100 Subject: gui/init: output to MSVC debugger if attached --- gui/init.cpp | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'gui') diff --git a/gui/init.cpp b/gui/init.cpp index e30df777..9e6b79dc 100644 --- a/gui/init.cpp +++ b/gui/init.cpp @@ -95,20 +95,39 @@ static void set_qt_style() #ifdef _WIN32 +#include +#include +#include +#include + static void qdebug_to_console(QtMsgType, const QMessageLogContext& ctx, const QString &msg) { - const unsigned short* const str_ = msg.utf16(); - const auto str = reinterpret_cast(str_); - static_assert(sizeof(*str_) == sizeof(*str)); - - std::fflush(stderr); - if (ctx.function) - std::fprintf(stderr, "[%s:%d%s]: %ls\n", ctx.file, ctx.line, ctx.function, str); - else if (ctx.file) - std::fprintf(stderr, "[%s:%d]: %ls\n", ctx.file, ctx.line, str); + const auto str = (const wchar_t*)msg.utf16(); + static_assert(sizeof(*str) == sizeof(wchar_t)); + + if (IsDebuggerPresent()) + { + static std::atomic_flag lock = ATOMIC_FLAG_INIT; + + while (!lock.test_and_set()) + (void)0; + + OutputDebugStringW(str); + OutputDebugStringW(L"\n"); + + lock.clear(); + } else - std::fprintf(stderr, "%ls\n", str); - std::fflush(stderr); + { + std::fflush(stderr); + if (ctx.function) + std::fprintf(stderr, "[%s:%d%s]: %ls\n", ctx.file, ctx.line, 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); + } } static void add_win32_path() @@ -161,10 +180,11 @@ static void add_win32_path() } } -#include - static void attach_parent_console() { + if (GetConsoleWindow() != nullptr) + return; + fflush(stdin); fflush(stderr); -- cgit v1.2.3