summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qloggingregistry.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/io/qloggingregistry.cpp')
-rw-r--r--src/corelib/io/qloggingregistry.cpp148
1 files changed, 67 insertions, 81 deletions
diff --git a/src/corelib/io/qloggingregistry.cpp b/src/corelib/io/qloggingregistry.cpp
index 1c26e3d1c1..b4181a2fa6 100644
--- a/src/corelib/io/qloggingregistry.cpp
+++ b/src/corelib/io/qloggingregistry.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qloggingregistry_p.h"
@@ -55,20 +19,20 @@
// We can't use the default macros because this would lead to recursion.
// Instead let's define our own one that unconditionally logs...
-#define debugMsg QMessageLogger(__FILE__, __LINE__, __FUNCTION__, "qt.core.logging").debug
-#define warnMsg QMessageLogger(__FILE__, __LINE__, __FUNCTION__, "qt.core.logging").warning
-
+#define debugMsg QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, "qt.core.logging").debug
+#define warnMsg QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, "qt.core.logging").warning
QT_BEGIN_NAMESPACE
+using namespace Qt::StringLiterals;
+
Q_GLOBAL_STATIC(QLoggingRegistry, qtLoggingRegistry)
/*!
\internal
Constructs a logging rule with default values.
*/
-QLoggingRule::QLoggingRule() :
- enabled(false)
+QLoggingRule::QLoggingRule()
{
}
@@ -76,9 +40,7 @@ QLoggingRule::QLoggingRule() :
\internal
Constructs a logging rule.
*/
-QLoggingRule::QLoggingRule(QStringView pattern, bool enabled) :
- messageType(-1),
- enabled(enabled)
+QLoggingRule::QLoggingRule(QStringView pattern, bool enabled) : enabled(enabled)
{
parse(pattern);
}
@@ -88,7 +50,7 @@ QLoggingRule::QLoggingRule(QStringView pattern, bool enabled) :
Return value 1 means filter passed, 0 means filter doesn't influence this
category, -1 means category doesn't pass this filter.
*/
-int QLoggingRule::pass(QLatin1String cat, QtMsgType msgType) const
+int QLoggingRule::pass(QLatin1StringView cat, QtMsgType msgType) const
{
// check message type
if (messageType > -1 && messageType != msgType)
@@ -102,7 +64,7 @@ int QLoggingRule::pass(QLatin1String cat, QtMsgType msgType) const
return 0;
}
- const int idx = cat.indexOf(category);
+ const qsizetype idx = cat.indexOf(category);
if (idx >= 0) {
if (flags == MidFilter) {
// matches somewhere
@@ -113,7 +75,7 @@ int QLoggingRule::pass(QLatin1String cat, QtMsgType msgType) const
return (enabled ? 1 : -1);
} else if (flags == RightFilter) {
// matches right
- if (idx == (cat.size() - category.count()))
+ if (idx == (cat.size() - category.size()))
return (enabled ? 1 : -1);
}
}
@@ -134,34 +96,35 @@ void QLoggingRule::parse(QStringView pattern)
QStringView p;
// strip trailing ".messagetype"
- if (pattern.endsWith(QLatin1String(".debug"))) {
+ if (pattern.endsWith(".debug"_L1)) {
p = pattern.chopped(6); // strlen(".debug")
messageType = QtDebugMsg;
- } else if (pattern.endsWith(QLatin1String(".info"))) {
+ } else if (pattern.endsWith(".info"_L1)) {
p = pattern.chopped(5); // strlen(".info")
messageType = QtInfoMsg;
- } else if (pattern.endsWith(QLatin1String(".warning"))) {
+ } else if (pattern.endsWith(".warning"_L1)) {
p = pattern.chopped(8); // strlen(".warning")
messageType = QtWarningMsg;
- } else if (pattern.endsWith(QLatin1String(".critical"))) {
+ } else if (pattern.endsWith(".critical"_L1)) {
p = pattern.chopped(9); // strlen(".critical")
messageType = QtCriticalMsg;
} else {
p = pattern;
}
- if (!p.contains(QLatin1Char('*'))) {
+ const QChar asterisk = u'*';
+ if (!p.contains(asterisk)) {
flags = FullText;
} else {
- if (p.endsWith(QLatin1Char('*'))) {
+ if (p.endsWith(asterisk)) {
flags |= LeftFilter;
p = p.chopped(1);
}
- if (p.startsWith(QLatin1Char('*'))) {
+ if (p.startsWith(asterisk)) {
flags |= RightFilter;
p = p.mid(1);
}
- if (p.contains(QLatin1Char('*'))) // '*' only supported at start/end
+ if (p.contains(asterisk)) // '*' only supported at start/end
flags = PatternFlags();
}
@@ -217,33 +180,33 @@ void QLoggingSettingsParser::parseNextLine(QStringView line)
line = line.trimmed();
// comment
- if (line.startsWith(QLatin1Char(';')))
+ if (line.startsWith(u';'))
return;
- if (line.startsWith(QLatin1Char('[')) && line.endsWith(QLatin1Char(']'))) {
+ if (line.startsWith(u'[') && line.endsWith(u']')) {
// new section
auto sectionName = line.mid(1).chopped(1).trimmed();
- m_inRulesSection = sectionName.compare(QLatin1String("rules"), Qt::CaseInsensitive) == 0;
+ m_inRulesSection = sectionName.compare("rules"_L1, Qt::CaseInsensitive) == 0;
return;
}
if (m_inRulesSection) {
- int equalPos = line.indexOf(QLatin1Char('='));
+ const qsizetype equalPos = line.indexOf(u'=');
if (equalPos != -1) {
- if (line.lastIndexOf(QLatin1Char('=')) == equalPos) {
+ if (line.lastIndexOf(u'=') == equalPos) {
const auto key = line.left(equalPos).trimmed();
#if QT_CONFIG(settings)
QString tmp;
- QSettingsPrivate::iniUnescapedKey(key.toUtf8(), 0, key.length(), tmp);
+ QSettingsPrivate::iniUnescapedKey(key.toUtf8(), tmp);
QStringView pattern = qToStringViewIgnoringNull(tmp);
#else
QStringView pattern = key;
#endif
const auto valueStr = line.mid(equalPos + 1).trimmed();
int value = -1;
- if (valueStr == QLatin1String("true"))
+ if (valueStr == "true"_L1)
value = 1;
- else if (valueStr == QLatin1String("false"))
+ else if (valueStr == "false"_L1)
value = 0;
QLoggingRule rule(pattern, (value == 1));
if (rule.flags != 0 && (value != -1))
@@ -278,20 +241,29 @@ QLoggingRegistry::QLoggingRegistry()
static bool qtLoggingDebug()
{
- static const bool debugEnv = qEnvironmentVariableIsSet("QT_LOGGING_DEBUG");
+ static const bool debugEnv = [] {
+ bool debug = qEnvironmentVariableIsSet("QT_LOGGING_DEBUG");
+ if (debug)
+ debugMsg("QT_LOGGING_DEBUG environment variable is set.");
+ return debug;
+ }();
return debugEnv;
}
static QList<QLoggingRule> loadRulesFromFile(const QString &filePath)
{
+ if (qtLoggingDebug()) {
+ debugMsg("Checking \"%s\" for rules",
+ QDir::toNativeSeparators(filePath).toUtf8().constData());
+ }
+
QFile file(filePath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
- if (qtLoggingDebug())
- debugMsg("Loading \"%s\" ...",
- QDir::toNativeSeparators(file.fileName()).toUtf8().constData());
QTextStream stream(&file);
QLoggingSettingsParser parser;
parser.setContent(stream);
+ if (qtLoggingDebug())
+ debugMsg("Loaded %td rules", static_cast<ptrdiff_t>(parser.rules().size()));
return parser.rules();
}
return QList<QLoggingRule>();
@@ -304,19 +276,30 @@ static QList<QLoggingRule> loadRulesFromFile(const QString &filePath)
*/
void QLoggingRegistry::initializeRules()
{
+ if (qtLoggingDebug()) {
+ debugMsg("Initializing the rules database ...");
+ debugMsg("Checking %s environment variable", "QTLOGGING_CONF");
+ }
QList<QLoggingRule> er, qr, cr;
// get rules from environment
const QByteArray rulesFilePath = qgetenv("QT_LOGGING_CONF");
if (!rulesFilePath.isEmpty())
er = loadRulesFromFile(QFile::decodeName(rulesFilePath));
+ if (qtLoggingDebug())
+ debugMsg("Checking %s environment variable", "QT_LOGGING_RULES");
+
const QByteArray rulesSrc = qgetenv("QT_LOGGING_RULES").replace(';', '\n');
if (!rulesSrc.isEmpty()) {
- QTextStream stream(rulesSrc);
- QLoggingSettingsParser parser;
- parser.setImplicitRulesSection(true);
- parser.setContent(stream);
- er += parser.rules();
+ QTextStream stream(rulesSrc);
+ QLoggingSettingsParser parser;
+ parser.setImplicitRulesSection(true);
+ parser.setContent(stream);
+
+ if (qtLoggingDebug())
+ debugMsg("Loaded %td rules", static_cast<ptrdiff_t>(parser.rules().size()));
+
+ er += parser.rules();
}
const QString configFileName = QStringLiteral("qtlogging.ini");
@@ -354,8 +337,11 @@ void QLoggingRegistry::registerCategory(QLoggingCategory *cat, QtMsgType enableF
{
const auto locker = qt_scoped_lock(registryMutex);
- if (!categories.contains(cat)) {
- categories.insert(cat, enableForLevel);
+ const auto oldSize = categories.size();
+ auto &e = categories[cat];
+ if (categories.size() != oldSize) {
+ // new entry
+ e = enableForLevel;
(*categoryFilter)(cat);
}
}
@@ -378,10 +364,10 @@ void QLoggingRegistry::unregisterCategory(QLoggingCategory *cat)
for enabling debugging by default for category \a categoryName. The
category name must start with "qt."
*/
-void QLoggingRegistry::registerEnvironmentOverrideForCategory(QByteArrayView categoryName,
- QByteArrayView environment)
+void QLoggingRegistry::registerEnvironmentOverrideForCategory(const char *categoryName,
+ const char *environment)
{
- qtCategoryEnvironmentOverrides.insert(categoryName, environment);
+ qtCategoryEnvironmentOverrides.insert_or_assign(categoryName, environment);
}
/*!
@@ -473,11 +459,11 @@ void QLoggingRegistry::defaultCategoryFilter(QLoggingCategory *cat)
if (it == reg->qtCategoryEnvironmentOverrides.end())
debug = false;
else
- debug = qEnvironmentVariableIntValue(it.value().data());
+ debug = qEnvironmentVariableIntValue(it->second);
}
}
- const auto categoryName = QLatin1String(cat->categoryName());
+ const auto categoryName = QLatin1StringView(cat->categoryName());
for (const auto &ruleSet : reg->ruleSets) {
for (const auto &rule : ruleSet) {