source: trunk/src/corelib/io/qdebug.h@ 477

Last change on this file since 477 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 9.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QDEBUG_H
43#define QDEBUG_H
44
45#include <QtCore/qalgorithms.h>
46#include <QtCore/qhash.h>
47#include <QtCore/qlist.h>
48#include <QtCore/qmap.h>
49#include <QtCore/qpair.h>
50#include <QtCore/qtextstream.h>
51#include <QtCore/qstring.h>
52#include <QtCore/qvector.h>
53#include <QtCore/qset.h>
54
55QT_BEGIN_HEADER
56
57QT_BEGIN_NAMESPACE
58
59QT_MODULE(Core)
60
61class Q_CORE_EXPORT QDebug
62{
63 struct Stream {
64 Stream(QIODevice *device) : ts(device), ref(1), type(QtDebugMsg), space(true), message_output(false) {}
65 Stream(QString *string) : ts(string, QIODevice::WriteOnly), ref(1), type(QtDebugMsg), space(true), message_output(false) {}
66 Stream(QtMsgType t) : ts(&buffer, QIODevice::WriteOnly), ref(1), type(t), space(true), message_output(true) {}
67 QTextStream ts;
68 QString buffer;
69 int ref;
70 QtMsgType type;
71 bool space;
72 bool message_output;
73 } *stream;
74public:
75 inline QDebug(QIODevice *device) : stream(new Stream(device)) {}
76 inline QDebug(QString *string) : stream(new Stream(string)) {}
77 inline QDebug(QtMsgType t) : stream(new Stream(t)) {}
78 inline QDebug(const QDebug &o):stream(o.stream) { ++stream->ref; }
79 inline QDebug &operator=(const QDebug &other);
80 inline ~QDebug() {
81 if (!--stream->ref) {
82 if(stream->message_output)
83 qt_message_output(stream->type, stream->buffer.toLocal8Bit().data());
84 delete stream;
85 }
86 }
87 inline QDebug &space() { stream->space = true; stream->ts << " "; return *this; }
88 inline QDebug &nospace() { stream->space = false; return *this; }
89 inline QDebug &maybeSpace() { if (stream->space) stream->ts << " "; return *this; }
90
91 inline QDebug &operator<<(QChar t) { stream->ts << "\'" << t << "\'"; return maybeSpace(); }
92 inline QDebug &operator<<(QBool t) { stream->ts << (bool(t) ? "true" : "false"); return maybeSpace(); }
93 inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
94 inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); }
95 inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); }
96 inline QDebug &operator<<(unsigned short t) { stream->ts << t; return maybeSpace(); }
97 inline QDebug &operator<<(signed int t) { stream->ts << t; return maybeSpace(); }
98 inline QDebug &operator<<(unsigned int t) { stream->ts << t; return maybeSpace(); }
99 inline QDebug &operator<<(signed long t) { stream->ts << t; return maybeSpace(); }
100 inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); }
101 inline QDebug &operator<<(qint64 t)
102 { stream->ts << QString::number(t); return maybeSpace(); }
103 inline QDebug &operator<<(quint64 t)
104 { stream->ts << QString::number(t); return maybeSpace(); }
105 inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
106 inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
107 inline QDebug &operator<<(const char* t) { stream->ts << QString::fromAscii(t); return maybeSpace(); }
108 inline QDebug &operator<<(const QString & t) { stream->ts << "\"" << t << "\""; return maybeSpace(); }
109 inline QDebug &operator<<(const QStringRef & t) { return operator<<(t.toString()); }
110 inline QDebug &operator<<(const QLatin1String &t) { stream->ts << "\"" << t.latin1() << "\""; return maybeSpace(); }
111 inline QDebug &operator<<(const QByteArray & t) { stream->ts << "\"" << t << "\""; return maybeSpace(); }
112 inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
113 inline QDebug &operator<<(QTextStreamFunction f) {
114 stream->ts << f;
115 return *this;
116 }
117
118 inline QDebug &operator<<(QTextStreamManipulator m)
119 { stream->ts << m; return *this; }
120};
121
122class QNoDebug
123{
124public:
125 inline QNoDebug(){}
126 inline QNoDebug(const QDebug &){}
127 inline ~QNoDebug(){}
128#if !defined( QT_NO_TEXTSTREAM )
129 inline QNoDebug &operator<<(QTextStreamFunction) { return *this; }
130 inline QNoDebug &operator<<(QTextStreamManipulator) { return *this; }
131#endif
132 inline QNoDebug &space() { return *this; }
133 inline QNoDebug &nospace() { return *this; }
134 inline QNoDebug &maybeSpace() { return *this; }
135
136#ifndef QT_NO_MEMBER_TEMPLATES
137 template<typename T>
138 inline QNoDebug &operator<<(const T &) { return *this; }
139#endif
140};
141
142Q_CORE_EXPORT_INLINE QDebug qCritical() { return QDebug(QtCriticalMsg); }
143
144inline QDebug &QDebug::operator=(const QDebug &other)
145{
146 if (this != &other) {
147 QDebug copy(other);
148 qSwap(stream, copy.stream);
149 }
150 return *this;
151}
152
153#if defined(FORCE_UREF)
154template <class T>
155inline QDebug &operator<<(QDebug debug, const QList<T> &list)
156#else
157template <class T>
158inline QDebug operator<<(QDebug debug, const QList<T> &list)
159#endif
160{
161 debug.nospace() << "(";
162 for (Q_TYPENAME QList<T>::size_type i = 0; i < list.count(); ++i) {
163 if (i)
164 debug << ", ";
165 debug << list.at(i);
166 }
167 debug << ")";
168 return debug.space();
169}
170
171#if defined(FORCE_UREF)
172template <typename T>
173inline QDebug &operator<<(QDebug debug, const QVector<T> &vec)
174#else
175template <typename T>
176inline QDebug operator<<(QDebug debug, const QVector<T> &vec)
177#endif
178{
179 debug.nospace() << "QVector";
180 return operator<<(debug, vec.toList());
181}
182
183#if defined(FORCE_UREF)
184template <class aKey, class aT>
185inline QDebug &operator<<(QDebug debug, const QMap<aKey, aT> &map)
186#else
187template <class aKey, class aT>
188inline QDebug operator<<(QDebug debug, const QMap<aKey, aT> &map)
189#endif
190{
191 debug.nospace() << "QMap(";
192 for (typename QMap<aKey, aT>::const_iterator it = map.constBegin();
193 it != map.constEnd(); ++it) {
194 debug << "(" << it.key() << ", " << it.value() << ")";
195 }
196 debug << ")";
197 return debug.space();
198}
199
200#if defined(FORCE_UREF)
201template <class aKey, class aT>
202inline QDebug &operator<<(QDebug debug, const QHash<aKey, aT> &hash)
203#else
204template <class aKey, class aT>
205inline QDebug operator<<(QDebug debug, const QHash<aKey, aT> &hash)
206#endif
207{
208 debug.nospace() << "QHash(";
209 for (typename QHash<aKey, aT>::const_iterator it = hash.constBegin();
210 it != hash.constEnd(); ++it)
211 debug << "(" << it.key() << ", " << it.value() << ")";
212 debug << ")";
213 return debug.space();
214}
215
216#if defined(FORCE_UREF)
217template <class T1, class T2>
218inline QDebug &operator<<(QDebug debug, const QPair<T1, T2> &pair)
219#else
220template <class T1, class T2>
221inline QDebug operator<<(QDebug debug, const QPair<T1, T2> &pair)
222#endif
223{
224 debug.nospace() << "QPair(" << pair.first << "," << pair.second << ")";
225 return debug.space();
226}
227
228template <typename T>
229inline QDebug operator<<(QDebug debug, const QSet<T> &set)
230{
231 debug.nospace() << "QSet";
232 return operator<<(debug, set.toList());
233}
234
235#if !defined(QT_NO_DEBUG_STREAM)
236Q_CORE_EXPORT_INLINE QDebug qDebug() { return QDebug(QtDebugMsg); }
237
238#else // QT_NO_DEBUG_STREAM
239#undef qDebug
240inline QNoDebug qDebug() { return QNoDebug(); }
241#define qDebug QT_NO_QDEBUG_MACRO
242
243#ifdef QT_NO_MEMBER_TEMPLATES
244template<typename T>
245inline QNoDebug operator<<(QNoDebug debug, const T &) { return debug; }
246#endif
247
248#endif
249
250#if !defined(QT_NO_WARNING_OUTPUT)
251Q_CORE_EXPORT_INLINE QDebug qWarning() { return QDebug(QtWarningMsg); }
252#else
253#undef qWarning
254inline QNoDebug qWarning() { return QNoDebug(); }
255#define qWarning QT_NO_QWARNING_MACRO
256#endif
257
258QT_END_NAMESPACE
259
260QT_END_HEADER
261
262#endif // QDEBUG_H
Note: See TracBrowser for help on using the repository browser.