Initialize QLoggingRegistry rules on first use
Allows categorized logging before QCoreApplication has been created, which otherwise would silently fail to output anything because the category would never be enabled, despite QT_LOGGING_RULES being set. Change-Id: I1861e5366ea980dff2ffa753b137276c77278eee Reviewed-by: Kai Koehne <kai.koehne@qt.io> Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
parent
ea18eec931
commit
fa2a653b3b
@ -255,6 +255,7 @@ void QLoggingSettingsParser::parseNextLine(QStringRef line)
|
|||||||
QLoggingRegistry::QLoggingRegistry()
|
QLoggingRegistry::QLoggingRegistry()
|
||||||
: categoryFilter(defaultCategoryFilter)
|
: categoryFilter(defaultCategoryFilter)
|
||||||
{
|
{
|
||||||
|
initalizeRules(); // Init on first use
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool qtLoggingDebug()
|
static bool qtLoggingDebug()
|
||||||
@ -283,7 +284,7 @@ static QVector<QLoggingRule> loadRulesFromFile(const QString &filePath)
|
|||||||
Initializes the rules database by loading
|
Initializes the rules database by loading
|
||||||
$QT_LOGGING_CONF, $QT_LOGGING_RULES, and .config/QtProject/qtlogging.ini.
|
$QT_LOGGING_CONF, $QT_LOGGING_RULES, and .config/QtProject/qtlogging.ini.
|
||||||
*/
|
*/
|
||||||
void QLoggingRegistry::init()
|
void QLoggingRegistry::initalizeRules()
|
||||||
{
|
{
|
||||||
QVector<QLoggingRule> er, qr, cr;
|
QVector<QLoggingRule> er, qr, cr;
|
||||||
// get rules from environment
|
// get rules from environment
|
||||||
|
@ -113,7 +113,7 @@ class Q_AUTOTEST_EXPORT QLoggingRegistry
|
|||||||
public:
|
public:
|
||||||
QLoggingRegistry();
|
QLoggingRegistry();
|
||||||
|
|
||||||
void init();
|
void initalizeRules();
|
||||||
|
|
||||||
void registerCategory(QLoggingCategory *category, QtMsgType enableForLevel);
|
void registerCategory(QLoggingCategory *category, QtMsgType enableForLevel);
|
||||||
void unregisterCategory(QLoggingCategory *category);
|
void unregisterCategory(QLoggingCategory *category);
|
||||||
|
@ -776,8 +776,6 @@ void QCoreApplicationPrivate::init()
|
|||||||
if (!coreappdata()->applicationVersionSet)
|
if (!coreappdata()->applicationVersionSet)
|
||||||
coreappdata()->applicationVersion = appVersion();
|
coreappdata()->applicationVersion = appVersion();
|
||||||
|
|
||||||
QLoggingRegistry::instance()->init();
|
|
||||||
|
|
||||||
#if QT_CONFIG(library)
|
#if QT_CONFIG(library)
|
||||||
// Reset the lib paths, so that they will be recomputed, taking the availability of argv[0]
|
// Reset the lib paths, so that they will be recomputed, taking the availability of argv[0]
|
||||||
// into account. If necessary, recompute right away and replay the manual changes on top of the
|
// into account. If necessary, recompute right away and replay the manual changes on top of the
|
||||||
|
@ -197,10 +197,22 @@ private slots:
|
|||||||
// Check whether QT_LOGGING_CONF is picked up from environment
|
// Check whether QT_LOGGING_CONF is picked up from environment
|
||||||
//
|
//
|
||||||
|
|
||||||
qputenv("QT_LOGGING_CONF", QFINDTESTDATA("qtlogging.ini").toLocal8Bit());
|
Q_ASSERT(!qApp); // Rules should not require an app to resolve
|
||||||
|
|
||||||
QLoggingRegistry registry;
|
qputenv("QT_LOGGING_RULES", "qt.foo.bar=true");
|
||||||
registry.init();
|
QLoggingCategory qtEnabledByLoggingRule("qt.foo.bar");
|
||||||
|
QCOMPARE(qtEnabledByLoggingRule.isDebugEnabled(), true);
|
||||||
|
QLoggingCategory qtDisabledByDefault("qt.foo.baz");
|
||||||
|
QCOMPARE(qtDisabledByDefault.isDebugEnabled(), false);
|
||||||
|
|
||||||
|
QLoggingRegistry ®istry = *QLoggingRegistry::instance();
|
||||||
|
QCOMPARE(registry.ruleSets[QLoggingRegistry::ApiRules].size(), 0);
|
||||||
|
QCOMPARE(registry.ruleSets[QLoggingRegistry::ConfigRules].size(), 0);
|
||||||
|
QCOMPARE(registry.ruleSets[QLoggingRegistry::EnvironmentRules].size(), 1);
|
||||||
|
|
||||||
|
qunsetenv("QT_LOGGING_RULES");
|
||||||
|
qputenv("QT_LOGGING_CONF", QFINDTESTDATA("qtlogging.ini").toLocal8Bit());
|
||||||
|
registry.initalizeRules();
|
||||||
|
|
||||||
QCOMPARE(registry.ruleSets[QLoggingRegistry::ApiRules].size(), 0);
|
QCOMPARE(registry.ruleSets[QLoggingRegistry::ApiRules].size(), 0);
|
||||||
QCOMPARE(registry.ruleSets[QLoggingRegistry::ConfigRules].size(), 0);
|
QCOMPARE(registry.ruleSets[QLoggingRegistry::ConfigRules].size(), 0);
|
||||||
@ -208,7 +220,7 @@ private slots:
|
|||||||
|
|
||||||
// check that QT_LOGGING_RULES take precedence
|
// check that QT_LOGGING_RULES take precedence
|
||||||
qputenv("QT_LOGGING_RULES", "Digia.*=true");
|
qputenv("QT_LOGGING_RULES", "Digia.*=true");
|
||||||
registry.init();
|
registry.initalizeRules();
|
||||||
QCOMPARE(registry.ruleSets[QLoggingRegistry::EnvironmentRules].size(), 2);
|
QCOMPARE(registry.ruleSets[QLoggingRegistry::EnvironmentRules].size(), 2);
|
||||||
QCOMPARE(registry.ruleSets[QLoggingRegistry::EnvironmentRules].at(1).enabled, true);
|
QCOMPARE(registry.ruleSets[QLoggingRegistry::EnvironmentRules].at(1).enabled, true);
|
||||||
}
|
}
|
||||||
@ -234,7 +246,7 @@ private slots:
|
|||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
QLoggingRegistry registry;
|
QLoggingRegistry registry;
|
||||||
registry.init();
|
registry.initalizeRules();
|
||||||
QCOMPARE(registry.ruleSets[QLoggingRegistry::ConfigRules].size(), 1);
|
QCOMPARE(registry.ruleSets[QLoggingRegistry::ConfigRules].size(), 1);
|
||||||
|
|
||||||
// remove file again
|
// remove file again
|
||||||
@ -300,6 +312,6 @@ private slots:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
QTEST_MAIN(tst_QLoggingRegistry)
|
QTEST_APPLESS_MAIN(tst_QLoggingRegistry)
|
||||||
|
|
||||||
#include "tst_qloggingregistry.moc"
|
#include "tst_qloggingregistry.moc"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user