qdebug.h
上传用户:detong
上传日期:2022-06-22
资源大小:20675k
文件大小:9k
源码类别:

系统编程

开发平台:

Unix_Linux

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