From 868201155fd677dbc6d14346f5ea61e82ebce27b Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Fri, 23 Jan 2015 16:19:11 +0100 Subject: 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 --- src/corelib/io/qurl.cpp | 5 +++-- src/corelib/itemmodels/qabstractitemmodel.cpp | 5 +++-- src/corelib/itemmodels/qitemselectionmodel.cpp | 3 ++- src/corelib/json/qjsonarray.cpp | 3 ++- src/corelib/json/qjsondocument.cpp | 3 ++- src/corelib/json/qjsonobject.cpp | 3 ++- src/corelib/json/qjsonvalue.cpp | 17 +++++++++-------- src/corelib/kernel/qcoreapplication_win.cpp | 3 ++- src/corelib/kernel/qppsattribute.cpp | 3 ++- src/corelib/kernel/qtimerinfo_unix.cpp | 4 +++- src/corelib/kernel/qvariant.cpp | 12 +++++++----- src/corelib/plugin/quuid.cpp | 3 ++- src/corelib/tools/qbitarray.cpp | 11 ++++++----- src/corelib/tools/qdatetime.cpp | 9 ++++++--- src/corelib/tools/qeasingcurve.cpp | 1 + src/corelib/tools/qlocale.cpp | 10 ++++++---- src/corelib/tools/qmargins.cpp | 6 ++++-- src/corelib/tools/qregexp.cpp | 3 ++- src/corelib/tools/qregularexpression.cpp | 11 +++++++---- src/corelib/tools/qtimezone.cpp | 3 ++- src/corelib/tools/qversionnumber.cpp | 3 ++- tests/auto/corelib/json/tst_qtjson.cpp | 4 ++-- 22 files changed, 77 insertions(+), 48 deletions(-) diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index eb97eaf6d6..9c739140d7 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -3904,8 +3904,9 @@ QDataStream &operator>>(QDataStream &in, QUrl &url) #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug d, const QUrl &url) { - d.maybeSpace() << "QUrl(" << url.toDisplayString() << ')'; - return d.space(); + QDebugStateSaver saver(d); + d.nospace() << "QUrl(" << url.toDisplayString() << ')'; + return d; } #endif diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index 43b51bcaf5..7eed37033c 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -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 ** ** 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 QDebug operator<<(QDebug dbg, const QModelIndex &idx) { + QDebugStateSaver saver(dbg); dbg.nospace() << "QModelIndex(" << idx.row() << ',' << idx.column() << ',' << idx.internalPointer() << ',' << idx.model() << ')'; - return dbg.space(); + return dbg; } QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx) diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp index 920190e712..f1dd503bff 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.cpp +++ b/src/corelib/itemmodels/qitemselectionmodel.cpp @@ -1849,9 +1849,10 @@ void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelectio #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QItemSelectionRange &range) { + QDebugStateSaver saver(dbg); dbg.nospace() << "QItemSelectionRange(" << range.topLeft() << ',' << range.bottomRight() << ')'; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/json/qjsonarray.cpp b/src/corelib/json/qjsonarray.cpp index acbd7dda8f..72674b8c40 100644 --- a/src/corelib/json/qjsonarray.cpp +++ b/src/corelib/json/qjsonarray.cpp @@ -1154,6 +1154,7 @@ void QJsonArray::compact() #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) QDebug operator<<(QDebug dbg, const QJsonArray &a) { + QDebugStateSaver saver(dbg); if (!a.a) { dbg << "QJsonArray()"; return dbg; @@ -1163,7 +1164,7 @@ QDebug operator<<(QDebug dbg, const QJsonArray &a) dbg.nospace() << "QJsonArray(" << json.constData() // print as utf-8 string without extra quotation marks << ")"; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/json/qjsondocument.cpp b/src/corelib/json/qjsondocument.cpp index 6e64df42ad..fe8bfdf0b5 100644 --- a/src/corelib/json/qjsondocument.cpp +++ b/src/corelib/json/qjsondocument.cpp @@ -561,6 +561,7 @@ bool QJsonDocument::isNull() const #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) QDebug operator<<(QDebug dbg, const QJsonDocument &o) { + QDebugStateSaver saver(dbg); if (!o.d) { dbg << "QJsonDocument()"; return dbg; @@ -573,7 +574,7 @@ QDebug operator<<(QDebug dbg, const QJsonDocument &o) dbg.nospace() << "QJsonDocument(" << json.constData() // print as utf-8 string without extra quotation marks << ")"; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/json/qjsonobject.cpp b/src/corelib/json/qjsonobject.cpp index df604e4433..3210f05507 100644 --- a/src/corelib/json/qjsonobject.cpp +++ b/src/corelib/json/qjsonobject.cpp @@ -1111,6 +1111,7 @@ void QJsonObject::setValueAt(int i, const QJsonValue &val) #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) QDebug operator<<(QDebug dbg, const QJsonObject &o) { + QDebugStateSaver saver(dbg); if (!o.o) { dbg << "QJsonObject()"; return dbg; @@ -1120,7 +1121,7 @@ QDebug operator<<(QDebug dbg, const QJsonObject &o) dbg.nospace() << "QJsonObject(" << json.constData() // print as utf-8 string without extra quotation marks << ")"; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/json/qjsonvalue.cpp b/src/corelib/json/qjsonvalue.cpp index 4c5d9e3308..bc2bca2823 100644 --- a/src/corelib/json/qjsonvalue.cpp +++ b/src/corelib/json/qjsonvalue.cpp @@ -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 ** ** 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) QDebug operator<<(QDebug dbg, const QJsonValue &o) { + QDebugStateSaver saver(dbg); switch (o.t) { case QJsonValue::Undefined: - dbg.nospace() << "QJsonValue(undefined)"; + dbg << "QJsonValue(undefined)"; break; case QJsonValue::Null: - dbg.nospace() << "QJsonValue(null)"; + dbg << "QJsonValue(null)"; break; case QJsonValue::Bool: dbg.nospace() << "QJsonValue(bool, " << o.toBool() << ")"; @@ -727,16 +728,16 @@ QDebug operator<<(QDebug dbg, const QJsonValue &o) break; case QJsonValue::Array: dbg.nospace() << "QJsonValue(array, "; - dbg.nospace() << o.toArray(); - dbg.nospace() << ")"; + dbg << o.toArray(); + dbg << ")"; break; case QJsonValue::Object: dbg.nospace() << "QJsonValue(object, "; - dbg.nospace() << o.toObject(); - dbg.nospace() << ")"; + dbg << o.toObject(); + dbg << ")"; break; } - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/kernel/qcoreapplication_win.cpp b/src/corelib/kernel/qcoreapplication_win.cpp index 424ee3fdea..dfb358202d 100644 --- a/src/corelib/kernel/qcoreapplication_win.cpp +++ b/src/corelib/kernel/qcoreapplication_win.cpp @@ -1041,8 +1041,9 @@ QString decodeMSG(const MSG& msg) QDebug operator<<(QDebug dbg, const MSG &msg) { + QDebugStateSaver saver(dbg); dbg << decodeMSG(msg); - return dbg.nospace(); + return dbg; } #endif diff --git a/src/corelib/kernel/qppsattribute.cpp b/src/corelib/kernel/qppsattribute.cpp index 09d8d1bb0c..f4c067e35a 100644 --- a/src/corelib/kernel/qppsattribute.cpp +++ b/src/corelib/kernel/qppsattribute.cpp @@ -260,7 +260,8 @@ QVariant QPpsAttribute::toVariant() const QDebug operator<<(QDebug dbg, const QPpsAttribute &attribute) { - dbg << "QPpsAttribute("; + QDebugStateSaver saver(dbg); + dbg.nospace() << "QPpsAttribute("; switch (attribute.type()) { case QPpsAttribute::Number: diff --git a/src/corelib/kernel/qtimerinfo_unix.cpp b/src/corelib/kernel/qtimerinfo_unix.cpp index 43674f2c9c..f4a42acb59 100644 --- a/src/corelib/kernel/qtimerinfo_unix.cpp +++ b/src/corelib/kernel/qtimerinfo_unix.cpp @@ -207,11 +207,13 @@ static timespec roundToMillisecond(timespec val) #ifdef QTIMERINFO_DEBUG QDebug operator<<(QDebug s, timeval tv) { + QDebugStateSaver saver(s); 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) { + QDebugStateSaver saver(s); s << (t == Qt::PreciseTimer ? "P" : t == Qt::CoarseTimer ? "C" : "VC"); return s; diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index df1b4ed872..9e61813e20 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -3424,10 +3424,11 @@ bool QVariant::isNull() const #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QVariant &v) { + QDebugStateSaver saver(dbg); const uint typeId = v.d.type; dbg.nospace() << "QVariant("; if (typeId != QMetaType::UnknownType) { - dbg.nospace() << QMetaType::typeName(typeId) << ", "; + dbg << QMetaType::typeName(typeId) << ", "; bool userStream = false; bool canConvertToString = false; if (typeId >= QMetaType::User) { @@ -3439,19 +3440,20 @@ QDebug operator<<(QDebug dbg, const QVariant &v) else if (!userStream) handlerManager[typeId]->debugStream(dbg, v); } else { - dbg.nospace() << "Invalid"; + dbg << "Invalid"; } - dbg.nospace() << ')'; - return dbg.space(); + dbg << ')'; + return dbg; } QDebug operator<<(QDebug dbg, const QVariant::Type p) { + QDebugStateSaver saver(dbg); dbg.nospace() << "QVariant::" << (int(p) != int(QMetaType::UnknownType) ? QMetaType::typeName(p) : "Invalid"); - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index 6df3f3b0d1..55b64c4ec3 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -993,8 +993,9 @@ QUuid QUuid::createUuid() */ QDebug operator<<(QDebug dbg, const QUuid &id) { + QDebugStateSaver saver(dbg); dbg.nospace() << "QUuid(" << id.toString() << ')'; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/tools/qbitarray.cpp b/src/corelib/tools/qbitarray.cpp index 5c391d7e4f..ddccd8cd02 100644 --- a/src/corelib/tools/qbitarray.cpp +++ b/src/corelib/tools/qbitarray.cpp @@ -772,18 +772,19 @@ QDataStream &operator>>(QDataStream &in, QBitArray &ba) #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QBitArray &array) { + QDebugStateSaver saver(dbg); dbg.nospace() << "QBitArray("; for (int i = 0; i < array.size();) { if (array.testBit(i)) - dbg.nospace() << '1'; + dbg << '1'; else - dbg.nospace() << '0'; + dbg << '0'; i += 1; if (!(i % 4) && (i < array.size())) - dbg.nospace() << ' '; + dbg << ' '; } - dbg.nospace() << ')'; - return dbg.space(); + dbg << ')'; + return dbg; } #endif diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index b95ccc0874..656638834f 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -5006,18 +5006,21 @@ QDataStream &operator>>(QDataStream &in, QDateTime &dateTime) #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING) QDebug operator<<(QDebug dbg, const QDate &date) { + QDebugStateSaver saver(dbg); dbg.nospace() << "QDate(" << date.toString(Qt::ISODate) << ')'; - return dbg.space(); + return dbg; } QDebug operator<<(QDebug dbg, const QTime &time) { + QDebugStateSaver saver(dbg); dbg.nospace() << "QTime(" << time.toString(QStringLiteral("HH:mm:ss.zzz")) << ')'; - return dbg.space(); + return dbg; } QDebug operator<<(QDebug dbg, const QDateTime &date) { + QDebugStateSaver saver(dbg); QString spec; switch (date.d->m_spec) { 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; dbg.nospace() << "QDateTime(" << output << ')'; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/tools/qeasingcurve.cpp b/src/corelib/tools/qeasingcurve.cpp index e6fb531d7d..7d63f95d1b 100644 --- a/src/corelib/tools/qeasingcurve.cpp +++ b/src/corelib/tools/qeasingcurve.cpp @@ -1417,6 +1417,7 @@ qreal QEasingCurve::valueForProgress(qreal progress) const #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug debug, const QEasingCurve &item) { + QDebugStateSaver saver(debug); debug << "type:" << item.d_ptr->type << "func:" << item.d_ptr->func; if (item.d_ptr->config) { diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index 78334563dd..f589777e65 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -3583,10 +3583,12 @@ QString QLocale::nativeCountryName() const #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QLocale &l) { - dbg.nospace() << "QLocale(" << qPrintable(QLocale::languageToString(l.language())) - << ", " << qPrintable(QLocale::scriptToString(l.script())) - << ", " << qPrintable(QLocale::countryToString(l.country())) << ')'; - return dbg.space(); + QDebugStateSaver saver(dbg); + dbg.nospace().noquote() + << "QLocale(" << QLocale::languageToString(l.language()) + << ", " << QLocale::scriptToString(l.script()) + << ", " << QLocale::countryToString(l.country()) << ')'; + return dbg; } #endif QT_END_NAMESPACE diff --git a/src/corelib/tools/qmargins.cpp b/src/corelib/tools/qmargins.cpp index 265e44bfcf..b91e57ce39 100644 --- a/src/corelib/tools/qmargins.cpp +++ b/src/corelib/tools/qmargins.cpp @@ -432,9 +432,10 @@ QDataStream &operator>>(QDataStream &s, QMargins &m) #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QMargins &m) { + QDebugStateSaver saver(dbg); dbg.nospace() << "QMargins(" << m.left() << ", " << m.top() << ", " << m.right() << ", " << m.bottom() << ')'; - return dbg.space(); + return dbg; } #endif @@ -764,9 +765,10 @@ QDataStream &operator>>(QDataStream &s, QMarginsF &m) #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QMarginsF &m) { + QDebugStateSaver saver(dbg); dbg.nospace() << "QMarginsF(" << m.left() << ", " << m.top() << ", " << m.right() << ", " << m.bottom() << ')'; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/tools/qregexp.cpp b/src/corelib/tools/qregexp.cpp index 71d897082b..e0ee8b12db 100644 --- a/src/corelib/tools/qregexp.cpp +++ b/src/corelib/tools/qregexp.cpp @@ -4577,9 +4577,10 @@ QDataStream &operator>>(QDataStream &in, QRegExp ®Exp) #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QRegExp &r) { + QDebugStateSaver saver(dbg); dbg.nospace() << "QRegExp(patternSyntax=" << r.patternSyntax() << ", pattern='"<< r.pattern() << "')"; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/tools/qregularexpression.cpp b/src/corelib/tools/qregularexpression.cpp index 228ee5b842..2d469414ee 100644 --- a/src/corelib/tools/qregularexpression.cpp +++ b/src/corelib/tools/qregularexpression.cpp @@ -2444,8 +2444,9 @@ QDataStream &operator>>(QDataStream &in, QRegularExpression &re) */ QDebug operator<<(QDebug debug, const QRegularExpression &re) { + QDebugStateSaver saver(debug); 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) { + QDebugStateSaver saver(debug); QByteArray flags; if (patternOptions == QRegularExpression::NoPatternOption) { @@ -2487,7 +2489,7 @@ QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOption debug.nospace() << "QRegularExpression::PatternOptions(" << flags << ")"; - return debug.space(); + return debug; } /*! \relates QRegularExpressionMatch @@ -2499,11 +2501,12 @@ QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOption */ QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match) { + QDebugStateSaver saver(debug); debug.nospace() << "QRegularExpressionMatch("; if (!match.isValid()) { debug << "Invalid)"; - return debug.space(); + return debug; } debug << "Valid"; @@ -2528,7 +2531,7 @@ QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match) debug << ")"; - return debug.space(); + return debug; } #endif diff --git a/src/corelib/tools/qtimezone.cpp b/src/corelib/tools/qtimezone.cpp index 30c4c60167..f11a574c6f 100644 --- a/src/corelib/tools/qtimezone.cpp +++ b/src/corelib/tools/qtimezone.cpp @@ -980,9 +980,10 @@ QDataStream &operator>>(QDataStream &ds, QTimeZone &tz) #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QTimeZone &tz) { + QDebugStateSaver saver(dbg); //TODO Include backend and data version details? dbg.nospace() << "QTimeZone(" << QString::fromUtf8(tz.id()) << ')'; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/tools/qversionnumber.cpp b/src/corelib/tools/qversionnumber.cpp index 7817cabe33..40f6004514 100644 --- a/src/corelib/tools/qversionnumber.cpp +++ b/src/corelib/tools/qversionnumber.cpp @@ -458,8 +458,9 @@ QDataStream& operator>>(QDataStream &in, QVersionNumber &version) #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug debug, const QVersionNumber &version) { + QDebugStateSaver saver(debug); debug.noquote() << version.toString(); - return debug.quote(); + return debug; } #endif diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index 997ceaf7b9..e525cd669d 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -2204,13 +2204,13 @@ void tst_QtJson::testDebugStream() array.append(1); array.append(QLatin1String("foo")); value = QJsonValue(array); // array - QTest::ignoreMessage(QtDebugMsg, "QJsonValue(array, QJsonArray([1,\"foo\"]) )"); + QTest::ignoreMessage(QtDebugMsg, "QJsonValue(array, QJsonArray([1,\"foo\"]))"); qDebug() << value; QJsonObject object; object.insert(QLatin1String("foo"), QLatin1String("bar")); value = QJsonValue(object); // object - QTest::ignoreMessage(QtDebugMsg, "QJsonValue(object, QJsonObject({\"foo\":\"bar\"}) )"); + QTest::ignoreMessage(QtDebugMsg, "QJsonValue(object, QJsonObject({\"foo\":\"bar\"}))"); qDebug() << value; } } -- cgit v1.2.3