QtCore: Use QDebugStateSaver in (almost) all QDebug operator<<

Unify the behavior of the different operator<< by always using
QDebugStateSaver (appending an optional space at exit), and making
sure that the space(), nospace() setting isn't 'leaked'.

Change-Id: I38e4f82fa6f7419d8b5edfc4dc37495af497e8ac
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
This commit is contained in:
Kai Koehne 2015-01-23 16:19:11 +01:00
parent 6389160f04
commit 868201155f
22 changed files with 77 additions and 48 deletions

View File

@ -3904,8 +3904,9 @@ QDataStream &operator>>(QDataStream &in, QUrl &url)
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug d, const QUrl &url) QDebug operator<<(QDebug d, const QUrl &url)
{ {
d.maybeSpace() << "QUrl(" << url.toDisplayString() << ')'; QDebugStateSaver saver(d);
return d.space(); d.nospace() << "QUrl(" << url.toDisplayString() << ')';
return d;
} }
#endif #endif

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal ** Contact: http://www.qt-project.org/legal
** **
** This file is part of the QtCore module of the Qt Toolkit. ** This file is part of the QtCore module of the Qt Toolkit.
@ -449,9 +449,10 @@ bool QPersistentModelIndex::isValid() const
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QModelIndex &idx) QDebug operator<<(QDebug dbg, const QModelIndex &idx)
{ {
QDebugStateSaver saver(dbg);
dbg.nospace() << "QModelIndex(" << idx.row() << ',' << idx.column() dbg.nospace() << "QModelIndex(" << idx.row() << ',' << idx.column()
<< ',' << idx.internalPointer() << ',' << idx.model() << ')'; << ',' << idx.internalPointer() << ',' << idx.model() << ')';
return dbg.space(); return dbg;
} }
QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx) QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx)

View File

@ -1849,9 +1849,10 @@ void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelectio
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QItemSelectionRange &range) QDebug operator<<(QDebug dbg, const QItemSelectionRange &range)
{ {
QDebugStateSaver saver(dbg);
dbg.nospace() << "QItemSelectionRange(" << range.topLeft() dbg.nospace() << "QItemSelectionRange(" << range.topLeft()
<< ',' << range.bottomRight() << ')'; << ',' << range.bottomRight() << ')';
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -1154,6 +1154,7 @@ void QJsonArray::compact()
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
QDebug operator<<(QDebug dbg, const QJsonArray &a) QDebug operator<<(QDebug dbg, const QJsonArray &a)
{ {
QDebugStateSaver saver(dbg);
if (!a.a) { if (!a.a) {
dbg << "QJsonArray()"; dbg << "QJsonArray()";
return dbg; return dbg;
@ -1163,7 +1164,7 @@ QDebug operator<<(QDebug dbg, const QJsonArray &a)
dbg.nospace() << "QJsonArray(" dbg.nospace() << "QJsonArray("
<< json.constData() // print as utf-8 string without extra quotation marks << json.constData() // print as utf-8 string without extra quotation marks
<< ")"; << ")";
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -561,6 +561,7 @@ bool QJsonDocument::isNull() const
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
QDebug operator<<(QDebug dbg, const QJsonDocument &o) QDebug operator<<(QDebug dbg, const QJsonDocument &o)
{ {
QDebugStateSaver saver(dbg);
if (!o.d) { if (!o.d) {
dbg << "QJsonDocument()"; dbg << "QJsonDocument()";
return dbg; return dbg;
@ -573,7 +574,7 @@ QDebug operator<<(QDebug dbg, const QJsonDocument &o)
dbg.nospace() << "QJsonDocument(" dbg.nospace() << "QJsonDocument("
<< json.constData() // print as utf-8 string without extra quotation marks << json.constData() // print as utf-8 string without extra quotation marks
<< ")"; << ")";
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -1111,6 +1111,7 @@ void QJsonObject::setValueAt(int i, const QJsonValue &val)
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
QDebug operator<<(QDebug dbg, const QJsonObject &o) QDebug operator<<(QDebug dbg, const QJsonObject &o)
{ {
QDebugStateSaver saver(dbg);
if (!o.o) { if (!o.o) {
dbg << "QJsonObject()"; dbg << "QJsonObject()";
return dbg; return dbg;
@ -1120,7 +1121,7 @@ QDebug operator<<(QDebug dbg, const QJsonObject &o)
dbg.nospace() << "QJsonObject(" dbg.nospace() << "QJsonObject("
<< json.constData() // print as utf-8 string without extra quotation marks << json.constData() // print as utf-8 string without extra quotation marks
<< ")"; << ")";
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal ** Contact: http://www.qt-project.org/legal
** **
** This file is part of the QtCore module of the Qt Toolkit. ** This file is part of the QtCore module of the Qt Toolkit.
@ -709,12 +709,13 @@ QJsonValue QJsonValueRef::toValue() const
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
QDebug operator<<(QDebug dbg, const QJsonValue &o) QDebug operator<<(QDebug dbg, const QJsonValue &o)
{ {
QDebugStateSaver saver(dbg);
switch (o.t) { switch (o.t) {
case QJsonValue::Undefined: case QJsonValue::Undefined:
dbg.nospace() << "QJsonValue(undefined)"; dbg << "QJsonValue(undefined)";
break; break;
case QJsonValue::Null: case QJsonValue::Null:
dbg.nospace() << "QJsonValue(null)"; dbg << "QJsonValue(null)";
break; break;
case QJsonValue::Bool: case QJsonValue::Bool:
dbg.nospace() << "QJsonValue(bool, " << o.toBool() << ")"; dbg.nospace() << "QJsonValue(bool, " << o.toBool() << ")";
@ -727,16 +728,16 @@ QDebug operator<<(QDebug dbg, const QJsonValue &o)
break; break;
case QJsonValue::Array: case QJsonValue::Array:
dbg.nospace() << "QJsonValue(array, "; dbg.nospace() << "QJsonValue(array, ";
dbg.nospace() << o.toArray(); dbg << o.toArray();
dbg.nospace() << ")"; dbg << ")";
break; break;
case QJsonValue::Object: case QJsonValue::Object:
dbg.nospace() << "QJsonValue(object, "; dbg.nospace() << "QJsonValue(object, ";
dbg.nospace() << o.toObject(); dbg << o.toObject();
dbg.nospace() << ")"; dbg << ")";
break; break;
} }
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -1041,8 +1041,9 @@ QString decodeMSG(const MSG& msg)
QDebug operator<<(QDebug dbg, const MSG &msg) QDebug operator<<(QDebug dbg, const MSG &msg)
{ {
QDebugStateSaver saver(dbg);
dbg << decodeMSG(msg); dbg << decodeMSG(msg);
return dbg.nospace(); return dbg;
} }
#endif #endif

View File

@ -260,7 +260,8 @@ QVariant QPpsAttribute::toVariant() const
QDebug operator<<(QDebug dbg, const QPpsAttribute &attribute) QDebug operator<<(QDebug dbg, const QPpsAttribute &attribute)
{ {
dbg << "QPpsAttribute("; QDebugStateSaver saver(dbg);
dbg.nospace() << "QPpsAttribute(";
switch (attribute.type()) { switch (attribute.type()) {
case QPpsAttribute::Number: case QPpsAttribute::Number:

View File

@ -207,11 +207,13 @@ static timespec roundToMillisecond(timespec val)
#ifdef QTIMERINFO_DEBUG #ifdef QTIMERINFO_DEBUG
QDebug operator<<(QDebug s, timeval tv) QDebug operator<<(QDebug s, timeval tv)
{ {
QDebugStateSaver saver(s);
s.nospace() << tv.tv_sec << "." << qSetFieldWidth(6) << qSetPadChar(QChar(48)) << tv.tv_usec << reset; s.nospace() << tv.tv_sec << "." << qSetFieldWidth(6) << qSetPadChar(QChar(48)) << tv.tv_usec << reset;
return s.space(); return s;
} }
QDebug operator<<(QDebug s, Qt::TimerType t) QDebug operator<<(QDebug s, Qt::TimerType t)
{ {
QDebugStateSaver saver(s);
s << (t == Qt::PreciseTimer ? "P" : s << (t == Qt::PreciseTimer ? "P" :
t == Qt::CoarseTimer ? "C" : "VC"); t == Qt::CoarseTimer ? "C" : "VC");
return s; return s;

View File

@ -3424,10 +3424,11 @@ bool QVariant::isNull() const
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QVariant &v) QDebug operator<<(QDebug dbg, const QVariant &v)
{ {
QDebugStateSaver saver(dbg);
const uint typeId = v.d.type; const uint typeId = v.d.type;
dbg.nospace() << "QVariant("; dbg.nospace() << "QVariant(";
if (typeId != QMetaType::UnknownType) { if (typeId != QMetaType::UnknownType) {
dbg.nospace() << QMetaType::typeName(typeId) << ", "; dbg << QMetaType::typeName(typeId) << ", ";
bool userStream = false; bool userStream = false;
bool canConvertToString = false; bool canConvertToString = false;
if (typeId >= QMetaType::User) { if (typeId >= QMetaType::User) {
@ -3439,19 +3440,20 @@ QDebug operator<<(QDebug dbg, const QVariant &v)
else if (!userStream) else if (!userStream)
handlerManager[typeId]->debugStream(dbg, v); handlerManager[typeId]->debugStream(dbg, v);
} else { } else {
dbg.nospace() << "Invalid"; dbg << "Invalid";
} }
dbg.nospace() << ')'; dbg << ')';
return dbg.space(); return dbg;
} }
QDebug operator<<(QDebug dbg, const QVariant::Type p) QDebug operator<<(QDebug dbg, const QVariant::Type p)
{ {
QDebugStateSaver saver(dbg);
dbg.nospace() << "QVariant::" dbg.nospace() << "QVariant::"
<< (int(p) != int(QMetaType::UnknownType) << (int(p) != int(QMetaType::UnknownType)
? QMetaType::typeName(p) ? QMetaType::typeName(p)
: "Invalid"); : "Invalid");
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -993,8 +993,9 @@ QUuid QUuid::createUuid()
*/ */
QDebug operator<<(QDebug dbg, const QUuid &id) QDebug operator<<(QDebug dbg, const QUuid &id)
{ {
QDebugStateSaver saver(dbg);
dbg.nospace() << "QUuid(" << id.toString() << ')'; dbg.nospace() << "QUuid(" << id.toString() << ')';
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -772,18 +772,19 @@ QDataStream &operator>>(QDataStream &in, QBitArray &ba)
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QBitArray &array) QDebug operator<<(QDebug dbg, const QBitArray &array)
{ {
QDebugStateSaver saver(dbg);
dbg.nospace() << "QBitArray("; dbg.nospace() << "QBitArray(";
for (int i = 0; i < array.size();) { for (int i = 0; i < array.size();) {
if (array.testBit(i)) if (array.testBit(i))
dbg.nospace() << '1'; dbg << '1';
else else
dbg.nospace() << '0'; dbg << '0';
i += 1; i += 1;
if (!(i % 4) && (i < array.size())) if (!(i % 4) && (i < array.size()))
dbg.nospace() << ' '; dbg << ' ';
} }
dbg.nospace() << ')'; dbg << ')';
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -5006,18 +5006,21 @@ QDataStream &operator>>(QDataStream &in, QDateTime &dateTime)
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING) #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING)
QDebug operator<<(QDebug dbg, const QDate &date) QDebug operator<<(QDebug dbg, const QDate &date)
{ {
QDebugStateSaver saver(dbg);
dbg.nospace() << "QDate(" << date.toString(Qt::ISODate) << ')'; dbg.nospace() << "QDate(" << date.toString(Qt::ISODate) << ')';
return dbg.space(); return dbg;
} }
QDebug operator<<(QDebug dbg, const QTime &time) QDebug operator<<(QDebug dbg, const QTime &time)
{ {
QDebugStateSaver saver(dbg);
dbg.nospace() << "QTime(" << time.toString(QStringLiteral("HH:mm:ss.zzz")) << ')'; dbg.nospace() << "QTime(" << time.toString(QStringLiteral("HH:mm:ss.zzz")) << ')';
return dbg.space(); return dbg;
} }
QDebug operator<<(QDebug dbg, const QDateTime &date) QDebug operator<<(QDebug dbg, const QDateTime &date)
{ {
QDebugStateSaver saver(dbg);
QString spec; QString spec;
switch (date.d->m_spec) { switch (date.d->m_spec) {
case Qt::UTC: case Qt::UTC:
@ -5037,7 +5040,7 @@ QDebug operator<<(QDebug dbg, const QDateTime &date)
} }
QString output = date.toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz t")) + spec; QString output = date.toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz t")) + spec;
dbg.nospace() << "QDateTime(" << output << ')'; dbg.nospace() << "QDateTime(" << output << ')';
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -1417,6 +1417,7 @@ qreal QEasingCurve::valueForProgress(qreal progress) const
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug debug, const QEasingCurve &item) QDebug operator<<(QDebug debug, const QEasingCurve &item)
{ {
QDebugStateSaver saver(debug);
debug << "type:" << item.d_ptr->type debug << "type:" << item.d_ptr->type
<< "func:" << item.d_ptr->func; << "func:" << item.d_ptr->func;
if (item.d_ptr->config) { if (item.d_ptr->config) {

View File

@ -3583,10 +3583,12 @@ QString QLocale::nativeCountryName() const
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QLocale &l) QDebug operator<<(QDebug dbg, const QLocale &l)
{ {
dbg.nospace() << "QLocale(" << qPrintable(QLocale::languageToString(l.language())) QDebugStateSaver saver(dbg);
<< ", " << qPrintable(QLocale::scriptToString(l.script())) dbg.nospace().noquote()
<< ", " << qPrintable(QLocale::countryToString(l.country())) << ')'; << "QLocale(" << QLocale::languageToString(l.language())
return dbg.space(); << ", " << QLocale::scriptToString(l.script())
<< ", " << QLocale::countryToString(l.country()) << ')';
return dbg;
} }
#endif #endif
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -432,9 +432,10 @@ QDataStream &operator>>(QDataStream &s, QMargins &m)
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QMargins &m) { QDebug operator<<(QDebug dbg, const QMargins &m) {
QDebugStateSaver saver(dbg);
dbg.nospace() << "QMargins(" << m.left() << ", " dbg.nospace() << "QMargins(" << m.left() << ", "
<< m.top() << ", " << m.right() << ", " << m.bottom() << ')'; << m.top() << ", " << m.right() << ", " << m.bottom() << ')';
return dbg.space(); return dbg;
} }
#endif #endif
@ -764,9 +765,10 @@ QDataStream &operator>>(QDataStream &s, QMarginsF &m)
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QMarginsF &m) { QDebug operator<<(QDebug dbg, const QMarginsF &m) {
QDebugStateSaver saver(dbg);
dbg.nospace() << "QMarginsF(" << m.left() << ", " dbg.nospace() << "QMarginsF(" << m.left() << ", "
<< m.top() << ", " << m.right() << ", " << m.bottom() << ')'; << m.top() << ", " << m.right() << ", " << m.bottom() << ')';
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -4577,9 +4577,10 @@ QDataStream &operator>>(QDataStream &in, QRegExp &regExp)
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QRegExp &r) QDebug operator<<(QDebug dbg, const QRegExp &r)
{ {
QDebugStateSaver saver(dbg);
dbg.nospace() << "QRegExp(patternSyntax=" << r.patternSyntax() dbg.nospace() << "QRegExp(patternSyntax=" << r.patternSyntax()
<< ", pattern='"<< r.pattern() << "')"; << ", pattern='"<< r.pattern() << "')";
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -2444,8 +2444,9 @@ QDataStream &operator>>(QDataStream &in, QRegularExpression &re)
*/ */
QDebug operator<<(QDebug debug, const QRegularExpression &re) QDebug operator<<(QDebug debug, const QRegularExpression &re)
{ {
QDebugStateSaver saver(debug);
debug.nospace() << "QRegularExpression(" << re.pattern() << ", " << re.patternOptions() << ")"; debug.nospace() << "QRegularExpression(" << re.pattern() << ", " << re.patternOptions() << ")";
return debug.space(); return debug;
} }
/*! /*!
@ -2458,6 +2459,7 @@ QDebug operator<<(QDebug debug, const QRegularExpression &re)
*/ */
QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions) QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions)
{ {
QDebugStateSaver saver(debug);
QByteArray flags; QByteArray flags;
if (patternOptions == QRegularExpression::NoPatternOption) { if (patternOptions == QRegularExpression::NoPatternOption) {
@ -2487,7 +2489,7 @@ QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOption
debug.nospace() << "QRegularExpression::PatternOptions(" << flags << ")"; debug.nospace() << "QRegularExpression::PatternOptions(" << flags << ")";
return debug.space(); return debug;
} }
/*! /*!
\relates QRegularExpressionMatch \relates QRegularExpressionMatch
@ -2499,11 +2501,12 @@ QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOption
*/ */
QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match) QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match)
{ {
QDebugStateSaver saver(debug);
debug.nospace() << "QRegularExpressionMatch("; debug.nospace() << "QRegularExpressionMatch(";
if (!match.isValid()) { if (!match.isValid()) {
debug << "Invalid)"; debug << "Invalid)";
return debug.space(); return debug;
} }
debug << "Valid"; debug << "Valid";
@ -2528,7 +2531,7 @@ QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match)
debug << ")"; debug << ")";
return debug.space(); return debug;
} }
#endif #endif

View File

@ -980,9 +980,10 @@ QDataStream &operator>>(QDataStream &ds, QTimeZone &tz)
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QTimeZone &tz) QDebug operator<<(QDebug dbg, const QTimeZone &tz)
{ {
QDebugStateSaver saver(dbg);
//TODO Include backend and data version details? //TODO Include backend and data version details?
dbg.nospace() << "QTimeZone(" << QString::fromUtf8(tz.id()) << ')'; dbg.nospace() << "QTimeZone(" << QString::fromUtf8(tz.id()) << ')';
return dbg.space(); return dbg;
} }
#endif #endif

View File

@ -458,8 +458,9 @@ QDataStream& operator>>(QDataStream &in, QVersionNumber &version)
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug debug, const QVersionNumber &version) QDebug operator<<(QDebug debug, const QVersionNumber &version)
{ {
QDebugStateSaver saver(debug);
debug.noquote() << version.toString(); debug.noquote() << version.toString();
return debug.quote(); return debug;
} }
#endif #endif

View File

@ -2204,13 +2204,13 @@ void tst_QtJson::testDebugStream()
array.append(1); array.append(1);
array.append(QLatin1String("foo")); array.append(QLatin1String("foo"));
value = QJsonValue(array); // array value = QJsonValue(array); // array
QTest::ignoreMessage(QtDebugMsg, "QJsonValue(array, QJsonArray([1,\"foo\"]) )"); QTest::ignoreMessage(QtDebugMsg, "QJsonValue(array, QJsonArray([1,\"foo\"]))");
qDebug() << value; qDebug() << value;
QJsonObject object; QJsonObject object;
object.insert(QLatin1String("foo"), QLatin1String("bar")); object.insert(QLatin1String("foo"), QLatin1String("bar"));
value = QJsonValue(object); // object value = QJsonValue(object); // object
QTest::ignoreMessage(QtDebugMsg, "QJsonValue(object, QJsonObject({\"foo\":\"bar\"}) )"); QTest::ignoreMessage(QtDebugMsg, "QJsonValue(object, QJsonObject({\"foo\":\"bar\"}))");
qDebug() << value; qDebug() << value;
} }
} }