Let QLoggingCategory::defaultCategory return a pointer

The pointer can be null. Going trough the reference invokes undefined
behavior here.

Change-Id: Ia84e4e732cdcbbaee0f5f0679765d18069ea8b2d
Reviewed-by: Kai Koehne <kai.koehne@digia.com>
This commit is contained in:
hjk 2013-10-10 17:06:37 +02:00 committed by The Qt Project
parent f9889534d1
commit 7a47aebe9e
4 changed files with 29 additions and 22 deletions

View File

@ -927,7 +927,7 @@ static void qt_message_print(QtMsgType msgType, const QMessageLogContext &contex
#ifndef QT_BOOTSTRAPPED
// qDebug, qWarning, ... macros do not check whether category is enabled
if (!context.category || (strcmp(context.category, "default") == 0)) {
if (QLoggingCategory *defaultCategory = &QLoggingCategory::defaultCategory()) {
if (QLoggingCategory *defaultCategory = QLoggingCategory::defaultCategory()) {
if (!defaultCategory->isEnabled(msgType))
return;
}

View File

@ -244,12 +244,19 @@ void QLoggingCategory::setEnabled(QtMsgType type, bool enable)
*/
/*!
Returns the category \c "default" that is used e.g. by qDebug(), qWarning(),
qCritical(), qFatal().
Returns a pointer to the global category \c "default" that
is used e.g. by qDebug(), qWarning(), qCritical(), qFatal().
\note The returned pointer may be null during destruction of
static objects.
\note Ownership of the category is not transferred, do not
\c delete the returned pointer.
*/
QLoggingCategory &QLoggingCategory::defaultCategory()
QLoggingCategory *QLoggingCategory::defaultCategory()
{
return *qtDefaultCategory();
return qtDefaultCategory();
}
/*!

View File

@ -72,7 +72,7 @@ public:
// allows usage of both factory method and variable in qCX macros
QLoggingCategory &operator()() { return *this; }
static QLoggingCategory &defaultCategory();
static QLoggingCategory *defaultCategory();
typedef void (*CategoryFilter)(QLoggingCategory*);
static CategoryFilter installFilter(CategoryFilter);

View File

@ -226,7 +226,7 @@ private slots:
void QLoggingCategory_categoryName()
{
logMessage.clear();
QCOMPARE(QString::fromLatin1(QLoggingCategory::defaultCategory().categoryName()),
QCOMPARE(QString::fromLatin1(QLoggingCategory::defaultCategory()->categoryName()),
QStringLiteral("default"));
QLoggingCategory defaultCategory("default");
@ -237,7 +237,7 @@ private slots:
QCOMPARE(QByteArray(nullCategory.categoryName()), QByteArray("default"));
// we rely on the same pointer for any "default" category
QCOMPARE(QLoggingCategory::defaultCategory().categoryName(),
QCOMPARE(QLoggingCategory::defaultCategory()->categoryName(),
defaultCategory.categoryName());
QCOMPARE(defaultCategory.categoryName(),
nullCategory.categoryName());
@ -256,12 +256,12 @@ private slots:
{
logMessage.clear();
QCOMPARE(QLoggingCategory::defaultCategory().isDebugEnabled(), true);
QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(QtDebugMsg), true);
QCOMPARE(QLoggingCategory::defaultCategory().isWarningEnabled(), true);
QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(QtWarningMsg), true);
QCOMPARE(QLoggingCategory::defaultCategory().isCriticalEnabled(), true);
QCOMPARE(QLoggingCategory::defaultCategory().isEnabled(QtCriticalMsg), true);
QCOMPARE(QLoggingCategory::defaultCategory()->isDebugEnabled(), true);
QCOMPARE(QLoggingCategory::defaultCategory()->isEnabled(QtDebugMsg), true);
QCOMPARE(QLoggingCategory::defaultCategory()->isWarningEnabled(), true);
QCOMPARE(QLoggingCategory::defaultCategory()->isEnabled(QtWarningMsg), true);
QCOMPARE(QLoggingCategory::defaultCategory()->isCriticalEnabled(), true);
QCOMPARE(QLoggingCategory::defaultCategory()->isEnabled(QtCriticalMsg), true);
QLoggingCategory defaultCategory("default");
QCOMPARE(defaultCategory.isDebugEnabled(), true);
@ -287,11 +287,11 @@ private slots:
{
logMessage.clear();
QCOMPARE(QLoggingCategory::defaultCategory().isDebugEnabled(), true);
QCOMPARE(QLoggingCategory::defaultCategory()->isDebugEnabled(), true);
QLoggingCategory::defaultCategory().setEnabled(QtDebugMsg, false);
QCOMPARE(QLoggingCategory::defaultCategory().isDebugEnabled(), false);
QLoggingCategory::defaultCategory().setEnabled(QtDebugMsg, true);
QLoggingCategory::defaultCategory()->setEnabled(QtDebugMsg, false);
QCOMPARE(QLoggingCategory::defaultCategory()->isDebugEnabled(), false);
QLoggingCategory::defaultCategory()->setEnabled(QtDebugMsg, true);
// make sure nothing has printed warnings
QVERIFY(logMessage.isEmpty());
@ -300,13 +300,13 @@ private slots:
void QLoggingCategory_installFilter()
{
QVERIFY(QLoggingCategory::defaultCategory().isDebugEnabled());
QVERIFY(QLoggingCategory::defaultCategory()->isDebugEnabled());
QLoggingCategory::CategoryFilter defaultFilter =
QLoggingCategory::installFilter(customCategoryFilter);
QVERIFY(defaultFilter);
customCategoryFilterArgs.clear();
QVERIFY(!QLoggingCategory::defaultCategory().isDebugEnabled());
QVERIFY(!QLoggingCategory::defaultCategory()->isDebugEnabled());
QLoggingCategory cat("custom");
QCOMPARE(customCategoryFilterArgs, QStringList() << "custom");
@ -319,7 +319,7 @@ private slots:
QCOMPARE((void*)currentFilter, (void*)customCategoryFilter);
QCOMPARE(customCategoryFilterArgs.size(), 0);
QVERIFY(QLoggingCategory::defaultCategory().isDebugEnabled());
QVERIFY(QLoggingCategory::defaultCategory()->isDebugEnabled());
QVERIFY(!cat.isDebugEnabled());
// install default filter
@ -328,7 +328,7 @@ private slots:
QCOMPARE((void*)defaultFilter, (void*)currentFilter);
QCOMPARE(customCategoryFilterArgs.size(), 0);
QVERIFY(QLoggingCategory::defaultCategory().isDebugEnabled());
QVERIFY(QLoggingCategory::defaultCategory()->isDebugEnabled());
QVERIFY(!cat.isDebugEnabled());
}