Qt 4.8
qdebug.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 #include <QtCore/qcontiguouscache.h>
55 
57 
59 
60 QT_MODULE(Core)
61 
63 {
64  struct Stream {
65  Stream(QIODevice *device) : ts(device), ref(1), type(QtDebugMsg), space(true), message_output(false) {}
66  Stream(QString *string) : ts(string, QIODevice::WriteOnly), ref(1), type(QtDebugMsg), space(true), message_output(false) {}
67  Stream(QtMsgType t) : ts(&buffer, QIODevice::WriteOnly), ref(1), type(t), space(true), message_output(true) {}
70  int ref;
72  bool space;
74  } *stream;
75 public:
76  inline QDebug(QIODevice *device) : stream(new Stream(device)) {}
77  inline QDebug(QString *string) : stream(new Stream(string)) {}
78  inline QDebug(QtMsgType t) : stream(new Stream(t)) {}
79  inline QDebug(const QDebug &o):stream(o.stream) { ++stream->ref; }
80  inline QDebug &operator=(const QDebug &other);
81  inline ~QDebug() {
82  if (!--stream->ref) {
83  if(stream->message_output) {
84  QT_TRY {
85  qt_message_output(stream->type, stream->buffer.toLocal8Bit().data());
86  } QT_CATCH(std::bad_alloc&) { /* We're out of memory - give up. */ }
87  }
88  delete stream;
89  }
90  }
91  inline QDebug &space() { stream->space = true; stream->ts << ' '; return *this; }
92  inline QDebug &nospace() { stream->space = false; return *this; }
93  inline QDebug &maybeSpace() { if (stream->space) stream->ts << ' '; return *this; }
94 
95  inline QDebug &operator<<(QChar t) { stream->ts << '\'' << t << '\''; return maybeSpace(); }
96  inline QDebug &operator<<(QBool t) { stream->ts << (bool(t != 0) ? "true" : "false"); return maybeSpace(); }
97  inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
98  inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); }
99  inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); }
100  inline QDebug &operator<<(unsigned short t) { stream->ts << t; return maybeSpace(); }
101  inline QDebug &operator<<(signed int t) { stream->ts << t; return maybeSpace(); }
102  inline QDebug &operator<<(unsigned int t) { stream->ts << t; return maybeSpace(); }
103  inline QDebug &operator<<(signed long t) { stream->ts << t; return maybeSpace(); }
104  inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); }
105  inline QDebug &operator<<(qint64 t)
106  { stream->ts << QString::number(t); return maybeSpace(); }
107  inline QDebug &operator<<(quint64 t)
108  { stream->ts << QString::number(t); return maybeSpace(); }
109  inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
110  inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
111  inline QDebug &operator<<(const char* t) { stream->ts << QString::fromAscii(t); return maybeSpace(); }
112  inline QDebug &operator<<(const QString & t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); }
113  inline QDebug &operator<<(const QStringRef & t) { return operator<<(t.toString()); }
114  inline QDebug &operator<<(const QLatin1String &t) { stream->ts << '\"' << t.latin1() << '\"'; return maybeSpace(); }
115  inline QDebug &operator<<(const QByteArray & t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); }
116  inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
117  inline QDebug &operator<<(QTextStreamFunction f) {
118  stream->ts << f;
119  return *this;
120  }
121 
123  { stream->ts << m; return *this; }
124 };
125 
126 class QNoDebug
127 {
128 public:
129  inline QNoDebug(){}
130  inline QNoDebug(const QDebug &){}
131  inline ~QNoDebug(){}
132 #if !defined( QT_NO_TEXTSTREAM )
133  inline QNoDebug &operator<<(QTextStreamFunction) { return *this; }
134  inline QNoDebug &operator<<(QTextStreamManipulator) { return *this; }
135 #endif
136  inline QNoDebug &space() { return *this; }
137  inline QNoDebug &nospace() { return *this; }
138  inline QNoDebug &maybeSpace() { return *this; }
139 
140  template<typename T>
141  inline QNoDebug &operator<<(const T &) { return *this; }
142 };
143 
145 
146 inline QDebug &QDebug::operator=(const QDebug &other)
147 {
148  if (this != &other) {
149  QDebug copy(other);
150  qSwap(stream, copy.stream);
151  }
152  return *this;
153 }
154 
155 #if defined(FORCE_UREF)
156 template <class T>
157 inline QDebug &operator<<(QDebug debug, const QList<T> &list)
158 #else
159 template <class T>
160 inline QDebug operator<<(QDebug debug, const QList<T> &list)
161 #endif
162 {
163  debug.nospace() << '(';
164  for (Q_TYPENAME QList<T>::size_type i = 0; i < list.count(); ++i) {
165  if (i)
166  debug << ", ";
167  debug << list.at(i);
168  }
169  debug << ')';
170  return debug.space();
171 }
172 
173 #if defined(FORCE_UREF)
174 template <typename T>
175 inline QDebug &operator<<(QDebug debug, const QVector<T> &vec)
176 #else
177 template <typename T>
178 inline QDebug operator<<(QDebug debug, const QVector<T> &vec)
179 #endif
180 {
181  debug.nospace() << "QVector";
182  return operator<<(debug, vec.toList());
183 }
184 
185 #if defined(FORCE_UREF)
186 template <class aKey, class aT>
187 inline QDebug &operator<<(QDebug debug, const QMap<aKey, aT> &map)
188 #else
189 template <class aKey, class aT>
190 inline QDebug operator<<(QDebug debug, const QMap<aKey, aT> &map)
191 #endif
192 {
193  debug.nospace() << "QMap(";
194  for (typename QMap<aKey, aT>::const_iterator it = map.constBegin();
195  it != map.constEnd(); ++it) {
196  debug << '(' << it.key() << ", " << it.value() << ')';
197  }
198  debug << ')';
199  return debug.space();
200 }
201 
202 #if defined(FORCE_UREF)
203 template <class aKey, class aT>
204 inline QDebug &operator<<(QDebug debug, const QHash<aKey, aT> &hash)
205 #else
206 template <class aKey, class aT>
207 inline QDebug operator<<(QDebug debug, const QHash<aKey, aT> &hash)
208 #endif
209 {
210  debug.nospace() << "QHash(";
211  for (typename QHash<aKey, aT>::const_iterator it = hash.constBegin();
212  it != hash.constEnd(); ++it)
213  debug << '(' << it.key() << ", " << it.value() << ')';
214  debug << ')';
215  return debug.space();
216 }
217 
218 #if defined(FORCE_UREF)
219 template <class T1, class T2>
220 inline QDebug &operator<<(QDebug debug, const QPair<T1, T2> &pair)
221 #else
222 template <class T1, class T2>
223 inline QDebug operator<<(QDebug debug, const QPair<T1, T2> &pair)
224 #endif
225 {
226  debug.nospace() << "QPair(" << pair.first << ',' << pair.second << ')';
227  return debug.space();
228 }
229 
230 template <typename T>
231 inline QDebug operator<<(QDebug debug, const QSet<T> &set)
232 {
233  debug.nospace() << "QSet";
234  return operator<<(debug, set.toList());
235 }
236 
237 #if defined(FORCE_UREF)
238 template <class T>
239 inline QDebug &operator<<(QDebug debug, const QContiguousCache<T> &cache)
240 #else
241 template <class T>
242 inline QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)
243 #endif
244 {
245  debug.nospace() << "QContiguousCache(";
246  for (int i = cache.firstIndex(); i <= cache.lastIndex(); ++i) {
247  debug << cache[i];
248  if (i != cache.lastIndex())
249  debug << ", ";
250  }
251  debug << ')';
252  return debug.space();
253 }
254 
255 #if defined(FORCE_UREF)
256 template <class T>
257 inline QDebug &operator<<(QDebug debug, const QFlags<T> &flags)
258 #else
259 template <class T>
260 inline QDebug operator<<(QDebug debug, const QFlags<T> &flags)
261 #endif
262 {
263  debug.nospace() << "QFlags(";
264  bool needSeparator = false;
265  for (uint i = 0; i < sizeof(T) * 8; ++i) {
266  if (flags.testFlag(T(1 << i))) {
267  if (needSeparator)
268  debug.nospace() << '|';
269  else
270  needSeparator = true;
271  debug.nospace() << "0x" << QByteArray::number(T(1 << i), 16).constData();
272  }
273  }
274  debug << ')';
275  return debug.space();
276 }
277 
278 #if !defined(QT_NO_DEBUG_STREAM)
280 
281 #else // QT_NO_DEBUG_STREAM
282 #undef qDebug
283 inline QNoDebug qDebug() { return QNoDebug(); }
284 #define qDebug QT_NO_QDEBUG_MACRO
285 
286 #endif
287 
288 #if !defined(QT_NO_WARNING_OUTPUT)
290 #else
291 #undef qWarning
292 inline QNoDebug qWarning() { return QNoDebug(); }
293 #define qWarning QT_NO_QWARNING_MACRO
294 #endif
295 
297 
299 
300 #endif // QDEBUG_H
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
static uint hash(const uchar *p, int n)
Definition: qhash.cpp:68
QString toString() const
Returns a copy of the string reference as a QString object.
Definition: qstring.cpp:8653
#define Q_CORE_EXPORT_INLINE
Definition: qglobal.h:1493
The QContiguousCache class is a template class that provides a contiguous cache.
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QList< T > toList() const
Returns a QList object with the data contained in this QVector.
Definition: qvector.h:770
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
#define QT_MODULE(x)
Definition: qglobal.h:2783
QNoDebug & nospace()
Definition: qdebug.h:137
QNoDebug()
Definition: qdebug.h:129
QDebug(QString *string)
Constructs a debug stream that writes to the given string.
Definition: qdebug.h:77
QDebug & operator=(const QDebug &other)
Assigns the other debug stream to this stream and returns a reference to this stream.
Definition: qdebug.h:146
QDebug & operator<<(signed long t)
Writes the signed long integer, l, to the stream and returns a reference to the stream.
Definition: qdebug.h:103
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define it(className, varName)
QDebug operator<<(QDebug debug, const QList< T > &list)
Definition: qdebug.h:160
Stream(QIODevice *device)
Definition: qdebug.h:65
QDebug & operator<<(const QStringRef &t)
Writes the string reference, s, to the stream and returns a reference to the stream.
Definition: qdebug.h:113
QNoDebug & space()
Definition: qdebug.h:136
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QDebug & operator<<(const char *t)
Writes the &#39;\0&#39;-terminated string, s, to the stream and returns a reference to the stream...
Definition: qdebug.h:111
T1 first
Definition: qpair.h:65
~QDebug()
Flushes any pending data to be written and destroys the debug stream.
Definition: qdebug.h:81
T2 second
Definition: qpair.h:66
QDebug(QtMsgType t)
Constructs a debug stream that writes to the handler for the message type specified by type...
Definition: qdebug.h:78
bool testFlag(Enum f) const
Returns true if the flag is set, otherwise false.
Definition: qglobal.h:2345
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QDebug & operator<<(double t)
Writes the 64-bit floating point number, f, to the stream and returns a reference to the stream...
Definition: qdebug.h:110
The QString class provides a Unicode character string.
Definition: qstring.h:83
QDebug & operator<<(QChar t)
Writes the character, t, to the stream and returns a reference to the stream.
Definition: qdebug.h:95
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
static QList< QVariant > toList(char **buf, int count, T *=0)
Definition: qsql_ibase.cpp:472
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
QtMsgType
This enum describes the messages that can be sent to a message handler (QtMsgHandler).
Definition: qglobal.h:1881
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
Q_CORE_EXPORT_INLINE QDebug qWarning()
Definition: qdebug.h:289
QDebug & operator<<(const void *t)
Writes a pointer, p, to the stream and returns a reference to the stream.
Definition: qdebug.h:116
int size_type
Typedef for int.
Definition: qlist.h:305
QDebug(QIODevice *device)
Constructs a debug stream that writes to the given device.
Definition: qdebug.h:76
QDebug & operator<<(float t)
Writes the 32-bit floating point number, f, to the stream and returns a reference to the stream...
Definition: qdebug.h:109
Stream(QString *string)
Definition: qdebug.h:66
QNoDebug(const QDebug &)
Definition: qdebug.h:130
QString buffer
Definition: qdebug.h:69
QFuture< void > map(Sequence &sequence, MapFunction function)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QNoDebug & operator<<(QTextStreamManipulator)
Definition: qdebug.h:134
const char * latin1() const
Returns the Latin-1 string stored in this object.
Definition: qstring.h:661
static FILE * stream
QDebug & operator<<(bool t)
Writes the boolean value, t, to the stream and returns a reference to the stream. ...
Definition: qdebug.h:97
unsigned __int64 quint64
Definition: qglobal.h:943
#define Q_TYPENAME
Definition: qglobal.h:1717
QDebug & operator<<(char t)
Writes the character, t, to the stream and returns a reference to the stream.
Definition: qdebug.h:98
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QDebug & operator<<(unsigned long t)
Writes then unsigned long integer, l, to the stream and returns a reference to the stream...
Definition: qdebug.h:104
unsigned int uint
Definition: qglobal.h:996
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
bool space
Definition: qdebug.h:72
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
__int64 qint64
Definition: qglobal.h:942
QDebug & operator<<(QTextStreamFunction f)
Definition: qdebug.h:117
int firstIndex() const
Returns the first valid index in the cache.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
QDebug & operator<<(QBool t)
Writes the boolean value, t, to the stream and returns a reference to the stream. ...
Definition: qdebug.h:96
#define QT_CATCH(A)
Definition: qglobal.h:1537
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
QDebug & operator<<(signed int t)
Writes the signed integer, i, to the stream and returns a reference to the stream.
Definition: qdebug.h:101
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
QtMsgType type
Definition: qdebug.h:71
QTextStream &(* QTextStreamFunction)(QTextStream &)
Definition: qtextstream.h:270
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:466
void qt_message_output(QtMsgType msgType, const char *buf)
Definition: qglobal.cpp:2712
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
bool message_output
Definition: qdebug.h:73
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
QDebug & operator<<(qint64 t)
Writes the signed 64-bit integer, i, to the stream and returns a reference to the stream...
Definition: qdebug.h:105
QNoDebug & operator<<(const T &)
Definition: qdebug.h:141
struct QDebug::Stream * stream
Q_CORE_EXPORT_INLINE QDebug qDebug()
Definition: qdebug.h:279
QDebug(const QDebug &o)
Constructs a copy of the other debug stream.
Definition: qdebug.h:79
QDebug & operator<<(QTextStreamManipulator m)
Definition: qdebug.h:122
QDebug & operator<<(const QByteArray &t)
Writes the byte array, b, to the stream and returns a reference to the stream.
Definition: qdebug.h:115
QDebug & operator<<(const QString &t)
Writes the string, s, to the stream and returns a reference to the stream.
Definition: qdebug.h:112
QDebug & operator<<(signed short t)
Writes the signed short integer, i, to the stream and returns a reference to the stream.
Definition: qdebug.h:99
Stream(QtMsgType t)
Definition: qdebug.h:67
QDebug & operator<<(unsigned short t)
Writes then unsigned short integer, i, to the stream and returns a reference to the stream...
Definition: qdebug.h:100
QNoDebug & maybeSpace()
Definition: qdebug.h:138
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
Definition: qglobal.h:2313
Q_CORE_EXPORT_INLINE QDebug qCritical()
Definition: qdebug.h:144
QDebug & operator<<(const QLatin1String &t)
Writes the Latin1-encoded string, s, to the stream and returns a reference to the stream...
Definition: qdebug.h:114
QDebug & operator<<(quint64 t)
Writes then unsigned 64-bit integer, i, to the stream and returns a reference to the stream...
Definition: qdebug.h:107
#define class
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
#define QT_END_HEADER
Definition: qglobal.h:137
static QByteArray number(int, int base=10)
Returns a byte array containing the string equivalent of the number n to base base (10 by default)...
#define QT_TRY
Definition: qglobal.h:1536
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
QNoDebug & operator<<(QTextStreamFunction)
Definition: qdebug.h:133
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
int lastIndex() const
Returns the last valid index in the cache.
QTextStream ts
Definition: qdebug.h:68
QDebug & operator<<(unsigned int t)
Writes then unsigned integer, i, to the stream and returns a reference to the stream.
Definition: qdebug.h:102
~QNoDebug()
Definition: qdebug.h:131