Amends commit fa4bd30caa079a3b1e5eac1bb4f17365f456b8f9. Fixes: QTBUG-132111 Change-Id: Iad7969a1841b8cea162e4b0e6624d02313618ef1 Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit 529ebb31702e8465a9ba42f4c56c749cb7bf7b75) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
167 lines
4.7 KiB
C++
167 lines
4.7 KiB
C++
// Copyright (C) 2016 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
|
|
|
|
#ifndef QLOGGINGREGISTRY_P_H
|
|
#define QLOGGINGREGISTRY_P_H
|
|
|
|
//
|
|
// W A R N I N G
|
|
// -------------
|
|
//
|
|
// This file is not part of the Qt API. It exists for the convenience
|
|
// of a number of Qt sources files. This header file may change from
|
|
// version to version without notice, or even be removed.
|
|
//
|
|
// We mean it.
|
|
//
|
|
|
|
#include <QtCore/private/qglobal_p.h>
|
|
#include <QtCore/qloggingcategory.h>
|
|
#include <QtCore/qlist.h>
|
|
#include <QtCore/qhash.h>
|
|
#include <QtCore/qmutex.h>
|
|
#include <QtCore/qstring.h>
|
|
#include <QtCore/qtextstream.h>
|
|
|
|
#include <map>
|
|
|
|
class tst_QLoggingRegistry;
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
#define Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE_IMPL(name, env, categoryName) \
|
|
const QLoggingCategory &name() \
|
|
{ \
|
|
static constexpr char cname[] = categoryName; \
|
|
static_assert(cname[0] == 'q' && cname[1] == 't' && cname[2] == '.' \
|
|
&& cname[4] != '\0', "Category name must start with 'qt.'"); \
|
|
static const QLoggingCategoryWithEnvironmentOverride category(cname, env); \
|
|
return category; \
|
|
}
|
|
|
|
#define Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(name, env, categoryName) \
|
|
inline namespace QtPrivateLogging { \
|
|
Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE_IMPL(name, env, categoryName) \
|
|
} \
|
|
Q_WEAK_OVERLOAD \
|
|
Q_DECL_DEPRECATED_X("Logging categories should either be static or declared in a header") \
|
|
const QLoggingCategory &name() { return QtPrivateLogging::name(); }
|
|
|
|
#define Q_STATIC_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(name, env, categoryName) \
|
|
static Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE_IMPL(name, env, categoryName)
|
|
|
|
class Q_AUTOTEST_EXPORT QLoggingRule
|
|
{
|
|
public:
|
|
QLoggingRule();
|
|
QLoggingRule(QStringView pattern, bool enabled);
|
|
int pass(QLatin1StringView categoryName, QtMsgType type) const;
|
|
|
|
enum PatternFlag {
|
|
FullText = 0x1,
|
|
LeftFilter = 0x2,
|
|
RightFilter = 0x4,
|
|
MidFilter = LeftFilter | RightFilter
|
|
};
|
|
Q_DECLARE_FLAGS(PatternFlags, PatternFlag)
|
|
|
|
QString category;
|
|
int messageType = -1;
|
|
PatternFlags flags;
|
|
bool enabled = false;
|
|
|
|
private:
|
|
void parse(QStringView pattern);
|
|
};
|
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(QLoggingRule::PatternFlags)
|
|
Q_DECLARE_TYPEINFO(QLoggingRule, Q_RELOCATABLE_TYPE);
|
|
|
|
class Q_AUTOTEST_EXPORT QLoggingSettingsParser
|
|
{
|
|
public:
|
|
void setImplicitRulesSection(bool inRulesSection) { m_inRulesSection = inRulesSection; }
|
|
|
|
void setContent(QStringView content);
|
|
void setContent(QTextStream &stream);
|
|
|
|
QList<QLoggingRule> rules() const { return _rules; }
|
|
|
|
private:
|
|
void parseNextLine(QStringView line);
|
|
|
|
private:
|
|
bool m_inRulesSection = false;
|
|
QList<QLoggingRule> _rules;
|
|
};
|
|
|
|
class Q_AUTOTEST_EXPORT QLoggingRegistry
|
|
{
|
|
Q_DISABLE_COPY_MOVE(QLoggingRegistry)
|
|
public:
|
|
QLoggingRegistry();
|
|
|
|
void initializeRules();
|
|
|
|
void registerCategory(QLoggingCategory *category, QtMsgType enableForLevel);
|
|
void unregisterCategory(QLoggingCategory *category);
|
|
|
|
#ifndef QT_BUILD_INTERNAL
|
|
Q_CORE_EXPORT // always export from QtCore
|
|
#endif
|
|
void registerEnvironmentOverrideForCategory(const char *categoryName, const char *environment);
|
|
|
|
void setApiRules(const QString &content);
|
|
|
|
QLoggingCategory::CategoryFilter
|
|
installFilter(QLoggingCategory::CategoryFilter filter);
|
|
|
|
static QLoggingRegistry *instance();
|
|
|
|
private:
|
|
void updateRules();
|
|
|
|
static void defaultCategoryFilter(QLoggingCategory *category);
|
|
|
|
enum RuleSet {
|
|
// sorted by order in which defaultCategoryFilter considers them:
|
|
QtConfigRules,
|
|
ConfigRules,
|
|
ApiRules,
|
|
EnvironmentRules,
|
|
|
|
NumRuleSets
|
|
};
|
|
|
|
QMutex registryMutex;
|
|
|
|
// protected by mutex:
|
|
QList<QLoggingRule> ruleSets[NumRuleSets];
|
|
QHash<QLoggingCategory *, QtMsgType> categories;
|
|
QLoggingCategory::CategoryFilter categoryFilter;
|
|
std::map<QByteArrayView, const char *> qtCategoryEnvironmentOverrides;
|
|
|
|
friend class ::tst_QLoggingRegistry;
|
|
};
|
|
|
|
class QLoggingCategoryWithEnvironmentOverride : public QLoggingCategory
|
|
{
|
|
public:
|
|
QLoggingCategoryWithEnvironmentOverride(const char *category, const char *env)
|
|
: QLoggingCategory(registerOverride(category, env), QtInfoMsg)
|
|
{}
|
|
|
|
private:
|
|
static const char *registerOverride(const char *categoryName, const char *environment)
|
|
{
|
|
QLoggingRegistry *c = QLoggingRegistry::instance();
|
|
if (c)
|
|
c->registerEnvironmentOverrideForCategory(categoryName, environment);
|
|
return categoryName;
|
|
}
|
|
};
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
#endif // QLOGGINGREGISTRY_P_H
|