Print non-default categories in default message handler

Change the default output of the logging framework to prefix messages
with a 'category: ' in case the category is not "default", so that e.g.

  QLoggingCategory cat("qt.core.codes.windows");
  qCWarning(cat) << "MultiByteToWideChar: Cannot convert multibyte text";

will print

  qt.core.codes.windows: MultiByteToWideChar: Cannot convert multibyte text

while output from qWarning etc will show unaltered output. This should
help users to discover categories, and to group output together.

Change-Id: Iac2e1514f7dc5671966c36a440a119c857564cfc
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: hjk <hjk121@nokiamail.com>
This commit is contained in:
Kai Koehne 2013-09-20 11:45:54 +02:00 committed by The Qt Project
parent 42c8c7e9db
commit 15ddb91bc7
3 changed files with 20 additions and 8 deletions

View File

@ -549,6 +549,7 @@ static const char functionTokenC[] = "%{function}";
static const char pidTokenC[] = "%{pid}";
static const char appnameTokenC[] = "%{appname}";
static const char threadidTokenC[] = "%{threadid}";
static const char ifCategoryTokenC[] = "%{if-category}";
static const char ifDebugTokenC[] = "%{if-debug}";
static const char ifWarningTokenC[] = "%{if-warning}";
static const char ifCriticalTokenC[] = "%{if-critical}";
@ -556,7 +557,7 @@ static const char ifFatalTokenC[] = "%{if-fatal}";
static const char endifTokenC[] = "%{endif}";
static const char emptyTokenC[] = "";
static const char defaultPattern[] = "%{message}";
static const char defaultPattern[] = "%{if-category}%{category}: %{endif}%{message}";
struct QMessagePattern {
@ -675,6 +676,7 @@ void QMessagePattern::setPattern(const QString &pattern)
tokens[i] = LEVEL; \
inIf = true; \
}
IF_TOKEN(ifCategoryTokenC)
IF_TOKEN(ifDebugTokenC)
IF_TOKEN(ifWarningTokenC)
IF_TOKEN(ifCriticalTokenC)
@ -837,6 +839,9 @@ Q_CORE_EXPORT QString qMessageFormatString(QtMsgType type, const QMessageLogCont
message.append(QLatin1String("0x"));
message.append(QString::number(qlonglong(QThread::currentThread()->currentThread()), 16));
#endif
} else if (token == ifCategoryTokenC) {
if (!context.category || (strcmp(context.category, "default") == 0))
skip = true;
#define HANDLE_IF_TOKEN(LEVEL) \
} else if (token == if##LEVEL##TokenC) { \
skip = type != Qt##LEVEL##Msg;

View File

@ -40,6 +40,7 @@
****************************************************************************/
#include <QCoreApplication>
#include <QLoggingCategory>
struct T {
T() { qDebug("static constructor"); }
@ -57,6 +58,9 @@ int main(int argc, char **argv)
qWarning("qWarning");
qCritical("qCritical");
QLoggingCategory cat("category");
qCWarning(cat) << "qDebug with category";
qSetMessagePattern(QString());
qDebug("qDebug2");

View File

@ -666,13 +666,14 @@ void tst_qmessagehandler::qMessagePattern()
// qDebug() << output;
QVERIFY(!output.isEmpty());
QVERIFY(output.contains("debug 45 T::T static constructor"));
QVERIFY(output.contains("debug 46 T::T static constructor"));
// we can't be sure whether the QT_MESSAGE_PATTERN is already destructed
QVERIFY(output.contains("static destructor"));
QVERIFY(output.contains("debug tst_qlogging 56 main qDebug"));
QVERIFY(output.contains("warning tst_qlogging 57 main qWarning"));
QVERIFY(output.contains("critical tst_qlogging 58 main qCritical"));
QVERIFY(output.contains("debug tst_qlogging 62 main qDebug2"));
QVERIFY(output.contains("debug tst_qlogging 57 main qDebug"));
QVERIFY(output.contains("warning tst_qlogging 58 main qWarning"));
QVERIFY(output.contains("critical tst_qlogging 59 main qCritical"));
QVERIFY(output.contains("warning tst_qlogging 62 main qDebug with category "));
QVERIFY(output.contains("debug tst_qlogging 66 main qDebug2"));
environment = m_baseEnvironment;
environment.prepend("QT_MESSAGE_PATTERN=\"PREFIX: %{unknown} %{message}\"");
@ -710,7 +711,8 @@ void tst_qmessagehandler::qMessagePattern()
QByteArray expected = "static constructor\n"
"[debug] qDebug\n"
"[warning] qWarning\n"
"[critical] qCritical\n";
"[critical] qCritical\n"
"[warning] qDebug with category \n";
#ifdef Q_OS_WIN
output.replace("\r\n", "\n");
#endif
@ -727,7 +729,7 @@ void tst_qmessagehandler::qMessagePatternIf()
const QString appExe = m_appDir + "/app";
QStringList environment = m_baseEnvironment;
environment.prepend("QT_MESSAGE_PATTERN=\"[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{message}\"");
environment.prepend("QT_MESSAGE_PATTERN=\"[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{if-category}%{category}: %{endif}%{message}\"");
process.setEnvironment(environment);
process.start(appExe);
QVERIFY2(process.waitForStarted(), qPrintable(
@ -745,6 +747,7 @@ void tst_qmessagehandler::qMessagePatternIf()
QVERIFY(output.contains("[D] qDebug"));
QVERIFY(output.contains("[W] qWarning"));
QVERIFY(output.contains("[C] qCritical"));
QVERIFY(output.contains("[W] category: qDebug with category"));
QVERIFY(output.contains("[D] qDebug2"));
//