diff options
author | Friedemann Kleint <Friedemann.Kleint@theqtcompany.com> | 2015-02-23 10:06:48 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@theqtcompany.com> | 2015-03-13 13:24:36 +0000 |
commit | 3994ea1979249e0bf76eb38c89ff3d3dfb1d90b5 (patch) | |
tree | 8abe0184585c05905e3fb9a247ff58e5c775dbb9 | |
parent | d2f418e98f81da5130a69e6662a3968e63e5f077 (diff) |
Add a verbosity setting to QDebug.
Add setters for an int verbosity to QDebug. The streaming operators
can check on the setting and output more information accordingly.
[ChangeLog][QtCore][QDebug] How verbose a single debug output should be
can now be fine-tuned by setting a verbosity on the debug stream.
Change-Id: I77001fcf1ef090a580d1a137bb5a667fc1bf1e1b
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
-rw-r--r-- | src/corelib/io/qdebug.cpp | 27 | ||||
-rw-r--r-- | src/corelib/io/qdebug.h | 25 | ||||
-rw-r--r-- | tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 34 |
3 files changed, 80 insertions, 6 deletions
diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index 357d63137c..8676fe0259 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -334,6 +334,7 @@ QDebug &QDebug::resetFormat() stream->space = true; if (stream->context.version > 1) stream->flags = 0; + stream->setVerbosity(Stream::defaultVerbosity); return *this; } @@ -424,6 +425,32 @@ QDebug &QDebug::resetFormat() */ /*! + \fn int QDebug::verbosity() const + \since 5.6 + + Returns the verbosity of the debug stream. + + Streaming operators can check the value to decide whether + verbose output is desired and print more information depending on the + level. Higher values indicate that more information is desired. + + The allowed range is from 0 to 7. The default value is 2. + + \sa setVerbosity() +*/ + +/*! + \fn void QDebug::setVerbosity(int verbosityLevel) + \since 5.6 + + Sets the verbosity of the stream to \a verbosityLevel. + + The allowed range is from 0 to 7. The default value is 2. + + \sa verbosity() +*/ + +/*! \fn QDebug &QDebug::operator<<(QChar t) Writes the character, \a t, to the stream and returns a reference to the diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 7f1cb82f3a..95f5993e4f 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -53,9 +53,14 @@ class Q_CORE_EXPORT QDebug friend class QMessageLogger; friend class QDebugStateSaverPrivate; struct Stream { - Stream(QIODevice *device) : ts(device), ref(1), type(QtDebugMsg), space(true), message_output(false), flags(0) {} - Stream(QString *string) : ts(string, QIODevice::WriteOnly), ref(1), type(QtDebugMsg), space(true), message_output(false), flags(0) {} - Stream(QtMsgType t) : ts(&buffer, QIODevice::WriteOnly), ref(1), type(t), space(true), message_output(true), flags(0) {} + enum { defaultVerbosity = 2, verbosityShift = 29, verbosityMask = 0x7 }; + + Stream(QIODevice *device) : ts(device), ref(1), type(QtDebugMsg), + space(true), message_output(false), flags(defaultVerbosity << verbosityShift) {} + Stream(QString *string) : ts(string, QIODevice::WriteOnly), ref(1), type(QtDebugMsg), + space(true), message_output(false), flags(defaultVerbosity << verbosityShift) {} + Stream(QtMsgType t) : ts(&buffer, QIODevice::WriteOnly), ref(1), type(t), + space(true), message_output(true), flags(defaultVerbosity << verbosityShift) {} QTextStream ts; QString buffer; int ref; @@ -64,7 +69,7 @@ class Q_CORE_EXPORT QDebug bool message_output; QMessageLogContext context; - enum FormatFlag { + enum FormatFlag { // Note: Bits 29..31 are reserved for the verbose level introduced in 5.6. NoQuotes = 0x1 }; @@ -72,7 +77,15 @@ class Q_CORE_EXPORT QDebug bool testFlag(FormatFlag flag) const { return (context.version > 1) ? (flags & flag) : false; } void setFlag(FormatFlag flag) { if (context.version > 1) { flags |= flag; } } void unsetFlag(FormatFlag flag) { if (context.version > 1) { flags &= ~flag; } } - + int verbosity() const + { return context.version > 1 ? (flags >> verbosityShift) & verbosityMask : int(Stream::defaultVerbosity); } + void setVerbosity(int v) + { + if (context.version > 1) { + flags &= ~(verbosityMask << verbosityShift); + flags |= (v & verbosityMask) << verbosityShift; + } + } // added in 5.4 int flags; } *stream; @@ -96,6 +109,8 @@ public: inline QDebug &space() { stream->space = true; stream->ts << ' '; return *this; } inline QDebug &nospace() { stream->space = false; return *this; } inline QDebug &maybeSpace() { if (stream->space) stream->ts << ' '; return *this; } + int verbosity() const { return stream->verbosity(); } + void setVerbosity(int verbosityLevel) { stream->setVerbosity(verbosityLevel); } bool autoInsertSpaces() const { return stream->space; } void setAutoInsertSpaces(bool b) { stream->space = b; } diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 32344f1e26..1b7d410beb 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -49,6 +49,7 @@ private slots: void debugWithBool() const; void debugSpaceHandling() const; void debugNoQuotes() const; + void verbosity() const; void stateSaver() const; void veryLongWarningMessage() const; void qDebugQChar() const; @@ -192,7 +193,11 @@ public: QDebug operator<< (QDebug s, const MyLine& line) { const QDebugStateSaver saver(s); - s.nospace() << "MyLine(" << line.p1 << ", " << line.p2 << ")"; + s.nospace(); + s << "MyLine(" << line.p1 << ", "<< line.p2; + if (s.verbosity() > 2) + s << ", Manhattan length=" << (qAbs(line.p2.v1 - line.p1.v1) + qAbs(line.p2.v2 - line.p1.v2)); + s << ')'; return s; } @@ -255,6 +260,33 @@ void tst_QDebug::debugNoQuotes() const QCOMPARE(s_msg, QString::fromLatin1("'H' \"Hello\" \"Hello\" H Hello Hello")); } +void tst_QDebug::verbosity() const +{ + MyLine line(MyPoint(10, 11), MyPoint (12, 13)); + QString output; + QDebug d(&output); + d.nospace(); + d << line << '\n'; + const int oldVerbosity = d.verbosity(); + d.setVerbosity(0); + QCOMPARE(d.verbosity(), 0); + d.setVerbosity(7); + QCOMPARE(d.verbosity(), 7); + const int newVerbosity = oldVerbosity + 2; + d.setVerbosity(newVerbosity); + QCOMPARE(d.verbosity(), newVerbosity); + d << line << '\n'; + d.setVerbosity(oldVerbosity ); + QCOMPARE(d.verbosity(), oldVerbosity ); + d << line; + const QStringList lines = output.split(QLatin1Char('\n')); + QCOMPARE(lines.size(), 3); + // Verbose should be longer + QVERIFY2(lines.at(1).size() > lines.at(0).size(), qPrintable(lines.join(QLatin1Char(',')))); + // Switching back to brief produces same output + QCOMPARE(lines.at(0).size(), lines.at(2).size()); +} + void tst_QDebug::stateSaver() const { MessageHandlerSetter mhs(myMessageHandler); |