1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QLOGGINGREGISTRY_P_H
41#define QLOGGINGREGISTRY_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists for the convenience
48// of a number of Qt sources files. This header file may change from
49// version to version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtCore/private/qglobal_p.h>
55#include <QtCore/qloggingcategory.h>
56#include <QtCore/qlist.h>
57#include <QtCore/qmap.h>
58#include <QtCore/qmutex.h>
59#include <QtCore/qstring.h>
60#include <QtCore/qtextstream.h>
61
62class tst_QLoggingRegistry;
63
64QT_BEGIN_NAMESPACE
65
66class Q_AUTOTEST_EXPORT QLoggingRule
67{
68public:
69 QLoggingRule();
70 QLoggingRule(QStringView pattern, bool enabled);
71 int pass(QLatin1String categoryName, QtMsgType type) const;
72
73 enum PatternFlag {
74 FullText = 0x1,
75 LeftFilter = 0x2,
76 RightFilter = 0x4,
77 MidFilter = LeftFilter | RightFilter
78 };
79 Q_DECLARE_FLAGS(PatternFlags, PatternFlag)
80
81 QString category;
82 int messageType;
83 PatternFlags flags;
84 bool enabled;
85
86private:
87 void parse(QStringView pattern);
88};
89
90Q_DECLARE_OPERATORS_FOR_FLAGS(QLoggingRule::PatternFlags)
91Q_DECLARE_TYPEINFO(QLoggingRule, Q_MOVABLE_TYPE);
92
93class Q_AUTOTEST_EXPORT QLoggingSettingsParser
94{
95public:
96 void setImplicitRulesSection(bool inRulesSection) { m_inRulesSection = inRulesSection; }
97
98 void setContent(const QString &content);
99 void setContent(QTextStream &stream);
100
101 QList<QLoggingRule> rules() const { return _rules; }
102
103private:
104 void parseNextLine(QStringView line);
105
106private:
107 bool m_inRulesSection = false;
108 QList<QLoggingRule> _rules;
109};
110
111class Q_AUTOTEST_EXPORT QLoggingRegistry
112{
113public:
114 QLoggingRegistry();
115
116 void initializeRules();
117
118 void registerCategory(QLoggingCategory *category, QtMsgType enableForLevel);
119 void unregisterCategory(QLoggingCategory *category);
120
121 void setApiRules(const QString &content);
122
123 QLoggingCategory::CategoryFilter
124 installFilter(QLoggingCategory::CategoryFilter filter);
125
126 static QLoggingRegistry *instance();
127
128private:
129 void updateRules();
130
131 static void defaultCategoryFilter(QLoggingCategory *category);
132
133 enum RuleSet {
134 // sorted by order in which defaultCategoryFilter considers them:
135 QtConfigRules,
136 ConfigRules,
137 ApiRules,
138 EnvironmentRules,
139
140 NumRuleSets
141 };
142
143 QMutex registryMutex;
144
145 // protected by mutex:
146 QList<QLoggingRule> ruleSets[NumRuleSets];
147 QHash<QLoggingCategory *, QtMsgType> categories;
148 QLoggingCategory::CategoryFilter categoryFilter;
149
150 friend class ::tst_QLoggingRegistry;
151};
152
153QT_END_NAMESPACE
154
155#endif // QLOGGINGREGISTRY_P_H
156