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