From 4766be01022d6de50f7b42647299860c2d9909b8 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 2 Jan 2019 14:26:54 +0100 Subject: gui/init: avoid allocations in logger --- gui/init.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'gui/init.cpp') diff --git a/gui/init.cpp b/gui/init.cpp index 3fe9813f..ea46edd3 100644 --- a/gui/init.cpp +++ b/gui/init.cpp @@ -98,6 +98,9 @@ static void set_qt_style() #ifdef _WIN32 # include +# include +#else +# include #endif static void qdebug_to_console(QtMsgType, const QMessageLogContext& ctx, const QString &msg) @@ -118,19 +121,23 @@ static void qdebug_to_console(QtMsgType, const QMessageLogContext& ctx, const QS else #endif { - QByteArray bytes{msg.toUtf8()}; - - std::fflush(stderr); - const char* const s = bytes.constData(); +#if defined _WIN32 && 1 + const wchar_t* const bytes = (const wchar_t*)msg.utf16(); +#else + unsigned len = (unsigned)msg.size()+1; + wchar_t* const bytes = (wchar_t*)alloca(len * sizeof(wchar_t)); + bytes[len-1] = 0; + (void)msg.toWCharArray(bytes); +#endif { spinlock_guard l(lock); if (ctx.function) - std::fprintf(stderr, "[%s:%d] %s: %s\n", ctx.file, ctx.line, ctx.function, s); + std::fprintf(stderr, "[%s:%d] %s: %ls\n", ctx.file, ctx.line, ctx.function, bytes); else if (ctx.file) - std::fprintf(stderr, "[%s:%d]: %s\n", ctx.file, ctx.line, s); + std::fprintf(stderr, "[%s:%d]: %ls\n", ctx.file, ctx.line, bytes); else - std::fprintf(stderr, "%s\n", s); + std::fprintf(stderr, "%ls\n", bytes); } std::fflush(stderr); } -- cgit v1.2.3