QDebugStateSaver: call maybeSpace() in destructor.

tst_qdebug didn't test adding something else after the MyLine object,
so I didn't realize that a space was missing there. All debug operators
should end with maybeSpace(), but with the settings of the caller, so this
requires restoring the settings before calling it. To make it convenient
for all << operators, the destructor of QDebugStateSaver takes care of that.

Change-Id: I18ab78d99d7ee3be951082b5b5d34718ee60e21d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2014-02-07 16:12:35 +01:00 committed by The Qt Project
parent 73d761ffe5
commit 91c9cae720
7 changed files with 48 additions and 23 deletions

View File

@ -334,7 +334,9 @@ QT_BEGIN_NAMESPACE
\brief Convenience class for custom QDebug operators
Saves the settings used by QDebug, and restores them upon destruction.
Saves the settings used by QDebug, and restores them upon destruction,
then calls maybeSpace(), to separate arguments with a space if
autoInsertSpaces() was true at the time of constructing the QDebugStateSaver.
The automatic insertion of spaces between writes is one of the settings
that QDebugStateSaver stores for the duration of the current block.
@ -391,6 +393,7 @@ QDebugStateSaver::QDebugStateSaver(QDebug &dbg)
QDebugStateSaver::~QDebugStateSaver()
{
d->restoreState();
d->m_dbg.maybeSpace();
}
QT_END_NAMESPACE

View File

@ -259,10 +259,11 @@ QT_BEGIN_NAMESPACE
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug d, const QLine &p)
QDebug operator<<(QDebug dbg, const QLine &p)
{
d << "QLine(" << p.p1() << ',' << p.p2() << ')';
return d;
QDebugStateSaver saver(dbg);
dbg.nospace() << "QLine(" << p.p1() << ',' << p.p2() << ')';
return dbg;
}
#endif
@ -819,10 +820,11 @@ qreal QLineF::angle(const QLineF &l) const
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug d, const QLineF &p)
QDebug operator<<(QDebug dbg, const QLineF &p)
{
d << "QLineF(" << p.p1() << ',' << p.p2() << ')';
return d;
QDebugStateSaver saver(dbg);
dbg.nospace() << "QLineF(" << p.p1() << ',' << p.p2() << ')';
return dbg;
}
#endif

View File

@ -451,15 +451,18 @@ QDataStream &operator>>(QDataStream &s, QPoint &p)
*/
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QPoint &p) {
QDebug operator<<(QDebug dbg, const QPoint &p)
{
QDebugStateSaver saver(dbg);
dbg.nospace() << "QPoint(" << p.x() << ',' << p.y() << ')';
return dbg.space();
return dbg;
}
QDebug operator<<(QDebug d, const QPointF &p)
QDebug operator<<(QDebug dbg, const QPointF &p)
{
d.nospace() << "QPointF(" << p.x() << ", " << p.y() << ')';
return d.space();
QDebugStateSaver saver(dbg);
dbg.nospace() << "QPointF(" << p.x() << ',' << p.y() << ')';
return dbg;
}
#endif

View File

@ -1285,10 +1285,12 @@ QDataStream &operator>>(QDataStream &s, QRect &r)
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QRect &r) {
QDebug operator<<(QDebug dbg, const QRect &r)
{
QDebugStateSaver saver(dbg);
dbg.nospace() << "QRect(" << r.x() << ',' << r.y() << ' '
<< r.width() << 'x' << r.height() << ')';
return dbg.space();
return dbg;
}
#endif
@ -2491,10 +2493,12 @@ QDataStream &operator>>(QDataStream &s, QRectF &r)
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QRectF &r) {
QDebug operator<<(QDebug dbg, const QRectF &r)
{
QDebugStateSaver saver(dbg);
dbg.nospace() << "QRectF(" << r.x() << ',' << r.y() << ' '
<< r.width() << 'x' << r.height() << ')';
return dbg.space();
return dbg;
}
#endif

View File

@ -445,9 +445,11 @@ QDataStream &operator>>(QDataStream &s, QSize &sz)
#endif // QT_NO_DATASTREAM
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QSize &s) {
QDebug operator<<(QDebug dbg, const QSize &s)
{
QDebugStateSaver saver(dbg);
dbg.nospace() << "QSize(" << s.width() << ", " << s.height() << ')';
return dbg.space();
return dbg;
}
#endif
@ -870,9 +872,11 @@ QDataStream &operator>>(QDataStream &s, QSizeF &sz)
#endif // QT_NO_DATASTREAM
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QSizeF &s) {
QDebug operator<<(QDebug dbg, const QSizeF &s)
{
QDebugStateSaver saver(dbg);
dbg.nospace() << "QSizeF(" << s.width() << ", " << s.height() << ')';
return dbg.space();
return dbg;
}
#endif

View File

@ -61,6 +61,7 @@ SOURCES += \
../../corelib/io/qabstractfileengine.cpp \
../../corelib/io/qbuffer.cpp \
../../corelib/io/qdatastream.cpp \
../../corelib/io/qdebug.cpp \
../../corelib/io/qdir.cpp \
../../corelib/io/qdiriterator.cpp \
../../corelib/io/qfile.cpp \

View File

@ -163,7 +163,8 @@ public:
QDebug operator<< (QDebug s, const MyPoint& point)
{
const QDebugStateSaver saver(s);
return s.nospace() << "MyPoint(" << point.v1 << ", " << point.v2 << ")";
s.nospace() << "MyPoint(" << point.v1 << ", " << point.v2 << ")";
return s;
}
class MyLine
@ -199,10 +200,17 @@ void tst_QDebug::debugSpaceHandling() const
d << 1 << 2;
MyLine line(MyPoint(10, 11), MyPoint (12, 13));
d << line;
d << "bar";
// With the old implementation of MyPoint doing dbg.nospace() << ...; dbg.space() we ended up with
// MyLine(MyPoint(10, 11) , MyPoint(12, 13) )
}
QCOMPARE(s_msg, QString::fromLatin1(" foo key=value 1 2 MyLine(MyPoint(10, 11), MyPoint(12, 13))"));
QCOMPARE(s_msg, QString::fromLatin1(" foo key=value 1 2 MyLine(MyPoint(10, 11), MyPoint(12, 13)) bar"));
QVERIFY(qDebug().autoInsertSpaces());
qDebug() << QPoint(21, 22) << QRect(23, 24, 25, 26) << QLine(27, 28, 29, 30);
QCOMPARE(s_msg, QString::fromLatin1("QPoint(21,22) QRect(23,24 25x26) QLine(QPoint(27,28),QPoint(29,30))"));
qDebug() << QPointF(21, 22) << QRectF(23, 24, 25, 26) << QLineF(27, 28, 29, 30);
QCOMPARE(s_msg, QString::fromLatin1("QPointF(21,22) QRectF(23,24 25x26) QLineF(QPointF(27,28),QPointF(29,30))"));
}
void tst_QDebug::stateSaver() const
@ -214,7 +222,7 @@ void tst_QDebug::stateSaver() const
QDebugStateSaver saver(d);
d.nospace() << hex << right << qSetFieldWidth(3) << qSetPadChar('0') << 42;
}
d.space() << 42;
d << 42;
}
QCOMPARE(s_msg, QString::fromLatin1("02a 42"));
}