Line data Source code
1 : /****************************************************************************
2 : **
3 : ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 : ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 : ** conditions see http://qt.digia.com/licensing. For further information
15 : ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 : ** Foundation and appearing in the file LICENSE.LGPL included in the
21 : ** packaging of this file. Please review the following information to
22 : ** ensure the GNU Lesser General Public License version 2.1 requirements
23 : ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 : **
25 : ** In addition, as a special exception, Digia gives you certain additional
26 : ** rights. These rights are described in the Digia Qt LGPL Exception
27 : ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 : **
29 : ** GNU General Public License Usage
30 : ** Alternatively, this file may be used under the terms of the GNU
31 : ** General Public License version 3.0 as published by the Free Software
32 : ** Foundation and appearing in the file LICENSE.GPL included in the
33 : ** packaging of this file. Please review the following information to
34 : ** ensure the GNU General Public License version 3.0 requirements will be
35 : ** met: http://www.gnu.org/copyleft/gpl.html.
36 : **
37 : **
38 : ** $QT_END_LICENSE$
39 : **
40 : ****************************************************************************/
41 :
42 : #ifndef QLOGGINGCATEGORY_H
43 : #define QLOGGINGCATEGORY_H
44 :
45 : #include <QtCore/qglobal.h>
46 : #include <QtCore/qdebug.h>
47 :
48 : QT_BEGIN_NAMESPACE
49 :
50 : class Q_CORE_EXPORT QLoggingCategory
51 : {
52 : Q_DISABLE_COPY(QLoggingCategory)
53 : public:
54 : explicit QLoggingCategory(const char *category);
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 0 : bool isDebugEnabled() const { return bools.enabledDebug.load(); }
62 0 : bool isWarningEnabled() const { return bools.enabledWarning.load(); }
63 : bool isCriticalEnabled() const { return bools.enabledCritical.load(); }
64 : #else
65 : bool isDebugEnabled() const { return enabled.load() >> DebugShift & 1; }
66 : bool isWarningEnabled() const { return enabled.load() >> WarningShift & 1; }
67 : bool isCriticalEnabled() const { return enabled.load() >> CriticalShift & 1; }
68 : #endif
69 0 : 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 :
82 : private:
83 : void *d; // reserved for future use
84 : const char *name;
85 :
86 : #ifdef Q_BIG_ENDIAN
87 : enum { DebugShift = 0, WarningShift = 8, CriticalShift = 16 };
88 : #else
89 : enum { DebugShift = 24, WarningShift = 16, CriticalShift = 8 };
90 : #endif
91 :
92 : struct AtomicBools {
93 : #ifdef Q_ATOMIC_INT8_IS_SUPPORTED
94 : QBasicAtomicInteger<bool> enabledDebug;
95 : QBasicAtomicInteger<bool> enabledWarning;
96 : QBasicAtomicInteger<bool> enabledCritical;
97 : #endif
98 : };
99 : union {
100 : AtomicBools bools;
101 : QBasicAtomicInt enabled;
102 : };
103 : bool placeholder[4]; // reserve for future use
104 : };
105 :
106 : #define Q_DECLARE_LOGGING_CATEGORY(name) \
107 : extern const QLoggingCategory &name();
108 :
109 : // relies on QLoggingCategory(QString) being thread safe!
110 : #define Q_LOGGING_CATEGORY(name, string) \
111 : const QLoggingCategory &name() \
112 : { \
113 : static const QLoggingCategory category(string); \
114 : return category; \
115 : }
116 :
117 : #ifdef Q_COMPILER_VARIADIC_MACROS
118 :
119 : #define qCDebug(category, ...) \
120 : for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
121 : QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).debug(__VA_ARGS__)
122 : #define qCWarning(category, ...) \
123 : for (bool qt_category_enabled = category().isWarningEnabled(); qt_category_enabled; qt_category_enabled = false) \
124 : QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).warning(__VA_ARGS__)
125 : #define qCCritical(category, ...) \
126 : for (bool qt_category_enabled = category().isCriticalEnabled(); qt_category_enabled; qt_category_enabled = false) \
127 : QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).critical(__VA_ARGS__)
128 :
129 : #else
130 :
131 : // check for enabled category inside QMessageLogger.
132 : #define qCDebug qDebug
133 : #define qCWarning qWarning
134 : #define qCCritical qCritical
135 :
136 : #endif // Q_COMPILER_VARIADIC_MACROS
137 :
138 : #if defined(QT_NO_DEBUG_OUTPUT)
139 : # undef qCDebug
140 : # define qCDebug(category) QT_NO_QDEBUG_MACRO()
141 : #endif
142 : #if defined(QT_NO_WARNING_OUTPUT)
143 : # undef qCWarning
144 : # define qCWarning(category) QT_NO_QWARNING_MACRO()
145 : #endif
146 :
147 : QT_END_NAMESPACE
148 :
149 : #endif // QLOGGINGCATEGORY_H
|