summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2019-01-02 10:01:54 +0100
committerStanislaw Halik <sthalik@misaki.pl>2019-01-16 07:48:19 +0100
commitbc3c13ecc3692aca160ccfe4043c910dc1bd7e85 (patch)
tree2b713a00841fd5a8fea8bae69db8558096019408
parent910fdea2a904cf17afc8ec0bcb1869b647acbb5e (diff)
gui/init: enable custom Qt logger on non-win32
-rw-r--r--gui/init.cpp64
1 files changed, 40 insertions, 24 deletions
diff --git a/gui/init.cpp b/gui/init.cpp
index 2b84beef..66fceb3f 100644
--- a/gui/init.cpp
+++ b/gui/init.cpp
@@ -93,40 +93,56 @@ static void set_qt_style()
#endif
}
-#ifdef _WIN32
-
#include "compat/spinlock.hpp"
+#include <string>
-#include <cstring>
-#include <cwchar>
-#include <windows.h>
+#ifdef _WIN32
+# include <windows.h>
+#endif
static void qdebug_to_console(QtMsgType, const QMessageLogContext& ctx, const QString &msg)
{
- const auto str = (const wchar_t*)msg.utf16();
- static_assert(sizeof(*str) == sizeof(wchar_t));
+ static std::atomic_flag lock = ATOMIC_FLAG_INIT;
+ const auto bytes{msg.toUtf8()};
- if (IsDebuggerPresent())
- {
- static std::atomic_flag lock = ATOMIC_FLAG_INIT;
- spinlock_guard l(lock);
+ constexpr bool is_win32 =
+#ifdef _WIN32
+ true;
+#else
+ false;
+#endif
- OutputDebugStringW(str);
- OutputDebugStringW(L"\n");
- }
- else
+
+ if constexpr (is_win32)
{
- 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);
+ if (IsDebuggerPresent())
+ {
+ spinlock_guard l(lock);
+
+ OutputDebugStringA(bytes.constData());
+ OutputDebugStringA("\n");
+ }
else
- std::fprintf(stderr, "%ls\n", str);
- std::fflush(stderr);
+ {
+ std::fflush(stderr);
+ const char* const s = bytes.constData();
+ {
+ spinlock_guard l(lock);
+
+ if (ctx.function)
+ std::fprintf(stderr, "[%s:%d] %s: %s\n", ctx.file, ctx.line, ctx.function, s);
+ else if (ctx.file)
+ std::fprintf(stderr, "[%s:%d]: %s\n", ctx.file, ctx.line, s);
+ else
+ std::fprintf(stderr, "%s\n", s);
+ }
+ std::fflush(stderr);
+ }
}
}
+#ifdef _WIN32
+
static void add_win32_path()
{
// see https://software.intel.com/en-us/articles/limitation-to-the-length-of-the-system-path-variable
@@ -198,8 +214,6 @@ static void attach_parent_console()
fprintf(stderr, "\n");
fflush(stderr);
}
-
- (void)qInstallMessageHandler(qdebug_to_console);
}
#endif
@@ -235,6 +249,8 @@ int otr_main(int argc, char** argv, std::function<QWidget*()> const& make_main_w
attach_parent_console();
#endif
+ (void)qInstallMessageHandler(qdebug_to_console);
+
QDir::setCurrent(OPENTRACK_BASE_PATH);
set_qt_style();