Line data Source code
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 : #include <QtCore/qglobal.h>
41 :
42 : #ifndef QLOGGING_H
43 : #define QLOGGING_H
44 :
45 : #if 0
46 : // header is automatically included in qglobal.h
47 : #pragma qt_no_master_include
48 : #endif
49 :
50 : QT_BEGIN_NAMESPACE
51 :
52 : /*
53 : Forward declarations only.
54 :
55 : In order to use the qDebug() stream, you must #include<QDebug>
56 : */
57 : class QDebug;
58 : class QNoDebug;
59 :
60 : enum QtMsgType { QtDebugMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg, QtInfoMsg, QtSystemMsg = QtCriticalMsg };
61 :
62 : class QMessageLogContext
63 : {
64 : Q_DISABLE_COPY(QMessageLogContext)
65 : public:
66 : Q_DECL_CONSTEXPR QMessageLogContext()
67 : : version(2), line(0), file(Q_NULLPTR), function(Q_NULLPTR), category(Q_NULLPTR) {}
68 0 : Q_DECL_CONSTEXPR QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName)
69 0 : : version(2), line(lineNumber), file(fileName), function(functionName), category(categoryName) {}
70 :
71 : void copy(const QMessageLogContext &logContext);
72 :
73 : int version;
74 : int line;
75 : const char *file;
76 : const char *function;
77 : const char *category;
78 :
79 : private:
80 : friend class QMessageLogger;
81 : friend class QDebug;
82 : };
83 :
84 : class QLoggingCategory;
85 :
86 : class Q_CORE_EXPORT QMessageLogger
87 : {
88 : Q_DISABLE_COPY(QMessageLogger)
89 : public:
90 : Q_DECL_CONSTEXPR QMessageLogger() : context() {}
91 0 : Q_DECL_CONSTEXPR QMessageLogger(const char *file, int line, const char *function)
92 0 : : context(file, line, function, "default") {}
93 0 : Q_DECL_CONSTEXPR QMessageLogger(const char *file, int line, const char *function, const char *category)
94 0 : : context(file, line, function, category) {}
95 :
96 : void debug(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
97 : void noDebug(const char *, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3)
98 : {}
99 : void info(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
100 : void warning(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
101 : void critical(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
102 :
103 : typedef const QLoggingCategory &(*CategoryFunction)();
104 :
105 : void debug(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
106 : void debug(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
107 : void info(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
108 : void info(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
109 : void warning(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
110 : void warning(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
111 : void critical(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
112 : void critical(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
113 :
114 : #ifndef Q_CC_MSVC
115 : Q_NORETURN
116 : #endif
117 : void fatal(const char *msg, ...) const Q_DECL_NOTHROW Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
118 :
119 : #ifndef QT_NO_DEBUG_STREAM
120 : QDebug debug() const;
121 : QDebug debug(const QLoggingCategory &cat) const;
122 : QDebug debug(CategoryFunction catFunc) const;
123 : QDebug info() const;
124 : QDebug info(const QLoggingCategory &cat) const;
125 : QDebug info(CategoryFunction catFunc) const;
126 : QDebug warning() const;
127 : QDebug warning(const QLoggingCategory &cat) const;
128 : QDebug warning(CategoryFunction catFunc) const;
129 : QDebug critical() const;
130 : QDebug critical(const QLoggingCategory &cat) const;
131 : QDebug critical(CategoryFunction catFunc) const;
132 :
133 : QNoDebug noDebug() const Q_DECL_NOTHROW;
134 : #endif // QT_NO_DEBUG_STREAM
135 :
136 : private:
137 : QMessageLogContext context;
138 : };
139 :
140 : #if !defined(QT_MESSAGELOGCONTEXT) && !defined(QT_NO_MESSAGELOGCONTEXT)
141 : # if defined(QT_NO_DEBUG)
142 : # define QT_NO_MESSAGELOGCONTEXT
143 : # else
144 : # define QT_MESSAGELOGCONTEXT
145 : # endif
146 : #endif
147 :
148 : #ifdef QT_MESSAGELOGCONTEXT
149 : #define QT_MESSAGELOG_FILE __FILE__
150 : #define QT_MESSAGELOG_LINE __LINE__
151 : #define QT_MESSAGELOG_FUNC Q_FUNC_INFO
152 : #else
153 : #define QT_MESSAGELOG_FILE Q_NULLPTR
154 : #define QT_MESSAGELOG_LINE 0
155 : #define QT_MESSAGELOG_FUNC Q_NULLPTR
156 : #endif
157 :
158 : #define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
159 : #define qInfo QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).info
160 : #define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
161 : #define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical
162 : #define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal
163 :
164 : #define QT_NO_QDEBUG_MACRO while (false) QMessageLogger().noDebug
165 :
166 : #if defined(QT_NO_DEBUG_OUTPUT)
167 : # undef qDebug
168 : # define qDebug QT_NO_QDEBUG_MACRO
169 : #endif
170 : #if defined(QT_NO_INFO_OUTPUT)
171 : # undef qInfo
172 : # define qInfo QT_NO_QDEBUG_MACRO
173 : #endif
174 : #if defined(QT_NO_WARNING_OUTPUT)
175 : # undef qWarning
176 : # define qWarning QT_NO_QDEBUG_MACRO
177 : #endif
178 :
179 : Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context,
180 : const QString &message);
181 :
182 : Q_CORE_EXPORT void qErrnoWarning(int code, const char *msg, ...);
183 : Q_CORE_EXPORT void qErrnoWarning(const char *msg, ...);
184 :
185 : #if QT_DEPRECATED_SINCE(5, 0)// deprecated. Use qInstallMessageHandler instead!
186 : typedef void (*QtMsgHandler)(QtMsgType, const char *);
187 : Q_CORE_EXPORT QT_DEPRECATED QtMsgHandler qInstallMsgHandler(QtMsgHandler);
188 : #endif
189 :
190 : typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &);
191 : Q_CORE_EXPORT QtMessageHandler qInstallMessageHandler(QtMessageHandler);
192 :
193 : Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern);
194 : Q_CORE_EXPORT QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context,
195 : const QString &buf);
196 :
197 : QT_END_NAMESPACE
198 : #endif // QLOGGING_H
|