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>
This commit is contained in:
Friedemann Kleint 2015-02-23 10:06:48 +01:00
parent d2f418e98f
commit 3994ea1979
3 changed files with 80 additions and 6 deletions

View File

@ -334,6 +334,7 @@ QDebug &QDebug::resetFormat()
stream->space = true;
if (stream->context.version > 1)
stream->flags = 0;
stream->setVerbosity(Stream::defaultVerbosity);
return *this;
}
@ -423,6 +424,32 @@ QDebug &QDebug::resetFormat()
\sa quote(), noquote()
*/
/*!
\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)

View File

@ -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; }

View File

@ -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);