1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QLOGGING_H
5#define QLOGGING_H
6
7#include <QtCore/qtclasshelpermacros.h>
8#include <QtCore/qtconfigmacros.h>
9#include <QtCore/qtcoreexports.h>
10#include <QtCore/qcontainerfwd.h>
11
12#if 0
13// header is automatically included in qglobal.h
14#pragma qt_no_master_include
15#pragma qt_class(QtLogging)
16#endif
17
18QT_BEGIN_NAMESPACE
19
20/*
21 Forward declarations only.
22
23 In order to use the qDebug() stream, you must #include<QDebug>
24*/
25class QDebug;
26class QNoDebug;
27
28
29enum QtMsgType {
30 QtDebugMsg,
31 QT7_ONLY(QtInfoMsg,)
32 QtWarningMsg,
33 QtCriticalMsg,
34 QtFatalMsg,
35 QT6_ONLY(QtInfoMsg,)
36#if QT_DEPRECATED_SINCE(6, 7)
37 QtSystemMsg Q_DECL_ENUMERATOR_DEPRECATED_X("Use QtCriticalMsg instead.") = QtCriticalMsg
38#endif
39};
40
41class QMessageLogContext
42{
43 Q_DISABLE_COPY(QMessageLogContext)
44public:
45 constexpr QMessageLogContext() noexcept = default;
46 constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept
47 : line(lineNumber), file(fileName), function(functionName), category(categoryName) {}
48
49 int version = 2;
50 int line = 0;
51 const char *file = nullptr;
52 const char *function = nullptr;
53 const char *category = nullptr;
54
55private:
56 QMessageLogContext &copyContextFrom(const QMessageLogContext &logContext) noexcept;
57
58 friend class QMessageLogger;
59 friend class QDebug;
60};
61
62class QLoggingCategory;
63
64#if defined(Q_CC_MSVC_ONLY)
65# define QT_MESSAGE_LOGGER_NORETURN
66#else
67# define QT_MESSAGE_LOGGER_NORETURN Q_NORETURN
68#endif
69
70class Q_CORE_EXPORT QMessageLogger
71{
72 Q_DISABLE_COPY(QMessageLogger)
73public:
74 constexpr QMessageLogger() : context() {}
75 constexpr QMessageLogger(const char *file, int line, const char *function)
76 : context(file, line, function, "default") {}
77 constexpr QMessageLogger(const char *file, int line, const char *function, const char *category)
78 : context(file, line, function, category) {}
79
80 void debug(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
81 void noDebug(const char *, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3)
82 {}
83 void info(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
84 Q_DECL_COLD_FUNCTION
85 void warning(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
86 Q_DECL_COLD_FUNCTION
87 void critical(const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
88 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
89 void fatal(const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
90
91 typedef const QLoggingCategory &(*CategoryFunction)();
92
93 void debug(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
94 void debug(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
95 void info(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
96 void info(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
97 Q_DECL_COLD_FUNCTION
98 void warning(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
99 Q_DECL_COLD_FUNCTION
100 void warning(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
101 Q_DECL_COLD_FUNCTION
102 void critical(const QLoggingCategory &cat, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
103 Q_DECL_COLD_FUNCTION
104 void critical(CategoryFunction catFunc, const char *msg, ...) const Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
105 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
106 void fatal(const QLoggingCategory &cat, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
107 QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION
108 void fatal(CategoryFunction catFunc, const char *msg, ...) const noexcept Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
109
110#ifndef QT_NO_DEBUG_STREAM
111 QDebug debug() const;
112 QDebug debug(const QLoggingCategory &cat) const;
113 QDebug debug(CategoryFunction catFunc) const;
114 QDebug info() const;
115 QDebug info(const QLoggingCategory &cat) const;
116 QDebug info(CategoryFunction catFunc) const;
117 Q_DECL_COLD_FUNCTION
118 QDebug warning() const;
119 Q_DECL_COLD_FUNCTION
120 QDebug warning(const QLoggingCategory &cat) const;
121 Q_DECL_COLD_FUNCTION
122 QDebug warning(CategoryFunction catFunc) const;
123 Q_DECL_COLD_FUNCTION
124 QDebug critical() const;
125 Q_DECL_COLD_FUNCTION
126 QDebug critical(const QLoggingCategory &cat) const;
127 Q_DECL_COLD_FUNCTION
128 QDebug critical(CategoryFunction catFunc) const;
129 Q_DECL_COLD_FUNCTION
130 QDebug fatal() const;
131 Q_DECL_COLD_FUNCTION
132 QDebug fatal(const QLoggingCategory &cat) const;
133 Q_DECL_COLD_FUNCTION
134 QDebug fatal(CategoryFunction catFunc) const;
135
136 QNoDebug noDebug() const noexcept;
137#endif // QT_NO_DEBUG_STREAM
138
139private:
140 QMessageLogContext context;
141};
142
143#undef QT_MESSAGE_LOGGER_NORETURN
144
145#if !defined(QT_MESSAGELOGCONTEXT) && !defined(QT_NO_MESSAGELOGCONTEXT)
146# if defined(QT_NO_DEBUG)
147# define QT_NO_MESSAGELOGCONTEXT
148# else
149# define QT_MESSAGELOGCONTEXT
150# endif
151#endif
152
153#ifdef QT_MESSAGELOGCONTEXT
154 #define QT_MESSAGELOG_FILE static_cast<const char *>(__FILE__)
155 #define QT_MESSAGELOG_LINE __LINE__
156 #define QT_MESSAGELOG_FUNC static_cast<const char *>(Q_FUNC_INFO)
157#else
158 #define QT_MESSAGELOG_FILE nullptr
159 #define QT_MESSAGELOG_LINE 0
160 #define QT_MESSAGELOG_FUNC nullptr
161#endif
162
163#define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
164#define qInfo QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).info
165#define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
166#define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical
167#define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal
168
169#define QT_NO_QDEBUG_MACRO while (false) QMessageLogger().noDebug
170
171#if defined(QT_NO_DEBUG_OUTPUT)
172# undef qDebug
173# define qDebug QT_NO_QDEBUG_MACRO
174#endif
175#if defined(QT_NO_INFO_OUTPUT)
176# undef qInfo
177# define qInfo QT_NO_QDEBUG_MACRO
178#endif
179#if defined(QT_NO_WARNING_OUTPUT)
180# undef qWarning
181# define qWarning QT_NO_QDEBUG_MACRO
182#endif
183
184Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context,
185 const QString &message);
186
187Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(int code, const char *msg, ...);
188Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(const char *msg, ...);
189
190typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &);
191Q_CORE_EXPORT QtMessageHandler qInstallMessageHandler(QtMessageHandler);
192
193Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern);
194Q_CORE_EXPORT QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context,
195 const QString &buf);
196
197Q_DECL_COLD_FUNCTION
198Q_CORE_EXPORT QString qt_error_string(int errorCode = -1);
199
200QT_END_NAMESPACE
201#endif // QLOGGING_H
202