summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/global/qlogging
diff options
context:
space:
mode:
authorKai Koehne <kai.koehne@digia.com>2014-07-18 14:40:59 +0200
committerKai Koehne <kai.koehne@digia.com>2014-07-30 16:27:22 +0200
commite968793e81ffcb439210e7eb422c0063834cfdc9 (patch)
treee551d2d19b7f6281c117c671741d306f27267f35 /tests/auto/corelib/global/qlogging
parentc38af4e6bb51cfb1b52a38dea06a0a8a94db9545 (diff)
Add qFormatLogMessage()
Export the former qMessageFormatString() as qFormatLogMessage(). This allows custom message handlers to format their messages just like the default message handler, taking qSetMessagePattern() / QT_MESSAGE_PATTERN into account. The method should arguably not add the '\n' at the end, which a follow up commit will fix. Change-Id: Ib2a9cfda91473df079daf03bf3197e6ac63e013e Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'tests/auto/corelib/global/qlogging')
-rw-r--r--tests/auto/corelib/global/qlogging/tst_qlogging.cpp62
1 files changed, 61 insertions, 1 deletions
diff --git a/tests/auto/corelib/global/qlogging/tst_qlogging.cpp b/tests/auto/corelib/global/qlogging/tst_qlogging.cpp
index a8027a4157..1df5404995 100644
--- a/tests/auto/corelib/global/qlogging/tst_qlogging.cpp
+++ b/tests/auto/corelib/global/qlogging/tst_qlogging.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Olivier Goffart <ogoffart@woboq.com>
** Contact: http://www.qt-project.org/legal
**
@@ -68,6 +68,9 @@ private slots:
void qMessagePattern();
void setMessagePattern();
+ void formatLogMessage_data();
+ void formatLogMessage();
+
private:
QString m_appDir;
QStringList m_baseEnvironment;
@@ -803,6 +806,63 @@ void tst_qmessagehandler::setMessagePattern()
#endif // !QT_NO_PROCESS
}
+Q_DECLARE_METATYPE(QtMsgType)
+
+void tst_qmessagehandler::formatLogMessage_data()
+{
+ QTest::addColumn<QString>("pattern");
+ QTest::addColumn<QString>("result");
+
+ QTest::addColumn<QtMsgType>("type");
+ QTest::addColumn<QByteArray>("file");
+ QTest::addColumn<int>("line");
+ QTest::addColumn<QByteArray>("function");
+ QTest::addColumn<QByteArray>("category");
+ QTest::addColumn<QString>("message");
+
+#define BA QByteArrayLiteral
+
+ QTest::newRow("basic") << "%{type} %{file} %{line} %{function} %{message}"
+ << "debug main.cpp 1 func msg\n"
+ << 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"
+ << QtDebugMsg << BA("") << 0 << BA("func") << QByteArray() << "msg";
+ QTest::newRow("if_warning")
+ << format << "[W] msg\n"
+ << QtWarningMsg << BA("") << 0 << BA("func") << QByteArray() << "msg";
+ QTest::newRow("if_critical")
+ << format << "[C] msg\n"
+ << QtCriticalMsg << BA("") << 0 << BA("func") << QByteArray() << "msg";
+ QTest::newRow("if_fatal")
+ << format << "[F] msg\n"
+ << QtFatalMsg << BA("") << 0 << BA("func") << QByteArray() << "msg";
+ QTest::newRow("if_cat")
+ << format << "[F] cat: msg\n"
+ << QtFatalMsg << BA("") << 0 << BA("func") << BA("cat") << "msg";
+}
+
+void tst_qmessagehandler::formatLogMessage()
+{
+ QFETCH(QString, pattern);
+ QFETCH(QString, result);
+
+ QFETCH(QtMsgType, type);
+ QFETCH(QByteArray, file);
+ QFETCH(int, line);
+ QFETCH(QByteArray, function);
+ QFETCH(QByteArray, category);
+ QFETCH(QString, message);
+
+ qSetMessagePattern(pattern);
+ QMessageLogContext ctxt(file, line, function, category.isEmpty() ? 0 : category.data());
+ QString r = qFormatLogMessage(type, ctxt, message);
+ QCOMPARE(r, result);
+}
+
QTEST_MAIN(tst_qmessagehandler)
#include "tst_qlogging.moc"