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>
This commit is contained in:
parent
1ffe1a9a7c
commit
5bfe794aaa
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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";
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user