- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# qdebug.h

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 **
    00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    00004 ** All rights reserved.
    00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
    00006 **
    00007 ** This file is part of the QtCore module of the Qt Toolkit.
    00008 **
    00009 ** $QT_BEGIN_LICENSE:LGPL$
    00010 ** Commercial Usage
    00011 ** Licensees holding valid Qt Commercial licenses may use this file in
    00012 ** accordance with the Qt Commercial License Agreement provided with the
    00013 ** Software or, alternatively, in accordance with the terms contained in
    00014 ** a written agreement between you and Nokia.
    00015 **
    00016 ** GNU Lesser General Public License Usage
    00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
    00018 ** General Public License version 2.1 as published by the Free Software
    00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
    00020 ** packaging of this file.  Please review the following information to
    00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
    00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    00023 **
    00024 ** In addition, as a special exception, Nokia gives you certain additional
    00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
    00027 **
    00028 ** GNU General Public License Usage
    00029 ** Alternatively, this file may be used under the terms of the GNU
    00030 ** General Public License version 3.0 as published by the Free Software
    00031 ** Foundation and appearing in the file LICENSE.GPL included in the
    00032 ** packaging of this file.  Please review the following information to
    00033 ** ensure the GNU General Public License version 3.0 requirements will be
    00034 ** met: http://www.gnu.org/copyleft/gpl.html.
    00035 **
    00036 ** If you have questions regarding the use of this file, please contact
    00037 ** Nokia at qt-info@nokia.com.
    00038 ** $QT_END_LICENSE$
    00039 **
    00040 ****************************************************************************/
    00041
    00042 #ifndef QDEBUG_H
    00043 #define QDEBUG_H
    00044
    00045 #include <QtCore/qalgorithms.h>
    00046 #include <QtCore/qhash.h>
    00047 #include <QtCore/qlist.h>
    00048 #include <QtCore/qmap.h>
    00049 #include <QtCore/qpair.h>
    00050 #include <QtCore/qtextstream.h>
    00051 #include <QtCore/qstring.h>
    00052 #include <QtCore/qvector.h>
    00053 #include <QtCore/qset.h>
    00054 #include <QtCore/qcontiguouscache.h>
    00055
    00056 QT_BEGIN_HEADER
    00057
    00058 QT_BEGIN_NAMESPACE
    00059
    00060 QT_MODULE(Core)
    00061
    00062 class Q_CORE_EXPORT QDebug
    00063 {
    00064     struct Stream {
    00065         Stream(QIODevice *device) : ts(device), ref(1), type(QtDebugMsg), space(true), message_output(false) {}
    00066         Stream(QString *string) : ts(string, QIODevice::WriteOnly), ref(1), type(QtDebugMsg), space(true), message_output(false) {}
    00067         Stream(QtMsgType t) : ts(&buffer, QIODevice::WriteOnly), ref(1), type(t), space(true), message_output(true) {}
    00068         QTextStream ts;
    00069         QString buffer;
    00070         int ref;
    00071         QtMsgType type;
    00072         bool space;
    00073         bool message_output;
    00074     } *stream;
    00075 public:
    00076     inline QDebug(QIODevice *device) : stream(new Stream(device)) {}
    00077     inline QDebug(QString *string) : stream(new Stream(string)) {}
    00078     inline QDebug(QtMsgType t) : stream(new Stream(t)) {}
    00079     inline QDebug(const QDebug &o):stream(o.stream) { ++stream->ref; }
    00080     inline QDebug &operator=(const QDebug &other);
    00081     inline ~QDebug() {
    00082         if (!--stream->ref) {
    00083             if(stream->message_output) {
    00084                 QT_TRY {
    00085                     qt_message_output(stream->type, stream->buffer.toLocal8Bit().data());
    00086                 } QT_CATCH(std::bad_alloc&) { /* We're out of memory - give up. */ }
    00087             }
    00088             delete stream;
    00089         }
    00090     }
    00091     inline QDebug &space() { stream->space = true; stream->ts << ' '; return *this; }
    00092     inline QDebug &nospace() { stream->space = false; return *this; }
    00093     inline QDebug &maybeSpace() { if (stream->space) stream->ts << ' '; return *this; }
    00094
    00095     inline QDebug &operator<<(QChar t) { stream->ts << '\'' << t << '\''; return maybeSpace(); }
    00096     inline QDebug &operator<<(QBool t) { stream->ts << (bool(t != 0) ? "true" : "false"); return maybeSpace(); }
    00097     inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
    00098     inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); }
    00099     inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); }
    00100     inline QDebug &operator<<(unsigned short t) { stream->ts << t; return maybeSpace(); }
    00101     inline QDebug &operator<<(signed int t) { stream->ts << t; return maybeSpace(); }
    00102     inline QDebug &operator<<(unsigned int t) { stream->ts << t; return maybeSpace(); }
    00103     inline QDebug &operator<<(signed long t) { stream->ts << t; return maybeSpace(); }
    00104     inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); }
    00105     inline QDebug &operator<<(qint64 t)
    00106         { stream->ts << QString::number(t); return maybeSpace(); }
    00107     inline QDebug &operator<<(quint64 t)
    00108         { stream->ts << QString::number(t); return maybeSpace(); }
    00109     inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
    00110     inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
    00111     inline QDebug &operator<<(const char* t) { stream->ts << QString::fromAscii(t); return maybeSpace(); }
    00112     inline QDebug &operator<<(const QString & t) { stream->ts << '\"' << t  << '\"'; return maybeSpace(); }
    00113     inline QDebug &operator<<(const QStringRef & t) { return operator<<(t.toString()); }
    00114     inline QDebug &operator<<(const QLatin1String &t) { stream->ts << '\"'  << t.latin1() << '\"'; return maybeSpace(); }
    00115     inline QDebug &operator<<(const QByteArray & t) { stream->ts  << '\"' << t << '\"'; return maybeSpace(); }
    00116     inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
    00117     inline QDebug &operator<<(QTextStreamFunction f) {
    00118         stream->ts << f;
    00119         return *this;
    00120     }
    00121
    00122     inline QDebug &operator<<(QTextStreamManipulator m)
    00123     { stream->ts << m; return *this; }
    00124 };
    00125
    00126 class QNoDebug
    00127 {
    00128 public:
    00129     inline QNoDebug(){}
    00130     inline QNoDebug(const QDebug &){}
    00131     inline ~QNoDebug(){}
    00132 #if !defined( QT_NO_TEXTSTREAM )
    00133     inline QNoDebug &operator<<(QTextStreamFunction) { return *this; }
    00134     inline QNoDebug &operator<<(QTextStreamManipulator) { return *this; }
    00135 #endif
    00136     inline QNoDebug &space() { return *this; }
    00137     inline QNoDebug &nospace() { return *this; }
    00138     inline QNoDebug &maybeSpace() { return *this; }
    00139
    00140 #ifndef QT_NO_MEMBER_TEMPLATES
    00141     template<typename T>
    00142     inline QNoDebug &operator<<(const T &) { return *this; }
    00143 #endif
    00144 };
    00145
    00146 Q_CORE_EXPORT_INLINE QDebug qCritical() { return QDebug(QtCriticalMsg); }
    00147
    00148 inline QDebug &QDebug::operator=(const QDebug &other)
    00149 {
    00150     if (this != &other) {
    00151         QDebug copy(other);
    00152         qSwap(stream, copy.stream);
    00153     }
    00154     return *this;
    00155 }
    00156
    00157 #if defined(FORCE_UREF)
    00158 template <class T>
    00159 inline QDebug &operator<<(QDebug debug, const QList<T> &list)
    00160 #else
    00161 template <class T>
    00162 inline QDebug operator<<(QDebug debug, const QList<T> &list)
    00163 #endif
    00164 {
    00165     debug.nospace() << '(';
    00166     for (Q_TYPENAME QList<T>::size_type i = 0; i < list.count(); ++i) {
    00167         if (i)
    00168             debug << ", ";
    00169         debug << list.at(i);
    00170     }
    00171     debug << ')';
    00172     return debug.space();
    00173 }
    00174
    00175 #if defined(FORCE_UREF)
    00176 template <typename T>
    00177 inline QDebug &operator<<(QDebug debug, const QVector<T> &vec)
    00178 #else
    00179 template <typename T>
    00180 inline QDebug operator<<(QDebug debug, const QVector<T> &vec)
    00181 #endif
    00182 {
    00183     debug.nospace() << "QVector";
    00184     return operator<<(debug, vec.toList());
    00185 }
    00186
    00187 #if defined(FORCE_UREF)
    00188 template <class aKey, class aT>
    00189 inline QDebug &operator<<(QDebug debug, const QMap<aKey, aT> &map)
    00190 #else
    00191 template <class aKey, class aT>
    00192 inline QDebug operator<<(QDebug debug, const QMap<aKey, aT> &map)
    00193 #endif
    00194 {
    00195     debug.nospace() << "QMap(";
    00196     for (typename QMap<aKey, aT>::const_iterator it = map.constBegin();
    00197          it != map.constEnd(); ++it) {
    00198         debug << '(' << it.key() << ", " << it.value() << ')';
    00199     }
    00200     debug << ')';
    00201     return debug.space();
    00202 }
    00203
    00204 #if defined(FORCE_UREF)
    00205 template <class aKey, class aT>
    00206 inline QDebug &operator<<(QDebug debug, const QHash<aKey, aT> &hash)
    00207 #else
    00208 template <class aKey, class aT>
    00209 inline QDebug operator<<(QDebug debug, const QHash<aKey, aT> &hash)
    00210 #endif
    00211 {
    00212     debug.nospace() << "QHash(";
    00213     for (typename QHash<aKey, aT>::const_iterator it = hash.constBegin();
    00214             it != hash.constEnd(); ++it)
    00215         debug << '(' << it.key() << ", " << it.value() << ')';
    00216     debug << ')';
    00217     return debug.space();
    00218 }
    00219
    00220 #if defined(FORCE_UREF)
    00221 template <class T1, class T2>
    00222 inline QDebug &operator<<(QDebug debug, const QPair<T1, T2> &pair)
    00223 #else
    00224 template <class T1, class T2>
    00225 inline QDebug operator<<(QDebug debug, const QPair<T1, T2> &pair)
    00226 #endif
    00227 {
    00228     debug.nospace() << "QPair(" << pair.first << ',' << pair.second << ')';
    00229     return debug.space();
    00230 }
    00231
    00232 template <typename T>
    00233 inline QDebug operator<<(QDebug debug, const QSet<T> &set)
    00234 {
    00235     debug.nospace() << "QSet";
    00236     return operator<<(debug, set.toList());
    00237 }
    00238
    00239 #if defined(FORCE_UREF)
    00240 template <class T>
    00241 inline QDebug &operator<<(QDebug debug, const QContiguousCache<T> &cache)
    00242 #else
    00243 template <class T>
    00244 inline QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)
    00245 #endif
    00246 {
    00247     debug.nospace() << "QContiguousCache(";
    00248     for (int i = cache.firstIndex(); i <= cache.lastIndex(); ++i) {
    00249         debug << cache[i];
    00250         if (i != cache.lastIndex())
    00251             debug << ", ";
    00252     }
    00253     debug << ')';
    00254     return debug.space();
    00255 }
    00256
    00257 #if defined(FORCE_UREF)
    00258 template <class T>
    00259 inline QDebug &operator<<(QDebug debug, const QFlags<T> &flags)
    00260 #else
    00261 template <class T>
    00262 inline QDebug operator<<(QDebug debug, const QFlags<T> &flags)
    00263 #endif
    00264 {
    00265     debug.nospace() << "QFlags(";
    00266     bool needSeparator = false;
    00267     for (uint i = 0; i < sizeof(T) * 8; ++i) {
    00268         if (flags.testFlag(T(1 << i))) {
    00269             if (needSeparator)
    00270                 debug.nospace() << '|';
    00271             else
    00272                 needSeparator = true;
    00273             debug.nospace() << "0x" << QByteArray::number(T(1 << i), 16).constData();
    00274         }
    00275     }
    00276     debug << ')';
    00277     return debug.space();
    00278 }
    00279
    00280 #if !defined(QT_NO_DEBUG_STREAM)
    00281 Q_CORE_EXPORT_INLINE QDebug qDebug() { return QDebug(QtDebugMsg); }
    00282
    00283 #else // QT_NO_DEBUG_STREAM
    00284 #undef qDebug
    00285 inline QNoDebug qDebug() { return QNoDebug(); }
    00286 #define qDebug QT_NO_QDEBUG_MACRO
    00287
    00288 #ifdef QT_NO_MEMBER_TEMPLATES
    00289 template<typename T>
    00290 inline QNoDebug operator<<(QNoDebug debug, const T &) { return debug; }
    00291 #endif
    00292
    00293 #endif
    00294
    00295 #if !defined(QT_NO_WARNING_OUTPUT)
    00296 Q_CORE_EXPORT_INLINE QDebug qWarning() { return QDebug(QtWarningMsg); }
    00297 #else
    00298 #undef qWarning
    00299 inline QNoDebug qWarning() { return QNoDebug(); }
    00300 #define qWarning QT_NO_QWARNING_MACRO
    00301 #endif
    00302
    00303 QT_END_NAMESPACE
    00304
    00305 QT_END_HEADER
    00306
    00307 #endif // QDEBUG_H