From 32f27b4367c4e042a3f0cda671579494e31c1d69 Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 27 Sep 2013 13:37:37 +0200 Subject: Replace QLoggingCategory::isEnabled by non-template functions This yields the same results as previously and is more in line with existing interfaces. Change-Id: I0bf0372bf18f3bfde579385cddbe594bf71e3c52 Reviewed-by: Thiago Macieira Reviewed-by: Jerome Pasion --- src/corelib/doc/snippets/qloggingcategory/main.cpp | 2 +- src/corelib/io/qloggingcategory.cpp | 91 ++++++++++++++++------ src/corelib/io/qloggingcategory.h | 45 +++-------- .../io/qloggingcategory/tst_qloggingcategory.cpp | 38 ++++----- 4 files changed, 99 insertions(+), 77 deletions(-) diff --git a/src/corelib/doc/snippets/qloggingcategory/main.cpp b/src/corelib/doc/snippets/qloggingcategory/main.cpp index d6d9ae0ff3..27d64e8253 100644 --- a/src/corelib/doc/snippets/qloggingcategory/main.cpp +++ b/src/corelib/doc/snippets/qloggingcategory/main.cpp @@ -73,7 +73,7 @@ void main(int argc, char *argv[]) //![2] // don't run the expensive code if the string won't print - if (QT_DRIVER_USB().isEnabled()) { + if (QT_DRIVER_USB().isDebugEnabled()) { QStringList items; foreach (const UsbEntry &entry, usbEntries()) items << QString("%1 (%2)").arg(entry.id, entry.classtype); diff --git a/src/corelib/io/qloggingcategory.cpp b/src/corelib/io/qloggingcategory.cpp index 24eeb1584c..ca9f8c1656 100644 --- a/src/corelib/io/qloggingcategory.cpp +++ b/src/corelib/io/qloggingcategory.cpp @@ -70,23 +70,22 @@ Q_GLOBAL_STATIC_WITH_ARGS(QLoggingCategory, qtDefaultCategory, \section1 Checking category configuration - QLoggingCategory provides two isEnabled methods, a template one and a - non-template one, for checking whether the current category is enabled. - The template version checks for the most common case that no special rules - are applied in inline code, and should be preferred: + QLoggingCategory provides a generic \l isEnabled() and message + type dependent \l isDebugEnabled(), \l isWarningEnabled(), + \l isCriticalEnabled() and \l isTraceEnabled() + methods for checking whether the current category is enabled. - \snippet qloggingcategory/main.cpp 2 - - Note that qCDebug() prevents arguments from being evaluated - if the string won't print, so calling isEnabled explicitly is not needed: - \l isEnabled(). + \note The qCDebug(), qCWarning(), qCCritical(), qCTrace() and + qCTraceGuard() macros prevent arguments from being evaluated if the + respective message types are not enabled for the category, so explicit + checking is not needed: \snippet qloggingcategory/main.cpp 3 \section1 Default configuration - In the default configuration \l isEnabled() will return true for all - \l QtMsgType types except QtDebugMsg: QtDebugMsg is only active by default + In the default configuration \l isWarningEnabled() and \l isCriticalEnabled() + will return \c true. By default, \l isDebugEnabled() will return \c true only for the \c "default" category. \section1 Changing configuration @@ -157,20 +156,66 @@ QLoggingCategory::~QLoggingCategory() Returns the name of the category. */ +/*! + \fn bool QLoggingCategory::isDebugEnabled() const + + Returns \c true if debug messages should be shown for this category. + Returns \c false otherwise. + + \note The \l qCDebug() macro already does this check before executing any + code. However, calling this method may be useful to avoid + expensive generation of data that is only used for debug output. +*/ + +/*! + \fn bool QLoggingCategory::isWarningEnabled() const + + Returns \c true if warning messages should be shown for this category. + Returns \c false otherwise. + + \note The \l qCWarning() macro already does this check before executing any + code. However, calling this method may be useful to avoid + expensive generation of data that is only used for debug output. +*/ + +/*! + \fn bool QLoggingCategory::isCriticalEnabled() const + + Returns \c true if critical messages should be shown for this category. + Returns \c false otherwise. + + \note The \l qCCritical() macro already does this check before executing any + code. However, calling this method may be useful to avoid + expensive generation of data that is only used for debug output. +*/ + +/*! + \fn bool QLoggingCategory::isTraceEnabled() const + + Returns \c true if the tracers associated with this category should + receive messages. Returns \c false otherwise. + + \note The \l qCTrace() and \l qCTraceGuard() macros already do this check + before executing any + code. However, calling this method may be useful to avoid + expensive generation of data that is only used for debug output. +*/ + /*! \fn bool QLoggingCategory::isEnabled() const - Returns true if a message of the template \c QtMsgType argument should be - shown. Returns false otherwise. + Returns \c true if a message of the \c QtMsgType argument should be + shown for this category. Returns \c false otherwise. - \note The qCDebug, qCWarning, qCCritical macros already do this check before - executing any code. However, calling this method may be useful to avoid + \note The qCDebug(), qCWarning(), qCCritical(), qCTrace() and + qCTraceGuard() macros already do this check before executing any code. + However, calling this method may be useful to avoid expensive generation of data that is only used for debug output. */ /*! - Returns true if a message of type \a msgtype for the category should be - shown. Returns false otherwise. + Returns \c true if a message of type \a msgtype for the category should be + shown. Returns \c false otherwise. \note The templated, inline version of this method, \l isEnabled(), is optimized for the common case that no configuration is set, and should @@ -195,7 +240,7 @@ bool QLoggingCategory::isEnabled(QtMsgType msgtype) const Changes only affect the current QLoggingCategory object, and won't change e.g. the settings of another objects for the same category name. - \note QtFatalMsg cannot be changed. It will always return true. + \note QtFatalMsg cannot be changed. It will always return \c true. Example: @@ -291,7 +336,7 @@ void QLoggingCategory::setFilterRules(const QString &rules) \a category. The macro expands to code that first checks whether - \l QLoggingCategory::isEnabled() evaluates for debug output to \c{true}. + \l QLoggingCategory::isDebugEnabled() evaluates to \c{true}. If so, the stream arguments are processed and sent to the message handler. Example: @@ -313,7 +358,7 @@ void QLoggingCategory::setFilterRules(const QString &rules) \a category. The macro expands to code that first checks whether - \l QLoggingCategory::isEnabled() evaluates for warning output to \c{true}. + \l QLoggingCategory::isWarningEnabled() evaluates to \c{true}. If so, the stream arguments are processed and sent to the message handler. Example: @@ -335,7 +380,7 @@ void QLoggingCategory::setFilterRules(const QString &rules) \a category. The macro expands to code that first checks whether - \l QLoggingCategory::isEnabled() evaluates for critical output to \c{true}. + \l QLoggingCategory::isCriticalEnabled() evaluates to \c{true}. If so, the stream arguments are processed and sent to the message handler. Example: @@ -357,7 +402,7 @@ void QLoggingCategory::setFilterRules(const QString &rules) \a category. The macro expands to code that first checks whether - \l QLoggingCategory::isEnabled() evaluates for trace output to \c{true}. + \l QLoggingCategory::istraceEnabled() evaluates to \c{true}. If so, the stream arguments are processed and sent to the tracers registered with the category. @@ -378,7 +423,7 @@ void QLoggingCategory::setFilterRules(const QString &rules) The macro expands to code that creates a guard object with automatic storage. The guard constructor checks whether - \l QLoggingCategory::isEnabled() evaluates for trace output to \c{true}. + \l QLoggingCategory::isTraceEnabled() evaluates to \c{true}. If so, the stream arguments are processed and the \c{start()} functions of the tracers registered with the \a category are called. diff --git a/src/corelib/io/qloggingcategory.h b/src/corelib/io/qloggingcategory.h index 70192fef13..35da04c8f2 100644 --- a/src/corelib/io/qloggingcategory.h +++ b/src/corelib/io/qloggingcategory.h @@ -59,15 +59,14 @@ public: explicit QLoggingCategory(const char *category); ~QLoggingCategory(); - template - bool isEnabled() const - { - return isEnabled(T); - } - bool isEnabled(QtMsgType type) const; void setEnabled(QtMsgType type, bool enable); + bool isDebugEnabled() const { return enabledDebug; } + bool isWarningEnabled() const { return enabledWarning; } + bool isCriticalEnabled() const { return enabledCritical; } + bool isTraceEnabled() const { return enabledTrace; } + const char *categoryName() const { return name; } // allows usage of both factory method and variable in qCX macros @@ -95,30 +94,6 @@ private: bool enabledTrace; }; -template <> -inline bool QLoggingCategory::isEnabled() const -{ - return enabledDebug; -} - -template <> -inline bool QLoggingCategory::isEnabled() const -{ - return enabledWarning; -} - -template <> -inline bool QLoggingCategory::isEnabled() const -{ - return enabledCritical; -} - -template <> -inline bool QLoggingCategory::isEnabled() const -{ - return enabledTrace; -} - class Q_CORE_EXPORT QTracer { Q_DISABLE_COPY(QTracer) @@ -141,7 +116,7 @@ class Q_CORE_EXPORT QTraceGuard public: QTraceGuard(QLoggingCategory &category) { - target = category.isEnabled() ? &category : 0; + target = category.isTraceEnabled() ? &category : 0; if (target) start(); } @@ -175,16 +150,16 @@ private: } #define qCDebug(category) \ - for (bool enabled = category().isEnabled(); enabled; enabled = false) \ + for (bool enabled = category().isDebugEnabled(); enabled; enabled = false) \ QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).debug() #define qCWarning(category) \ - for (bool enabled = category().isEnabled(); enabled; enabled = false) \ + for (bool enabled = category().isWarningEnabled(); enabled; enabled = false) \ QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).warning() #define qCCritical(category) \ - for (bool enabled = category().isEnabled(); enabled; enabled = false) \ + for (bool enabled = category().isCriticalEnabled(); enabled; enabled = false) \ QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).critical() #define qCTrace(category) \ - for (bool enabled = category.isEnabled(); enabled; enabled = false) \ + for (bool enabled = category.isTraceEnabled(); enabled; enabled = false) \ QTraceGuard(category) diff --git a/tests/auto/corelib/io/qloggingcategory/tst_qloggingcategory.cpp b/tests/auto/corelib/io/qloggingcategory/tst_qloggingcategory.cpp index 7ddb221402..df93262d93 100644 --- a/tests/auto/corelib/io/qloggingcategory/tst_qloggingcategory.cpp +++ b/tests/auto/corelib/io/qloggingcategory/tst_qloggingcategory.cpp @@ -69,6 +69,7 @@ QByteArray qMyMessageFormatString(QtMsgType type, const QMessageLogContext &cont case QtWarningMsg: message.append(".warning"); break; case QtCriticalMsg:message.append(".critical"); break; case QtFatalMsg: message.append(".fatal"); break; + case QtTraceMsg: message.append(".trace"); break; } message.append(": "); message.append(qPrintable(str)); @@ -255,27 +256,27 @@ private slots: { logMessage.clear(); - QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(), true); + QCOMPARE(QLoggingCategory::defaultCategory().isDebugEnabled(), true); QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(QtDebugMsg), true); - QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(), true); + QCOMPARE(QLoggingCategory::defaultCategory().isWarningEnabled(), true); QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(QtWarningMsg), true); - QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(), true); + QCOMPARE(QLoggingCategory::defaultCategory().isCriticalEnabled(), true); QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(QtCriticalMsg), true); QLoggingCategory defaultCategory("default"); - QCOMPARE(defaultCategory.isEnabled(), true); + QCOMPARE(defaultCategory.isDebugEnabled(), true); QCOMPARE(defaultCategory.isEnabled(QtDebugMsg), true); - QCOMPARE(defaultCategory.isEnabled(), true); + QCOMPARE(defaultCategory.isWarningEnabled(), true); QCOMPARE(defaultCategory.isEnabled(QtWarningMsg), true); - QCOMPARE(defaultCategory.isEnabled(), true); + QCOMPARE(defaultCategory.isCriticalEnabled(), true); QCOMPARE(defaultCategory.isEnabled(QtCriticalMsg), true); QLoggingCategory customCategory("custom"); - QCOMPARE(customCategory.isEnabled(), false); + QCOMPARE(customCategory.isDebugEnabled(), false); QCOMPARE(customCategory.isEnabled(QtDebugMsg), false); - QCOMPARE(customCategory.isEnabled(), true); + QCOMPARE(customCategory.isWarningEnabled(), true); QCOMPARE(customCategory.isEnabled(QtWarningMsg), true); - QCOMPARE(customCategory.isEnabled(), true); + QCOMPARE(customCategory.isCriticalEnabled(), true); QCOMPARE(customCategory.isEnabled(QtCriticalMsg), true); // make sure nothing has printed warnings @@ -286,10 +287,10 @@ private slots: { logMessage.clear(); - QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(), true); + QCOMPARE(QLoggingCategory::defaultCategory().isDebugEnabled(), true); QLoggingCategory::defaultCategory().setEnabled(QtDebugMsg, false); - QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(), false); + QCOMPARE(QLoggingCategory::defaultCategory().isDebugEnabled(), false); QLoggingCategory::defaultCategory().setEnabled(QtDebugMsg, true); // make sure nothing has printed warnings @@ -299,17 +300,17 @@ private slots: void QLoggingCategory_installFilter() { - QVERIFY(QLoggingCategory::defaultCategory().isEnabled()); + QVERIFY(QLoggingCategory::defaultCategory().isDebugEnabled()); QLoggingCategory::CategoryFilter defaultFilter = QLoggingCategory::installFilter(customCategoryFilter); QVERIFY(defaultFilter); customCategoryFilterArgs.clear(); - QVERIFY(!QLoggingCategory::defaultCategory().isEnabled()); + QVERIFY(!QLoggingCategory::defaultCategory().isDebugEnabled()); QLoggingCategory cat("custom"); QCOMPARE(customCategoryFilterArgs, QStringList() << "custom"); - QVERIFY(cat.isEnabled()); + QVERIFY(cat.isDebugEnabled()); customCategoryFilterArgs.clear(); // install default filter @@ -318,8 +319,8 @@ private slots: QCOMPARE((void*)currentFilter, (void*)customCategoryFilter); QCOMPARE(customCategoryFilterArgs.size(), 0); - QVERIFY(QLoggingCategory::defaultCategory().isEnabled()); - QVERIFY(!cat.isEnabled()); + QVERIFY(QLoggingCategory::defaultCategory().isDebugEnabled()); + QVERIFY(!cat.isDebugEnabled()); // install default filter currentFilter = @@ -327,8 +328,8 @@ private slots: QCOMPARE((void*)defaultFilter, (void*)currentFilter); QCOMPARE(customCategoryFilterArgs.size(), 0); - QVERIFY(QLoggingCategory::defaultCategory().isEnabled()); - QVERIFY(!cat.isEnabled()); + QVERIFY(QLoggingCategory::defaultCategory().isDebugEnabled()); + QVERIFY(!cat.isDebugEnabled()); } void qDebugMacros() @@ -732,6 +733,7 @@ private slots: qCCritical(mycategoryobject) << "My Category Object"; QCOMPARE(cleanLogLine(logMessage), cleanLogLine(buf)); } + Q_UNUSED(pcategorybject); } void checkEmptyCategoryName() -- cgit v1.2.3