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 <thiago.macieira@intel.com>
Reviewed-by: Jerome Pasion <jerome.pasion@digia.com>
bb10
hjk 2013-09-27 13:37:37 +02:00 committed by The Qt Project
parent 09a11069e7
commit 32f27b4367
4 changed files with 100 additions and 78 deletions

View File

@ -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<QtDebugMsg>()) {
if (QT_DRIVER_USB().isDebugEnabled()) {
QStringList items;
foreach (const UsbEntry &entry, usbEntries())
items << QString("%1 (%2)").arg(entry.id, entry.classtype);

View File

@ -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
@ -158,19 +157,65 @@ QLoggingCategory::~QLoggingCategory()
*/
/*!
\fn bool QLoggingCategory::isEnabled() const
\fn bool QLoggingCategory::isDebugEnabled() const
Returns true if a message of the template \c QtMsgType argument should be
shown. Returns false otherwise.
Returns \c true if debug messages 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 \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.
*/
/*!
Returns true if a message of type \a msgtype for the category should be
shown. Returns false otherwise.
\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 \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(), 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 \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.

View File

@ -59,15 +59,14 @@ public:
explicit QLoggingCategory(const char *category);
~QLoggingCategory();
template <QtMsgType T>
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<QtDebugMsg>() const
{
return enabledDebug;
}
template <>
inline bool QLoggingCategory::isEnabled<QtWarningMsg>() const
{
return enabledWarning;
}
template <>
inline bool QLoggingCategory::isEnabled<QtCriticalMsg>() const
{
return enabledCritical;
}
template <>
inline bool QLoggingCategory::isEnabled<QtTraceMsg>() 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<QtTraceMsg>() ? &category : 0;
target = category.isTraceEnabled() ? &category : 0;
if (target)
start();
}
@ -175,16 +150,16 @@ private:
}
#define qCDebug(category) \
for (bool enabled = category().isEnabled<QtDebugMsg>(); 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<QtWarningMsg>(); 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<QtCriticalMsg>(); 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<QtTraceMsg>(); enabled; enabled = false) \
for (bool enabled = category.isTraceEnabled(); enabled; enabled = false) \
QTraceGuard(category)

View File

@ -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<QtDebugMsg>(), true);
QCOMPARE(QLoggingCategory::defaultCategory().isDebugEnabled(), true);
QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(QtDebugMsg), true);
QCOMPARE(QLoggingCategory::defaultCategory().isEnabled<QtWarningMsg>(), true);
QCOMPARE(QLoggingCategory::defaultCategory().isWarningEnabled(), true);
QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(QtWarningMsg), true);
QCOMPARE(QLoggingCategory::defaultCategory().isEnabled<QtCriticalMsg>(), true);
QCOMPARE(QLoggingCategory::defaultCategory().isCriticalEnabled(), true);
QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(QtCriticalMsg), true);
QLoggingCategory defaultCategory("default");
QCOMPARE(defaultCategory.isEnabled<QtDebugMsg>(), true);
QCOMPARE(defaultCategory.isDebugEnabled(), true);
QCOMPARE(defaultCategory.isEnabled(QtDebugMsg), true);
QCOMPARE(defaultCategory.isEnabled<QtWarningMsg>(), true);
QCOMPARE(defaultCategory.isWarningEnabled(), true);
QCOMPARE(defaultCategory.isEnabled(QtWarningMsg), true);
QCOMPARE(defaultCategory.isEnabled<QtCriticalMsg>(), true);
QCOMPARE(defaultCategory.isCriticalEnabled(), true);
QCOMPARE(defaultCategory.isEnabled(QtCriticalMsg), true);
QLoggingCategory customCategory("custom");
QCOMPARE(customCategory.isEnabled<QtDebugMsg>(), false);
QCOMPARE(customCategory.isDebugEnabled(), false);
QCOMPARE(customCategory.isEnabled(QtDebugMsg), false);
QCOMPARE(customCategory.isEnabled<QtWarningMsg>(), true);
QCOMPARE(customCategory.isWarningEnabled(), true);
QCOMPARE(customCategory.isEnabled(QtWarningMsg), true);
QCOMPARE(customCategory.isEnabled<QtCriticalMsg>(), 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<QtDebugMsg>(), true);
QCOMPARE(QLoggingCategory::defaultCategory().isDebugEnabled(), true);
QLoggingCategory::defaultCategory().setEnabled(QtDebugMsg, false);
QCOMPARE(QLoggingCategory::defaultCategory().isEnabled<QtDebugMsg>(), 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<QtDebugMsg>());
QVERIFY(QLoggingCategory::defaultCategory().isDebugEnabled());
QLoggingCategory::CategoryFilter defaultFilter =
QLoggingCategory::installFilter(customCategoryFilter);
QVERIFY(defaultFilter);
customCategoryFilterArgs.clear();
QVERIFY(!QLoggingCategory::defaultCategory().isEnabled<QtDebugMsg>());
QVERIFY(!QLoggingCategory::defaultCategory().isDebugEnabled());
QLoggingCategory cat("custom");
QCOMPARE(customCategoryFilterArgs, QStringList() << "custom");
QVERIFY(cat.isEnabled<QtDebugMsg>());
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<QtDebugMsg>());
QVERIFY(!cat.isEnabled<QtDebugMsg>());
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<QtDebugMsg>());
QVERIFY(!cat.isEnabled<QtDebugMsg>());
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()