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 explicit QLoggingCategory(const char *category, QtMsgType severityLevel = QtDebugMsg);
53 ~QLoggingCategory();
54
55 bool isEnabled(QtMsgType type) const;
56 void setEnabled(QtMsgType type, bool enable);
57
58#ifdef Q_ATOMIC_INT8_IS_SUPPORTED
59 bool isDebugEnabled() const { return bools.enabledDebug.loadRelaxed(); }
60 bool isInfoEnabled() const { return bools.enabledInfo.loadRelaxed(); }
61 bool isWarningEnabled() const { return bools.enabledWarning.loadRelaxed(); }
62 bool isCriticalEnabled() const { return bools.enabledCritical.loadRelaxed(); }
63#else
64 bool isDebugEnabled() const { return enabled.loadRelaxed() >> DebugShift & 1; }
65 bool isInfoEnabled() const { return enabled.loadRelaxed() >> InfoShift & 1; }
66 bool isWarningEnabled() const { return enabled.loadRelaxed() >> WarningShift & 1; }
67 bool isCriticalEnabled() const { return enabled.loadRelaxed() >> CriticalShift & 1; }
68#endif
69 const char *categoryName() const { return name; }
70
71 // allows usage of both factory method and variable in qCX macros
72 QLoggingCategory &operator()() { return *this; }
73 const QLoggingCategory &operator()() const { return *this; }
74
75 static QLoggingCategory *defaultCategory();
76
77 typedef void (*CategoryFilter)(QLoggingCategory*);
78 static CategoryFilter installFilter(CategoryFilter);
79
80 static void setFilterRules(const QString &rules);
81
82private:
83 void init(const char *category, QtMsgType severityLevel);
84
85 Q_DECL_UNUSED_MEMBER void *d; // reserved for future use
86 const char *name;
87
88#ifdef Q_BIG_ENDIAN
89 enum { DebugShift = 0, WarningShift = 8, CriticalShift = 16, InfoShift = 24 };
90#else
91 enum { DebugShift = 24, WarningShift = 16, CriticalShift = 8, InfoShift = 0};
92#endif
93
94 struct AtomicBools {
95#ifdef Q_ATOMIC_INT8_IS_SUPPORTED
96 QBasicAtomicInteger<bool> enabledDebug;
97 QBasicAtomicInteger<bool> enabledWarning;
98 QBasicAtomicInteger<bool> enabledCritical;
99 QBasicAtomicInteger<bool> enabledInfo;
100#endif
101 };
102 union {
103 AtomicBools bools;
104 QBasicAtomicInt enabled;
105 };
106 Q_DECL_UNUSED_MEMBER bool placeholder[4]; // reserved for future use
107};
108
109#define Q_DECLARE_LOGGING_CATEGORY(name) \
110 extern const QLoggingCategory &name();
111
112#define Q_LOGGING_CATEGORY(name, ...) \
113 const QLoggingCategory &name() \
114 { \
115 static const QLoggingCategory category(__VA_ARGS__); \
116 return category; \
117 }
118
119#if !defined(QT_NO_DEBUG_OUTPUT)
120# define qCDebug(category, ...) \
121 for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
122 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug(__VA_ARGS__)
123#else
124# define qCDebug(category, ...) QT_NO_QDEBUG_MACRO()
125#endif
126
127#if !defined(QT_NO_INFO_OUTPUT)
128# define qCInfo(category, ...) \
129 for (bool qt_category_enabled = category().isInfoEnabled(); qt_category_enabled; qt_category_enabled = false) \
130 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).info(__VA_ARGS__)
131#else
132# define qCInfo(category, ...) QT_NO_QDEBUG_MACRO()
133#endif
134
135#if !defined(QT_NO_WARNING_OUTPUT)
136# define qCWarning(category, ...) \
137 for (bool qt_category_enabled = category().isWarningEnabled(); qt_category_enabled; qt_category_enabled = false) \
138 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).warning(__VA_ARGS__)
139#else
140# define qCWarning(category, ...) QT_NO_QDEBUG_MACRO()
141#endif
142
143#define qCCritical(category, ...) \
144 for (bool qt_category_enabled = category().isCriticalEnabled(); qt_category_enabled; qt_category_enabled = false) \
145 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).critical(__VA_ARGS__)
146
147QT_END_NAMESPACE
148
149#endif // QLOGGINGCATEGORY_H
150