diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2021-05-26 18:05:36 +0300 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2021-05-31 21:50:07 +0300 |
commit | 86542054d035c43f926eeb96b517108eb825831e (patch) | |
tree | 10029d15b5a7ada0f856c8d09c8b048c41520bd0 /src/corelib/io | |
parent | 59a0539690f8fb5b97d9d2241167cd5fac236950 (diff) |
Consolidate debug string generation
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 <oswald.buddenhagen@gmx.de>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/io')
-rw-r--r-- | src/corelib/io/qdebug.cpp | 48 | ||||
-rw-r--r-- | src/corelib/io/qdebug_p.h | 3 | ||||
-rw-r--r-- | src/corelib/io/qprocess.cpp | 45 | ||||
-rw-r--r-- | src/corelib/io/qprocess_unix.cpp | 50 |
4 files changed, 59 insertions, 87 deletions
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 <private/qtextstream_p.h> #include <private/qtools_p.h> +#include <ctype.h> 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. /*! diff --git a/src/corelib/io/qdebug_p.h b/src/corelib/io/qdebug_p.h index dcb906d156..1ca632338f 100644 --- a/src/corelib/io/qdebug_p.h +++ b/src/corelib/io/qdebug_p.h @@ -55,11 +55,14 @@ #include "QtCore/qdebug.h" #include "QtCore/qmetaobject.h" #include "QtCore/qflags.h" +#include "QtCore/qbytearray.h" QT_BEGIN_NAMESPACE namespace QtDebugUtils { +Q_CORE_EXPORT QByteArray toPrintable(const char *data, int len, int maxSize); + // inline helpers for formatting basic classes. template <class Point> diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp index 745c88e726..16fb2be0ae 100644 --- a/src/corelib/io/qprocess.cpp +++ b/src/corelib/io/qprocess.cpp @@ -41,49 +41,12 @@ //#define QPROCESS_DEBUG #include <qdebug.h> +#include <private/qdebug_p.h> #include <qdir.h> #include <qscopedvaluerollback.h> #if defined(Q_OS_WIN) #include <qtimer.h> #endif -#if defined QPROCESS_DEBUG -#include <qstring.h> -#include <ctype.h> - -QT_BEGIN_NAMESPACE -/* - Returns a human readable representation of the first \a len - characters in \a data. -*/ -static QByteArray qt_prettyDebug(const char *data, int len, int maxSize) -{ - if (!data) return "(null)"; - QByteArray out; - for (int i = 0; i < len && i < 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: - char buf[5]; - qsnprintf(buf, sizeof(buf), "\\%3o", c); - buf[4] = '\0'; - out += QByteArray(buf); - } - } - - if (len < maxSize) - out += "..."; - - return out; -} - -QT_END_NAMESPACE - -#endif #include "qprocess.h" #include "qprocess_p.h" @@ -1940,8 +1903,8 @@ qint64 QProcess::writeData(const char *data, qint64 len) if (d->stdinChannel.closed) { #if defined QPROCESS_DEBUG - qDebug("QProcess::writeData(%p \"%s\", %lld) == 0 (write channel closing)", - data, qt_prettyDebug(data, len, 16).constData(), len); + qDebug("QProcess::writeData(%p \"%s\", %lld) == 0 (write channel closing)", + data, QtDebugUtils::toPrintable(data, len, 16).constData(), len); #endif return 0; } @@ -1965,7 +1928,7 @@ qint64 QProcess::writeData(const char *data, qint64 len) #endif #if defined QPROCESS_DEBUG qDebug("QProcess::writeData(%p \"%s\", %lld) == %lld (written to buffer)", - data, qt_prettyDebug(data, len, 16).constData(), len, len); + data, QtDebugUtils::toPrintable(data, len, 16).constData(), len, len); #endif return len; } diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp index 79e0cbd1c0..4fa36238a7 100644 --- a/src/corelib/io/qprocess_unix.cpp +++ b/src/corelib/io/qprocess_unix.cpp @@ -41,49 +41,7 @@ //#define QPROCESS_DEBUG #include "qdebug.h" - -#if QT_CONFIG(process) && defined(QPROCESS_DEBUG) -#include "private/qtools_p.h" -#include <ctype.h> - -/* - Returns a human readable representation of the first \a len - characters in \a data. -*/ -QT_BEGIN_NAMESPACE -static QByteArray qt_prettyDebug(const char *data, int len, int maxSize) -{ - if (!data) return "(null)"; - QByteArray out; - for (int i = 0; i < len; ++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[] = { - '\\', - QtMiscUtils::toOct(uchar(c) / 64), - QtMiscUtils::toOct(uchar(c) % 64 / 8), - QtMiscUtils::toOct(uchar(c) % 8), - 0 - }; - out += buf; - } - } - } - - if (len < maxSize) - out += "..."; - - return out; -} -QT_END_NAMESPACE -#endif - +#include <private/qdebug_p.h> #include "qplatformdefs.h" #include "qprocess.h" @@ -676,7 +634,7 @@ qint64 QProcessPrivate::readFromChannel(const Channel *channel, char *data, qint int save_errno = errno; qDebug("QProcessPrivate::readFromChannel(%d, %p \"%s\", %lld) == %lld", int(channel - &stdinChannel), - data, qt_prettyDebug(data, bytesRead, 16).constData(), maxlen, bytesRead); + data, QtDebugUtils::toPrintable(data, bytesRead, 16).constData(), maxlen, bytesRead); errno = save_errno; #endif if (bytesRead == -1 && errno == EWOULDBLOCK) @@ -691,8 +649,8 @@ bool QProcessPrivate::writeToStdin() qint64 written = qt_safe_write_nosignal(stdinChannel.pipe[1], data, bytesToWrite); #if defined QPROCESS_DEBUG - qDebug("QProcessPrivate::writeToStdin(), write(%p \"%s\", %lld) == %lld", - data, qt_prettyDebug(data, bytesToWrite, 16).constData(), bytesToWrite, written); + qDebug("QProcessPrivate::writeToStdin(), write(%p \"%s\", %lld) == %lld", data, + QtDebugUtils::toPrintable(data, bytesToWrite, 16).constData(), bytesToWrite, written); if (written == -1) qDebug("QProcessPrivate::writeToStdin(), failed to write (%ls)", qUtf16Printable(qt_error_string(errno))); #endif |