Reshuffle code between qglobal, qlogging
[profile/ivi/qtbase.git] / src / corelib / global / qlogging.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtCore/qglobal.h>
43
44 #ifndef QLOGGING_H
45 #define QLOGGING_H
46
47 #if 0
48 // header is automatically included in qglobal.h
49 #pragma qt_no_master_include
50 #endif
51
52 QT_BEGIN_HEADER
53 QT_BEGIN_NAMESPACE
54
55 /*
56   Forward declarations only.
57
58   In order to use the qDebug() stream, you must #include<QDebug>
59 */
60 class QDebug;
61 class QNoDebug;
62
63 enum QtMsgType { QtDebugMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg, QtSystemMsg = QtCriticalMsg };
64
65 class QMessageLogContext
66 {
67     Q_DISABLE_COPY(QMessageLogContext)
68 public:
69     QMessageLogContext() : version(1), line(0), file(0), function(0) {}
70     Q_DECL_CONSTEXPR QMessageLogContext(const char *fileName, int lineNumber,
71                                         const char *functionName)
72         : version(1), line(lineNumber), file(fileName), function(functionName) {}
73
74     int version;
75     int line;
76     const char *file;
77     const char *function;
78
79 private:
80     friend class QMessageLogger;
81     friend class QDebug;
82 };
83
84 class Q_CORE_EXPORT QMessageLogger
85 {
86     Q_DISABLE_COPY(QMessageLogger)
87 public:
88     QMessageLogger() : context() {}
89     Q_DECL_CONSTEXPR QMessageLogger(const char *file, int line, const char *function)
90         : context(file, line, function) {}
91
92     void debug(const char *msg, ...)
93 #if defined(Q_CC_GNU) && !defined(__INSURE__)
94     __attribute__ ((format (printf, 2, 3)))
95 #endif
96     ;
97     void noDebug(const char *, ...)
98 #if defined(Q_CC_GNU) && !defined(__INSURE__)
99     __attribute__ ((format (printf, 2, 3)))
100 #endif
101     {}
102     void warning(const char *msg, ...)
103 #if defined(Q_CC_GNU) && !defined(__INSURE__)
104     __attribute__ ((format (printf, 2, 3)))
105 #endif
106     ;
107     void critical(const char *msg, ...)
108 #if defined(Q_CC_GNU) && !defined(__INSURE__)
109     __attribute__ ((format (printf, 2, 3)))
110 #endif
111     ;
112     void fatal(const char *msg, ...)
113 #if defined(Q_CC_GNU) && !defined(__INSURE__)
114     __attribute__ ((format (printf, 2, 3)))
115 #endif
116     ;
117
118 #ifndef QT_NO_DEBUG_STREAM
119     QDebug debug();
120     QDebug warning();
121     QDebug critical();
122
123     QNoDebug noDebug();
124 #endif // QT_NO_DEBUG_STREAM
125
126 private:
127     QMessageLogContext context;
128 };
129
130 /*
131   qDebug, qWarning, qCritical, qFatal are redefined to automatically include context information
132  */
133 #define qDebug QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug
134 #define qWarning QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).warning
135 #define qCritical QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).critical
136 #define qFatal QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).fatal
137
138 #define QT_NO_QDEBUG_MACRO while (false) QMessageLogger().noDebug
139 #define QT_NO_QWARNING_MACRO while (false) QMessageLogger().noDebug
140
141 #if defined(QT_NO_DEBUG_OUTPUT)
142 #  undef qDebug
143 #  define qDebug QT_NO_QDEBUG_MACRO
144 #endif
145 #if defined(QT_NO_WARNING_OUTPUT)
146 #  undef qWarning
147 #  define qWarning QT_NO_QWARNING_MACRO
148 #endif
149
150 Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context, const char *buf);
151
152 Q_CORE_EXPORT void qErrnoWarning(int code, const char *msg, ...);
153 Q_CORE_EXPORT void qErrnoWarning(const char *msg, ...);
154
155 // deprecated. Use qInstallMessageHandler instead!
156 typedef void (*QtMsgHandler)(QtMsgType, const char *);
157 Q_CORE_EXPORT QtMsgHandler qInstallMsgHandler(QtMsgHandler);
158
159 typedef void (*QMessageHandler)(QtMsgType, const QMessageLogContext &, const char *);
160 Q_CORE_EXPORT QMessageHandler qInstallMessageHandler(QMessageHandler);
161
162 QT_END_HEADER
163 QT_END_NAMESPACE
164
165 #endif // QLOGGING_H