From a049325cc708b483ed1aadc6589419f4d74b19f3 Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Mon, 20 Jan 2020 15:54:42 +0100 Subject: Win: work around (estimated) 32k char limit for OutputDebugString MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reaching a certain number of characters, OutputDebugString will just eat the string and not give any output. As there is no way of handling that error properly we divide the string into usable chunks. Fixes: QTBUG-80996 Change-Id: Ic7ef34c48c212cbaec3a03790d1020506b7b4319 Reviewed-by: Miguel Costa Reviewed-by: Friedemann Kleint Reviewed-by: MÃ¥rten Nordheim --- src/corelib/global/qlogging.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 5a7f8242de..c1dd04f03c 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -1669,14 +1669,34 @@ static bool android_default_message_handler(QtMsgType type, #endif //Q_OS_ANDROID #ifdef Q_OS_WIN +static void win_outputDebugString_helper(QStringView message) +{ + const int maxOutputStringLength = 32766; + static QBasicMutex m; + auto locker = qt_unique_lock(m); + // fast path: Avoid string copies if one output is enough + if (message.length() <= maxOutputStringLength) { + OutputDebugString(reinterpret_cast(message.utf16())); + } else { + wchar_t *messagePart = new wchar_t[maxOutputStringLength + 1]; + for (int i = 0; i < message.length(); i += maxOutputStringLength ) { + const int length = std::min(message.length() - i, maxOutputStringLength ); + const int len = message.mid(i, length).toWCharArray(messagePart); + Q_ASSERT(len == length); + messagePart[len] = 0; + OutputDebugString(messagePart); + } + delete[] messagePart; + } +} + static bool win_message_handler(QtMsgType type, const QMessageLogContext &context, const QString &message) { if (shouldLogToStderr()) return false; // Leave logging up to stderr handler - QString formattedMessage = qFormatLogMessage(type, context, message); - formattedMessage.append(QLatin1Char('\n')); - OutputDebugString(reinterpret_cast(formattedMessage.utf16())); + const QString formattedMessage = qFormatLogMessage(type, context, message).append('\n'); + win_outputDebugString_helper(formattedMessage); return true; // Prevent further output to stderr } @@ -1832,11 +1852,11 @@ static void qt_message_print(QtMsgType msgType, const QMessageLogContext &contex static void qt_message_print(const QString &message) { #if defined(Q_OS_WINRT) - OutputDebugString(reinterpret_cast(message.utf16())); + win_outputDebugString_helper(message); return; #elif defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED) if (!shouldLogToStderr()) { - OutputDebugString(reinterpret_cast(message.utf16())); + win_outputDebugString_helper(message); return; } #endif -- cgit v1.2.3