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 
46 QT_BEGIN_NAMESPACE
47 
48 class Q_CORE_EXPORT QLoggingCategory
49 {
50     Q_DISABLE_COPY(QLoggingCategory)
51 public:
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
isDebugEnabled()61     bool isDebugEnabled() const { return bools.enabledDebug.loadRelaxed(); }
isInfoEnabled()62     bool isInfoEnabled() const { return bools.enabledInfo.loadRelaxed(); }
isWarningEnabled()63     bool isWarningEnabled() const { return bools.enabledWarning.loadRelaxed(); }
isCriticalEnabled()64     bool isCriticalEnabled() const { return bools.enabledCritical.loadRelaxed(); }
65 #else
isDebugEnabled()66     bool isDebugEnabled() const { return enabled.loadRelaxed() >> DebugShift & 1; }
isInfoEnabled()67     bool isInfoEnabled() const { return enabled.loadRelaxed() >> InfoShift & 1; }
isWarningEnabled()68     bool isWarningEnabled() const { return enabled.loadRelaxed() >> WarningShift & 1; }
isCriticalEnabled()69     bool isCriticalEnabled() const { return enabled.loadRelaxed() >> CriticalShift & 1; }
70 #endif
categoryName()71     const char *categoryName() const { return name; }
72 
73     // allows usage of both factory method and variable in qCX macros
operator()74     QLoggingCategory &operator()() { return *this; }
operator()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 
84 private:
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 #define Q_LOGGING_CATEGORY(name, ...) \
115     const QLoggingCategory &name() \
116     { \
117         static const QLoggingCategory category(__VA_ARGS__); \
118         return category; \
119     }
120 
121 #if !defined(QT_NO_DEBUG_OUTPUT)
122 #  define qCDebug(category, ...) \
123     for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
124         QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug(__VA_ARGS__)
125 #else
126 #  define qCDebug(category, ...) QT_NO_QDEBUG_MACRO()
127 #endif
128 
129 #if !defined(QT_NO_INFO_OUTPUT)
130 #  define qCInfo(category, ...) \
131     for (bool qt_category_enabled = category().isInfoEnabled(); qt_category_enabled; qt_category_enabled = false) \
132         QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).info(__VA_ARGS__)
133 #else
134 #  define qCInfo(category, ...) QT_NO_QDEBUG_MACRO()
135 #endif
136 
137 #if !defined(QT_NO_WARNING_OUTPUT)
138 #  define qCWarning(category, ...) \
139     for (bool qt_category_enabled = category().isWarningEnabled(); qt_category_enabled; qt_category_enabled = false) \
140         QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).warning(__VA_ARGS__)
141 #else
142 #  define qCWarning(category, ...) QT_NO_QDEBUG_MACRO()
143 #endif
144 
145 #define qCCritical(category, ...) \
146     for (bool qt_category_enabled = category().isCriticalEnabled(); qt_category_enabled; qt_category_enabled = false) \
147         QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).critical(__VA_ARGS__)
148 
149 QT_END_NAMESPACE
150 
151 #endif // QLOGGINGCATEGORY_H
152