Logging: Let user configure rules via QT_LOGGING_RULES

Check also for rules set in an environment variable QT_LOGGING_RULES.
This makes it even more convenient to set rules e.g. for just one run of an
application, without having to create a logging configuration file. It
is also more in place with the current way we enable/disable debugging
of parts of Qt via environment variables.

Change-Id: I4d05976f2b6c12bca472552ffa22345475cd01de
Reviewed-by: Laszlo Agocs <laszlo.agocs@digia.com>
Reviewed-by: Tomasz Olszak <olszak.tomasz@gmail.com>
This commit is contained in:
Kai Koehne 2014-03-11 17:04:28 +01:00 committed by The Qt Project
parent 490298e939
commit ef43967fcd
3 changed files with 25 additions and 7 deletions

View File

@ -108,8 +108,9 @@ Q_GLOBAL_STATIC_WITH_ARGS(QLoggingCategory, qtDefaultCategory,
Rules are evaluated in text order, from first to last. That is, if two rules
apply to a category/type, the rule that comes later is applied.
Rules can be set via \l setFilterRules(). Since Qt 5.3 logging rules
are also automatically loaded from the \c [Rules] section of a logging
Rules can be set via \l setFilterRules(). Since Qt 5.3 logging rules can also
be set in the \c QT_LOGGING_RULES environment variable, and
are automatically loaded from the \c [Rules] section of a logging
configuration file. Such configuration files are looked up in the QtProject
configuration directory, or explicitly set in a \c QT_LOGGING_CONF
environment variable.
@ -117,13 +118,15 @@ Q_GLOBAL_STATIC_WITH_ARGS(QLoggingCategory, qtDefaultCategory,
Rules set by \l setFilterRules() take precedence over rules specified
in the QtProject configuration directory, and can, in turn, be
overwritten by rules from the configuration file specified by
\c QT_LOGGING_CONF.
\c QT_LOGGING_CONF, and rules set by \c QT_LOGGING_RULES.
Order of evaluation:
\list
\li Rules from QtProject/qlogging.ini
\li Rules set by \l setFilterRules()
\li Rules from file in \c QT_LOGGING_CONF
\li Rules from environment variable QT_LOGGING_RULES
\endlist
The \c QtProject/qlogging.ini file is looked up in all directories returned
@ -344,9 +347,8 @@ QLoggingCategory::installFilter(QLoggingCategory::CategoryFilter filter)
\snippet qloggingcategory/main.cpp 2
\note The rules might be ignored if a custom category filter is installed
with \l installFilter(), or if the user defined a custom logging
configuration file in the \c QT_LOGGING_CONF environment variable.
with \l installFilter(), or if the user defined \c QT_LOGGING_CONF or \c QT_LOGGING_RULES
environment variable.
*/
void QLoggingCategory::setFilterRules(const QString &rules)
{

View File

@ -248,7 +248,7 @@ static bool qtLoggingDebug()
/*!
\internal
Initializes the rules database by loading
.config/QtProject/qtlogging.ini and $QT_LOGGING_CONF.
$QT_LOGGING_CONF, $QT_LOGGING_RULES, and .config/QtProject/qtlogging.ini.
*/
void QLoggingRegistry::init()
{
@ -266,6 +266,14 @@ void QLoggingRegistry::init()
envRules = parser.rules();
}
}
const QByteArray rulesSrc = qgetenv("QT_LOGGING_RULES");
if (!rulesSrc.isEmpty()) {
QTextStream stream(rulesSrc);
QLoggingSettingsParser parser;
parser.setSection(QStringLiteral("Rules"));
parser.setContent(stream);
envRules += parser.rules();
}
// get rules from qt configuration
QString envPath = QStandardPaths::locate(QStandardPaths::GenericConfigLocation,

View File

@ -64,6 +64,7 @@ private slots:
// ensure a clean environment
QStandardPaths::setTestModeEnabled(true);
qunsetenv("QT_LOGGING_CONF");
qunsetenv("QT_LOGGING_RULES");
}
void QLoggingRule_parse_data()
@ -219,6 +220,13 @@ private slots:
QCOMPARE(registry.envRules.size(), 1);
QCOMPARE(registry.rules.size(), 1);
// check that QT_LOGGING_RULES take precedence
qputenv("QT_LOGGING_RULES", "Digia.*=true");
registry.init();
QCOMPARE(registry.envRules.size(), 2);
QCOMPARE(registry.envRules.at(1).enabled, true);
QCOMPARE(registry.rules.size(), 2);
}
void QLoggingRegistry_config()