1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QLOGGINGREGISTRY_P_H
5#define QLOGGINGREGISTRY_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of a number of Qt sources files. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19#include <QtCore/qloggingcategory.h>
20#include <QtCore/qlist.h>
21#include <QtCore/qhash.h>
22#include <QtCore/qmap.h>
23#include <QtCore/qmutex.h>
24#include <QtCore/qstring.h>
25#include <QtCore/qtextstream.h>
26
27class tst_QLoggingRegistry;
28
29QT_BEGIN_NAMESPACE
30
31#define Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(name, env, categoryName) \
32 const QLoggingCategory &name() \
33 { \
34 static constexpr char cname[] = categoryName; \
35 static_assert(cname[0] == 'q' && cname[1] == 't' && cname[2] == '.' \
36 && cname[4] != '\0', "Category name must start with 'qt.'"); \
37 static const QLoggingCategoryWithEnvironmentOverride category(cname, env); \
38 return category; \
39 }
40
41class Q_AUTOTEST_EXPORT QLoggingRule
42{
43public:
44 QLoggingRule();
45 QLoggingRule(QStringView pattern, bool enabled);
46 int pass(QLatin1StringView categoryName, QtMsgType type) const;
47
48 enum PatternFlag {
49 FullText = 0x1,
50 LeftFilter = 0x2,
51 RightFilter = 0x4,
52 MidFilter = LeftFilter | RightFilter
53 };
54 Q_DECLARE_FLAGS(PatternFlags, PatternFlag)
55
56 QString category;
57 int messageType;
58 PatternFlags flags;
59 bool enabled;
60
61private:
62 void parse(QStringView pattern);
63};
64
65Q_DECLARE_OPERATORS_FOR_FLAGS(QLoggingRule::PatternFlags)
66Q_DECLARE_TYPEINFO(QLoggingRule, Q_RELOCATABLE_TYPE);
67
68class Q_AUTOTEST_EXPORT QLoggingSettingsParser
69{
70public:
71 void setImplicitRulesSection(bool inRulesSection) { m_inRulesSection = inRulesSection; }
72
73 void setContent(QStringView content);
74 void setContent(QTextStream &stream);
75
76 QList<QLoggingRule> rules() const { return _rules; }
77
78private:
79 void parseNextLine(QStringView line);
80
81private:
82 bool m_inRulesSection = false;
83 QList<QLoggingRule> _rules;
84};
85
86class Q_AUTOTEST_EXPORT QLoggingRegistry
87{
88public:
89 QLoggingRegistry();
90
91 void initializeRules();
92
93 void registerCategory(QLoggingCategory *category, QtMsgType enableForLevel);
94 void unregisterCategory(QLoggingCategory *category);
95
96#ifndef QT_BUILD_INTERNAL
97 Q_CORE_EXPORT // always export from QtCore
98#endif
99 void registerEnvironmentOverrideForCategory(QByteArrayView categoryName, QByteArrayView environment);
100
101 void setApiRules(const QString &content);
102
103 QLoggingCategory::CategoryFilter
104 installFilter(QLoggingCategory::CategoryFilter filter);
105
106 static QLoggingRegistry *instance();
107
108private:
109 void updateRules();
110
111 static void defaultCategoryFilter(QLoggingCategory *category);
112
113 enum RuleSet {
114 // sorted by order in which defaultCategoryFilter considers them:
115 QtConfigRules,
116 ConfigRules,
117 ApiRules,
118 EnvironmentRules,
119
120 NumRuleSets
121 };
122
123 QMutex registryMutex;
124
125 // protected by mutex:
126 QList<QLoggingRule> ruleSets[NumRuleSets];
127 QHash<QLoggingCategory *, QtMsgType> categories;
128 QLoggingCategory::CategoryFilter categoryFilter;
129 QMap<QByteArrayView, QByteArrayView> qtCategoryEnvironmentOverrides;
130
131 friend class ::tst_QLoggingRegistry;
132};
133
134class QLoggingCategoryWithEnvironmentOverride : public QLoggingCategory
135{
136public:
137 QLoggingCategoryWithEnvironmentOverride(const char *category, const char *env)
138 : QLoggingCategory(registerOverride(categoryName: category, environment: env), QtInfoMsg)
139 {}
140
141private:
142 static const char *registerOverride(QByteArrayView categoryName, QByteArrayView environment)
143 {
144 QLoggingRegistry *c = QLoggingRegistry::instance();
145 if (c)
146 c->registerEnvironmentOverrideForCategory(categoryName, environment);
147 return categoryName.data();
148 }
149};
150
151QT_END_NAMESPACE
152
153#endif // QLOGGINGREGISTRY_P_H
154

source code of qtbase/src/corelib/io/qloggingregistry_p.h