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:
parent
490298e939
commit
ef43967fcd
@ -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
|
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.
|
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
|
Rules can be set via \l setFilterRules(). Since Qt 5.3 logging rules can also
|
||||||
are also automatically loaded from the \c [Rules] section of a logging
|
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 file. Such configuration files are looked up in the QtProject
|
||||||
configuration directory, or explicitly set in a \c QT_LOGGING_CONF
|
configuration directory, or explicitly set in a \c QT_LOGGING_CONF
|
||||||
environment variable.
|
environment variable.
|
||||||
@ -117,13 +118,15 @@ Q_GLOBAL_STATIC_WITH_ARGS(QLoggingCategory, qtDefaultCategory,
|
|||||||
Rules set by \l setFilterRules() take precedence over rules specified
|
Rules set by \l setFilterRules() take precedence over rules specified
|
||||||
in the QtProject configuration directory, and can, in turn, be
|
in the QtProject configuration directory, and can, in turn, be
|
||||||
overwritten by rules from the configuration file specified by
|
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:
|
Order of evaluation:
|
||||||
\list
|
\list
|
||||||
\li Rules from QtProject/qlogging.ini
|
\li Rules from QtProject/qlogging.ini
|
||||||
\li Rules set by \l setFilterRules()
|
\li Rules set by \l setFilterRules()
|
||||||
\li Rules from file in \c QT_LOGGING_CONF
|
\li Rules from file in \c QT_LOGGING_CONF
|
||||||
|
\li Rules from environment variable QT_LOGGING_RULES
|
||||||
\endlist
|
\endlist
|
||||||
|
|
||||||
The \c QtProject/qlogging.ini file is looked up in all directories returned
|
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
|
\snippet qloggingcategory/main.cpp 2
|
||||||
|
|
||||||
\note The rules might be ignored if a custom category filter is installed
|
\note The rules might be ignored if a custom category filter is installed
|
||||||
with \l installFilter(), or if the user defined a custom logging
|
with \l installFilter(), or if the user defined \c QT_LOGGING_CONF or \c QT_LOGGING_RULES
|
||||||
configuration file in the \c QT_LOGGING_CONF environment variable.
|
environment variable.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
void QLoggingCategory::setFilterRules(const QString &rules)
|
void QLoggingCategory::setFilterRules(const QString &rules)
|
||||||
{
|
{
|
||||||
|
@ -248,7 +248,7 @@ static bool qtLoggingDebug()
|
|||||||
/*!
|
/*!
|
||||||
\internal
|
\internal
|
||||||
Initializes the rules database by loading
|
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()
|
void QLoggingRegistry::init()
|
||||||
{
|
{
|
||||||
@ -266,6 +266,14 @@ void QLoggingRegistry::init()
|
|||||||
envRules = parser.rules();
|
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
|
// get rules from qt configuration
|
||||||
QString envPath = QStandardPaths::locate(QStandardPaths::GenericConfigLocation,
|
QString envPath = QStandardPaths::locate(QStandardPaths::GenericConfigLocation,
|
||||||
|
@ -64,6 +64,7 @@ private slots:
|
|||||||
// ensure a clean environment
|
// ensure a clean environment
|
||||||
QStandardPaths::setTestModeEnabled(true);
|
QStandardPaths::setTestModeEnabled(true);
|
||||||
qunsetenv("QT_LOGGING_CONF");
|
qunsetenv("QT_LOGGING_CONF");
|
||||||
|
qunsetenv("QT_LOGGING_RULES");
|
||||||
}
|
}
|
||||||
|
|
||||||
void QLoggingRule_parse_data()
|
void QLoggingRule_parse_data()
|
||||||
@ -219,6 +220,13 @@ private slots:
|
|||||||
QCOMPARE(registry.envRules.size(), 1);
|
QCOMPARE(registry.envRules.size(), 1);
|
||||||
|
|
||||||
QCOMPARE(registry.rules.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()
|
void QLoggingRegistry_config()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user