From 86542054d035c43f926eeb96b517108eb825831e Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Wed, 26 May 2021 18:05:36 +0300 Subject: Consolidate debug string generation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Several QIODevice subclasses use the qt_prettyDebug() function to get a printable representation of the buffer data for debug output. Rather than having this feature statically implemented in each respective file, this patch introduces a generic function in the QtDebugUtils namespace. Accordingly, some inaccuracies in the use-cases have been corrected. Change-Id: I1a8465cab08c8acf5fdcdba5085182511b1cbb7b Reviewed-by: Oswald Buddenhagen Reviewed-by: MÃ¥rten Nordheim --- src/corelib/io/qdebug.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src/corelib/io/qdebug.cpp') diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index b1ebf097cf..0a7a847ef6 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -46,15 +46,63 @@ #endif #include "qdebug.h" +#include "private/qdebug_p.h" #include "qmetaobject.h" #include #include +#include QT_BEGIN_NAMESPACE using QtMiscUtils::toHexUpper; +using QtMiscUtils::toHexLower; using QtMiscUtils::fromHex; +/* + Returns a human readable representation of the first \a maxSize + characters in \a data. +*/ +QByteArray QtDebugUtils::toPrintable(const char *data, int len, int maxSize) +{ + if (!data) + return "(null)"; + + QByteArray out; + for (int i = 0; i < qMin(len, maxSize); ++i) { + char c = data[i]; + if (isprint(c)) { + out += c; + } else { + switch (c) { + case '\n': + out += "\\n"; + break; + case '\r': + out += "\\r"; + break; + case '\t': + out += "\\t"; + break; + default: { + const char buf[] = { + '\\', + 'x', + toHexLower(uchar(c) / 16), + toHexLower(uchar(c) % 16), + 0 + }; + out += buf; + } + } + } + } + + if (maxSize < len) + out += "..."; + + return out; +} + // This file is needed to force compilation of QDebug into the kernel library. /*! -- cgit v1.2.3