summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/global/qlogging.cpp24
-rw-r--r--src/corelib/global/qlogging.h4
-rw-r--r--src/testlib/qtestlog.cpp6
-rw-r--r--tests/auto/corelib/global/qlogging/tst_qlogging.cpp62
4 files changed, 83 insertions, 13 deletions
diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp
index 847b7cb4c0..013efec3d5 100644
--- a/src/corelib/global/qlogging.cpp
+++ b/src/corelib/global/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
**
@@ -1052,10 +1052,20 @@ static void slog2_default_handler(QtMsgType msgType, const char *message)
Q_GLOBAL_STATIC(QMessagePattern, qMessagePattern)
/*!
- \internal
-*/
-Q_CORE_EXPORT QString qMessageFormatString(QtMsgType type, const QMessageLogContext &context,
- const QString &str)
+ \relates <QtGlobal>
+ \since 5.4
+
+ Generates a formatted string out of the \a type, \a context, \a str arguments.
+
+ qFormatLogMessage returns a QString that is formatted according to the current message pattern.
+ It can be used by custom message handlers to format output similar to Qt's default message
+ handler.
+
+ The function is thread-safe.
+
+ \sa qInstallMessageHandler(), qSetMessagePattern()
+ */
+QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &str)
{
QString message;
@@ -1278,7 +1288,7 @@ static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &con
bool toConsole;
};
- QString logMessage = qMessageFormatString(type, context, buf);
+ QString logMessage = qFormatLogMessage(type, context, buf);
#if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB)
if (!qWinLogToStderr()) {
@@ -1583,7 +1593,7 @@ void qErrnoWarning(int code, const char *msg, ...)
environment variable; if both qSetMessagePattern() is called and QT_MESSAGE_PATTERN is
set, the environment variable takes precedence.
- qSetMessagePattern() has no effect if a custom message handler is installed.
+ Custom message handlers can use qFormatLogMessage() to take \a pattern into account.
\sa qInstallMessageHandler(), {Debugging Techniques}
*/
diff --git a/src/corelib/global/qlogging.h b/src/corelib/global/qlogging.h
index 043f799414..fe965ec7f5 100644
--- a/src/corelib/global/qlogging.h
+++ b/src/corelib/global/qlogging.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -167,6 +167,8 @@ typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QS
Q_CORE_EXPORT QtMessageHandler qInstallMessageHandler(QtMessageHandler);
Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern);
+Q_CORE_EXPORT QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context,
+ const QString &buf);
QT_END_NAMESPACE
#endif // QLOGGING_H
diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp
index 1e7f131652..708a91b557 100644
--- a/src/testlib/qtestlog.cpp
+++ b/src/testlib/qtestlog.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).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtTest module of the Qt Toolkit.
@@ -59,8 +59,6 @@
QT_BEGIN_NAMESPACE
-Q_CORE_EXPORT QString qMessageFormatString(QtMsgType type, const QMessageLogContext &context, const QString& msg);
-
static void saveCoverageTool(const char * appname, bool testfailed, bool installedTestCoverage)
{
#ifdef __COVERAGESCANNER__
@@ -287,7 +285,7 @@ namespace QTest {
// the message is expected, so just swallow it.
return;
- QString msg = qMessageFormatString(type, context, message);
+ QString msg = qFormatLogMessage(type, context, message);
msg.chop(1); // remove trailing newline
if (type != QtFatalMsg) {
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"