src/corelib/io/qdebug.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the QtCore module of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #ifndef QDEBUG_H
00025 #define QDEBUG_H
00026 
00027 #include <QtCore/qalgorithms.h>
00028 #include <QtCore/qhash.h>
00029 #include <QtCore/qlist.h>
00030 #include <QtCore/qmap.h>
00031 #include <QtCore/qpair.h>
00032 #include <QtCore/qtextstream.h>
00033 #include <QtCore/qstring.h>
00034 #include <QtCore/qvector.h>
00035 #include <QtCore/qset.h>
00036 
00037 QT_BEGIN_HEADER
00038 
00039 QT_MODULE(Core)
00040 
00041 #if !defined(QT_NO_DEBUG_STREAM)
00042 class Q_CORE_EXPORT QDebug
00043 {
00044     struct Stream {
00045         Stream(QIODevice *device) : ts(device), ref(1), type(QtDebugMsg), space(true), message_output(false) {}
00046         Stream(QString *string) : ts(string, QIODevice::WriteOnly), ref(1), type(QtDebugMsg), space(true), message_output(false) {}
00047         Stream(QtMsgType t) : ts(&buffer, QIODevice::WriteOnly), ref(1), type(t), space(true), message_output(true) {}
00048         QTextStream ts;
00049         QString buffer;
00050         int ref;
00051         QtMsgType type;
00052         bool space;
00053         bool message_output;
00054     } *stream;
00055 public:
00056     inline QDebug(QIODevice *device) : stream(new Stream(device)) {}
00057     inline QDebug(QString *string) : stream(new Stream(string)) {}
00058     inline QDebug(QtMsgType t) : stream(new Stream(t)) {}
00059     inline QDebug(const QDebug &o):stream(o.stream) { ++stream->ref; }
00060     inline QDebug &operator=(const QDebug &other);
00061     inline ~QDebug() {
00062         if (!--stream->ref) {
00063             if(stream->message_output)
00064                 qt_message_output(stream->type, stream->buffer.toLocal8Bit().data());
00065             delete stream;
00066         }
00067     }
00068     inline QDebug &space() { stream->space = true; stream->ts << " "; return *this; }
00069     inline QDebug &nospace() { stream->space = false; return *this; }
00070     inline QDebug &maybeSpace() { if (stream->space) stream->ts << " "; return *this; }
00071 
00072     inline QDebug &operator<<(QChar t) { stream->ts << "\'" << t << "\'"; return maybeSpace(); }
00073     inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
00074     inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); }
00075     inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); }
00076     inline QDebug &operator<<(unsigned short t) { stream->ts << t; return maybeSpace(); }
00077     inline QDebug &operator<<(signed int t) { stream->ts << t; return maybeSpace(); }
00078     inline QDebug &operator<<(unsigned int t) { stream->ts << t; return maybeSpace(); }
00079     inline QDebug &operator<<(signed long t) { stream->ts << t; return maybeSpace(); }
00080     inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); }
00081     inline QDebug &operator<<(qint64 t)
00082         { stream->ts << QString::number(t); return maybeSpace(); }
00083     inline QDebug &operator<<(quint64 t)
00084         { stream->ts << QString::number(t); return maybeSpace(); }
00085     inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
00086     inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
00087     inline QDebug &operator<<(const char* t) { stream->ts << QString::fromAscii(t); return maybeSpace(); }
00088     inline QDebug &operator<<(const QString & t) { stream->ts << "\"" << t  << "\""; return maybeSpace(); }
00089     inline QDebug &operator<<(const QLatin1String &t) { stream->ts << "\""  << t.latin1() << "\""; return maybeSpace(); }
00090     inline QDebug &operator<<(const QByteArray & t) { stream->ts  << "\"" << t << "\""; return maybeSpace(); }
00091     inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
00092     inline QDebug &operator<<(QTextStreamFunction f) {
00093         stream->ts << f;
00094         return *this;
00095     }
00096 
00097     inline QDebug &operator<<(QTextStreamManipulator m)
00098     { stream->ts << m; return *this; }
00099 };
00100 
00101 inline QDebug &QDebug::operator=(const QDebug &other)
00102 {
00103     if (this != &other) {
00104         QDebug copy(other);
00105         qSwap(stream, copy.stream);
00106     }
00107     return *this;
00108 }
00109 
00110 #if defined(FORCE_UREF)
00111 template <class T>
00112 inline QDebug &operator<<(QDebug debug, const QList<T> &list)
00113 #else
00114 template <class T>
00115 inline QDebug operator<<(QDebug debug, const QList<T> &list)
00116 #endif
00117 {
00118     debug.nospace() << "(";
00119     for (Q_TYPENAME QList<T>::size_type i = 0; i < list.count(); ++i) {
00120         if (i)
00121             debug << ", ";
00122         debug << list.at(i);
00123     }
00124     debug << ")";
00125     return debug.space();
00126 }
00127 
00128 #if defined(FORCE_UREF)
00129 template <typename T>
00130 inline QDebug &operator<<(QDebug debug, const QVector<T> &vec)
00131 #else
00132 template <typename T>
00133 inline QDebug operator<<(QDebug debug, const QVector<T> &vec)
00134 #endif
00135 {
00136     debug.nospace() << "QVector";
00137     return operator<<(debug, vec.toList());
00138 }
00139 
00140 #if defined(FORCE_UREF)
00141 template <class aKey, class aT>
00142 inline QDebug &operator<<(QDebug debug, const QMap<aKey, aT> &map)
00143 #else
00144 template <class aKey, class aT>
00145 inline QDebug operator<<(QDebug debug, const QMap<aKey, aT> &map)
00146 #endif
00147 {
00148     debug.nospace() << "QMap(";
00149     for (typename QMap<aKey, aT>::const_iterator it = map.constBegin();
00150          it != map.constEnd(); ++it) {
00151         debug << "(" << it.key() << ", " << it.value() << ")";
00152     }
00153     debug << ")";
00154     return debug.space();
00155 }
00156 
00157 #if defined(FORCE_UREF)
00158 template <class aKey, class aT>
00159 inline QDebug &operator<<(QDebug debug, const QHash<aKey, aT> &hash)
00160 #else
00161 template <class aKey, class aT>
00162 inline QDebug operator<<(QDebug debug, const QHash<aKey, aT> &hash)
00163 #endif
00164 {
00165     debug.nospace() << "QHash(";
00166     for (typename QHash<aKey, aT>::const_iterator it = hash.constBegin();
00167             it != hash.constEnd(); ++it)
00168         debug << "(" << it.key() << ", " << it.value() << ")";
00169     debug << ")";
00170     return debug.space();
00171 }
00172 
00173 #if defined(FORCE_UREF)
00174 template <class T1, class T2>
00175 inline QDebug &operator<<(QDebug debug, const QPair<T1, T2> &pair)
00176 #else
00177 template <class T1, class T2>
00178 inline QDebug operator<<(QDebug debug, const QPair<T1, T2> &pair)
00179 #endif
00180 {
00181     debug.nospace() << "QPair(" << pair.first << "," << pair.second << ")";
00182     return debug.space();
00183 }
00184 
00185 template <typename T>
00186 inline QDebug operator<<(QDebug debug, const QSet<T> &set)
00187 {
00188     debug.nospace() << "QSet";
00189     return operator<<(debug, set.toList());
00190 }
00191 
00192 Q_CORE_EXPORT_INLINE QDebug qDebug() { return QDebug(QtDebugMsg); }
00193 Q_CORE_EXPORT_INLINE QDebug qWarning() { return QDebug(QtWarningMsg); }
00194 Q_CORE_EXPORT_INLINE QDebug qCritical() { return QDebug(QtCriticalMsg); }
00195 
00196 #else // QT_NO_DEBUG_STREAM
00197 
00198 class QNoDebug
00199 {
00200 public:
00201     inline QNoDebug(){}
00202     inline QNoDebug(const QDebug &){}
00203     inline ~QNoDebug(){}
00204 #if !defined( QT_NO_TEXTSTREAM )
00205     inline QNoDebug &operator<<(QTextStreamFunction) { return *this; }
00206     inline QNoDebug &operator<<(QTextStreamManipulator) { return *this; }
00207 #endif
00208     inline QNoDebug &space() { return *this; }
00209     inline QNoDebug &nospace() { return *this; }
00210     inline QNoDebug &maybeSpace() { return *this; }
00211 
00212 #ifndef QT_NO_MEMBER_TEMPLATES
00213     template<typename T>
00214     inline QNoDebug &operator<<(const T &) { return *this; }
00215 #endif
00216 };
00217 #undef qDebug
00218 inline QNoDebug qDebug() { return QNoDebug(); }
00219 #define qDebug if(1) ; else qDebug
00220 
00221 #ifdef QT_NO_MEMBER_TEMPLATES
00222 template<typename T>
00223 inline QNoDebug operator<<(QNoDebug debug, const T &) { return debug; }
00224 #endif
00225 
00226 #endif
00227 
00228 QT_END_HEADER
00229 
00230 #endif // QDEBUG_H

Generated on Thu Mar 15 11:53:12 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1