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 QLOGGINGCATEGORY_H
41#define QLOGGINGCATEGORY_H
42
43#include <QtCore/qglobal.h>
44#include <QtCore/qdebug.h>
45
46QT_BEGIN_NAMESPACE
47
48class Q_CORE_EXPORT QLoggingCategory
49{
50 Q_DISABLE_COPY(QLoggingCategory)
51public:
52 // ### Qt 6: Merge constructors
53 explicit QLoggingCategory(const char *category);
54 QLoggingCategory(const char *category, QtMsgType severityLevel);
55 ~QLoggingCategory();
56
57 bool isEnabled(QtMsgType type) const;
58 void setEnabled(QtMsgType type, bool enable);
59
60#ifdef Q_ATOMIC_INT8_IS_SUPPORTED
61 bool isDebugEnabled() const { return bools.enabledDebug.load(); }
62 bool isInfoEnabled() const { return bools.enabledInfo.load(); }
63 bool isWarningEnabled() const { return bools.enabledWarning.load(); }
64 bool isCriticalEnabled() const { return bools.enabledCritical.load(); }
65#else
66 bool isDebugEnabled() const { return enabled.load() >> DebugShift & 1; }
67 bool isInfoEnabled() const { return enabled.load() >> InfoShift & 1; }
68 bool isWarningEnabled() const { return enabled.load() >> WarningShift & 1; }
69 bool isCriticalEnabled() const { return enabled.load() >> CriticalShift & 1; }
70#endif
71 const char *categoryName() const { return name; }
72
73 // allows usage of both factory method and variable in qCX macros
74 QLoggingCategory &operator()() { return *this; }
75 const QLoggingCategory &operator()() const { return *this; }
76
77 static QLoggingCategory *defaultCategory();
78
79 typedef void (*CategoryFilter)(QLoggingCategory*);
80 static CategoryFilter installFilter(CategoryFilter);
81
82 static void setFilterRules(const QString &rules);
83
84private:
85 void init(const char *category, QtMsgType severityLevel);
86
87 Q_DECL_UNUSED_MEMBER void *d; // reserved for future use
88 const char *name;
89
90#ifdef Q_BIG_ENDIAN
91 enum { DebugShift = 0, WarningShift = 8, CriticalShift = 16, InfoShift = 24 };
92#else
93 enum { DebugShift = 24, WarningShift = 16, CriticalShift = 8, InfoShift = 0};
94#endif
95
96 struct AtomicBools {
97#ifdef Q_ATOMIC_INT8_IS_SUPPORTED
98 QBasicAtomicInteger<bool> enabledDebug;
99 QBasicAtomicInteger<bool> enabledWarning;
100 QBasicAtomicInteger<bool> enabledCritical;
101 QBasicAtomicInteger<bool> enabledInfo;
102#endif
103 };
104 union {
105 AtomicBools bools;
106 QBasicAtomicInt enabled;
107 };
108 Q_DECL_UNUSED_MEMBER bool placeholder[4]; // reserved for future use
109};
110
111#define Q_DECLARE_LOGGING_CATEGORY(name) \
112 extern const QLoggingCategory &name();
113
114#if defined(Q_COMPILER_VARIADIC_MACROS) || defined(Q_MOC_RUN)
115
116#define Q_LOGGING_CATEGORY(name, ...) \
117 const QLoggingCategory &name() \
118 { \
119 static const QLoggingCategory category(__VA_ARGS__); \
120 return category; \
121 }
122
123#define qCDebug(category, ...) \
124 for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
125 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug(__VA_ARGS__)
126#define qCInfo(category, ...) \
127 for (bool qt_category_enabled = category().isInfoEnabled(); qt_category_enabled; qt_category_enabled = false) \
128 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).info(__VA_ARGS__)
129#define qCWarning(category, ...) \
130 for (bool qt_category_enabled = category().isWarningEnabled(); qt_category_enabled; qt_category_enabled = false) \
131 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).warning(__VA_ARGS__)
132#define qCCritical(category, ...) \
133 for (bool qt_category_enabled = category().isCriticalEnabled(); qt_category_enabled; qt_category_enabled = false) \
134 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).critical(__VA_ARGS__)
135
136#else // defined(Q_COMPILER_VARIADIC_MACROS) || defined(Q_MOC_RUN)
137
138// Optional msgType argument not supported
139#define Q_LOGGING_CATEGORY(name, string) \
140 const QLoggingCategory &name() \
141 { \
142 static const QLoggingCategory category(string); \
143 return category; \
144 }
145
146// check for enabled category inside QMessageLogger.
147#define qCDebug qDebug
148#define qCInfo qInfo
149#define qCWarning qWarning
150#define qCCritical qCritical
151
152#endif // Q_COMPILER_VARIADIC_MACROS || defined(Q_MOC_RUN)
153
154#if defined(QT_NO_DEBUG_OUTPUT)
155# undef qCDebug
156# define qCDebug(category) QT_NO_QDEBUG_MACRO()
157#endif
158#if defined(QT_NO_INFO_OUTPUT)
159# undef qCInfo
160# define qCInfo(category) QT_NO_QDEBUG_MACRO()
161#endif
162#if defined(QT_NO_WARNING_OUTPUT)
163# undef qCWarning
164# define qCWarning(category) QT_NO_QDEBUG_MACRO()
165#endif
166
167QT_END_NAMESPACE
168
169#endif // QLOGGINGCATEGORY_H
170