diff options
author | Kai Koehne <kai.koehne@theqtcompany.com> | 2014-10-16 15:12:59 +0200 |
---|---|---|
committer | Kai Koehne <kai.koehne@theqtcompany.com> | 2014-10-27 08:59:02 +0100 |
commit | 5bfe794aaab34d6aa021b5640972509e3bc6ced8 (patch) | |
tree | 3d4357aa5cb927c66aac33ecba59b0b36697d55a | |
parent | 1ffe1a9a7c5199e46bf280806456a5d86f314169 (diff) |
Remove trailing '\n' in qFormatLogMessage output
Do not automatically add a \n to all messages formatted by
qFormatLogMessage. Some backends require a final newline,
some don't, so it's best to only append it where it's actually needed.
The returned string will be null if the pattern is empty. This allows
to differentiate between the case that the pattern just didn't apply
(empty line is printed), and the case that qSetMessagePattern(QString())
have been called (nothing is printed).
Change-Id: I17fde997a4074f58f82de6dea129948155c322d6
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
-rw-r--r-- | src/corelib/global/qlogging.cpp | 19 | ||||
-rw-r--r-- | src/testlib/qtestlog.cpp | 1 | ||||
-rw-r--r-- | tests/auto/corelib/global/qlogging/tst_qlogging.cpp | 12 |
3 files changed, 15 insertions, 17 deletions
diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index f356bab42d..6def794d5e 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -1101,14 +1101,9 @@ QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, con if (!pattern) { // after destruction of static QMessagePattern instance message.append(str); - message.append(QLatin1Char('\n')); return message; } - // don't print anything if pattern was empty - if (pattern->tokens[0] == 0) - return message; - bool skip = false; // we do not convert file, function, line literals to local encoding due to overhead @@ -1227,7 +1222,6 @@ QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, con message.append(QLatin1String(token)); } } - message.append(QLatin1Char('\n')); return message; } @@ -1289,7 +1283,7 @@ static void android_default_message_handler(QtMsgType type, case QtFatalMsg: priority = ANDROID_LOG_FATAL; break; }; - __android_log_print(priority, "Qt", "%s:%d (%s): %s", + __android_log_print(priority, "Qt", "%s:%d (%s): %s\n", context.file, context.line, context.function, qPrintable(message)); } @@ -1303,16 +1297,21 @@ static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &con { QString logMessage = qFormatLogMessage(type, context, buf); + // print nothing if message pattern didn't apply / was empty. + // (still print empty lines, e.g. because message itself was empty) + if (logMessage.isNull()) + return; + if (!qt_logging_to_console()) { #if defined(Q_OS_WIN) + logMessage.append(QLatin1Char('\n')); OutputDebugString(reinterpret_cast<const wchar_t *>(logMessage.utf16())); return; #elif defined(QT_USE_SLOG2) + logMessage.append(QLatin1Char('\n')); slog2_default_handler(type, logMessage.toLocal8Bit().constData()); return; #elif defined(QT_USE_JOURNALD) && !defined(QT_BOOTSTRAPPED) - // remove trailing \n, systemd appears to want them newline-less - logMessage.chop(1); systemd_default_message_handler(type, context, logMessage); return; #elif defined(Q_OS_ANDROID) @@ -1320,7 +1319,7 @@ static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &con return; #endif } - fprintf(stderr, "%s", logMessage.toLocal8Bit().constData()); + fprintf(stderr, "%s\n", logMessage.toLocal8Bit().constData()); fflush(stderr); } diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp index 7ea953232f..e48fdc1ad0 100644 --- a/src/testlib/qtestlog.cpp +++ b/src/testlib/qtestlog.cpp @@ -279,7 +279,6 @@ namespace QTest { return; QString msg = qFormatLogMessage(type, context, message); - msg.chop(1); // remove trailing newline if (type != QtFatalMsg) { if (counter.load() <= 0) diff --git a/tests/auto/corelib/global/qlogging/tst_qlogging.cpp b/tests/auto/corelib/global/qlogging/tst_qlogging.cpp index 35bd518b3a..0a55da5b7e 100644 --- a/tests/auto/corelib/global/qlogging/tst_qlogging.cpp +++ b/tests/auto/corelib/global/qlogging/tst_qlogging.cpp @@ -886,25 +886,25 @@ void tst_qmessagehandler::formatLogMessage_data() #define BA QByteArrayLiteral QTest::newRow("basic") << "%{type} %{file} %{line} %{function} %{message}" - << "debug main.cpp 1 func msg\n" + << "debug main.cpp 1 func msg" << QtDebugMsg << BA("main.cpp") << 1 << BA("func") << BA("") << "msg"; // test the if conditions QString format = "[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{if-category}%{category}: %{endif}%{message}"; QTest::newRow("if-debug") - << format << "[D] msg\n" + << format << "[D] msg" << QtDebugMsg << BA("") << 0 << BA("func") << QByteArray() << "msg"; QTest::newRow("if_warning") - << format << "[W] msg\n" + << format << "[W] msg" << QtWarningMsg << BA("") << 0 << BA("func") << QByteArray() << "msg"; QTest::newRow("if_critical") - << format << "[C] msg\n" + << format << "[C] msg" << QtCriticalMsg << BA("") << 0 << BA("func") << QByteArray() << "msg"; QTest::newRow("if_fatal") - << format << "[F] msg\n" + << format << "[F] msg" << QtFatalMsg << BA("") << 0 << BA("func") << QByteArray() << "msg"; QTest::newRow("if_cat") - << format << "[F] cat: msg\n" + << format << "[F] cat: msg" << QtFatalMsg << BA("") << 0 << BA("func") << BA("cat") << "msg"; } |