diff options
author | Kai Koehne <kai.koehne@digia.com> | 2014-06-19 11:09:47 +0200 |
---|---|---|
committer | Kai Koehne <kai.koehne@digia.com> | 2014-07-10 09:27:44 +0200 |
commit | 6d166c88220ee09821b65fb2b711fa77a5312971 (patch) | |
tree | 78f69bc74ba66f8a7d8a97db4cae8fd21b70992e /src/corelib/io/qdebug.h | |
parent | 27797cb37a63eceed77e9b6c790eaaf9c438b360 (diff) |
Add QDebug::noquote() stream modifier
Allow the user to disable the quoting of QString, QByteArray,
QStringLiteral by passing a "noquote()" stream modifier.
This requires another flag to be added to QDebug::Stream. To
keep BC we're using the QMessageLogContext::version field
to differentiate between QDebug streams created by earlier
versions.
Task-number: QTBUG-37146
Change-Id: I9b215eabfcfd754af16ea87f3ef928d698e37d77
Reviewed-by: Jerome Pasion <jerome.pasion@digia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io/qdebug.h')
-rw-r--r-- | src/corelib/io/qdebug.h | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 584d6bf41f..0bbb617c1f 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -61,9 +61,9 @@ 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) {} - Stream(QString *string) : ts(string, QIODevice::WriteOnly), ref(1), type(QtDebugMsg), space(true), message_output(false) {} - Stream(QtMsgType t) : ts(&buffer, QIODevice::WriteOnly), ref(1), type(t), space(true), message_output(true) {} + 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) {} QTextStream ts; QString buffer; int ref; @@ -71,6 +71,18 @@ class Q_CORE_EXPORT QDebug bool space; bool message_output; QMessageLogContext context; + + enum FormatFlag { + NoQuotes = 0x1 + }; + + // ### Qt 6: unify with space, introduce own version member + 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; } } + + // added in 5.4 + int flags; } *stream; public: inline QDebug(QIODevice *device) : stream(new Stream(device)) {} @@ -88,7 +100,11 @@ public: bool autoInsertSpaces() const { return stream->space; } void setAutoInsertSpaces(bool b) { stream->space = b; } - inline QDebug &operator<<(QChar t) { stream->ts << '\'' << t << '\''; return maybeSpace(); } + inline QDebug "e() { stream->unsetFlag(Stream::NoQuotes); return *this; } + inline QDebug &noquote() { stream->setFlag(Stream::NoQuotes); return *this; } + inline QDebug &maybeQuote(char c = '"') { if (!(stream->testFlag(Stream::NoQuotes))) stream->ts << c; return *this; } + + inline QDebug &operator<<(QChar t) { maybeQuote('\''); stream->ts << t; maybeQuote('\''); return maybeSpace(); } inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); } inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); } @@ -102,10 +118,10 @@ public: inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(const char* t) { stream->ts << QString::fromUtf8(t); return maybeSpace(); } - inline QDebug &operator<<(const QString & t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); } + inline QDebug &operator<<(const QString & t) { maybeQuote(); stream->ts << t; maybeQuote(); return maybeSpace(); } inline QDebug &operator<<(const QStringRef & t) { return operator<<(t.toString()); } - inline QDebug &operator<<(QLatin1String t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); } - inline QDebug &operator<<(const QByteArray & t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); } + inline QDebug &operator<<(QLatin1String t) { maybeQuote(); stream->ts << t; maybeQuote(); return maybeSpace(); } + inline QDebug &operator<<(const QByteArray & t) { maybeQuote(); stream->ts << t; maybeQuote(); return maybeSpace(); } inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(QTextStreamFunction f) { stream->ts << f; @@ -137,6 +153,9 @@ public: inline QNoDebug &space() { return *this; } inline QNoDebug &nospace() { return *this; } inline QNoDebug &maybeSpace() { return *this; } + inline QNoDebug "e() { return *this; } + inline QNoDebug &noquote() { return *this; } + inline QNoDebug &maybeQuote(const char = '"') { return *this; } template<typename T> inline QNoDebug &operator<<(const T &) { return *this; } |