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>
This commit is contained in:
Alex Trotsenko 2021-05-26 18:05:36 +03:00
parent 59a0539690
commit 86542054d0
8 changed files with 83 additions and 257 deletions

View File

@ -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.
/*!

View File

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

View File

@ -41,48 +41,11 @@
//#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"
@ -1941,7 +1904,7 @@ 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);
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;
}

View File

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

View File

@ -230,6 +230,7 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384;
#include "qfile.h"
#include "qnumeric.h"
#include "qvarlengtharray.h"
#include <private/qdebug_p.h>
#include <locale.h>
#include "private/qlocale_p.h"
@ -239,48 +240,6 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384;
#include <limits.h>
#include <new>
#if defined QTEXTSTREAM_DEBUG
#include <ctype.h>
#include "private/qtools_p.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) {
char c = data[i];
if (isprint(int(uchar(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',
QtMiscUtils::toHexLower(uchar(c) / 16),
QtMiscUtils::toHexLower(uchar(c) % 16),
0
};
out += buf;
}
}
}
if (len < maxSize)
out += "...";
return out;
}
QT_END_NAMESPACE
#endif
// A precondition macro
#define Q_VOID
#define CHECK_VALID_STREAM(x) do { \
@ -448,7 +407,7 @@ bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes)
#if defined (QTEXTSTREAM_DEBUG)
qDebug("QTextStreamPrivate::fillReadBuffer(), device->read(\"%s\", %d) == %d",
qt_prettyDebug(buf, qMin(32,int(bytesRead)) , int(bytesRead)).constData(), int(sizeof(buf)), int(bytesRead));
QtDebugUtils::toPrintable(buf, bytesRead, 32).constData(), int(sizeof(buf)), int(bytesRead));
#endif
int oldReadBufferSize = readBuffer.size();
@ -486,7 +445,7 @@ bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes)
#if defined (QTEXTSTREAM_DEBUG)
qDebug("QTextStreamPrivate::fillReadBuffer() read %d bytes from device. readBuffer = [%s]", int(bytesRead),
qt_prettyDebug(readBuffer.toLatin1(), readBuffer.size(), readBuffer.size()).data());
QtDebugUtils::toPrintable(readBuffer.toLatin1(), readBuffer.size(), readBuffer.size()).constData());
#endif
return true;
}
@ -536,7 +495,7 @@ void QTextStreamPrivate::flushWriteBuffer()
qint64 bytesWritten = device->write(data);
#if defined (QTEXTSTREAM_DEBUG)
qDebug("QTextStreamPrivate::flushWriteBuffer(), device->write(\"%s\") == %d",
qt_prettyDebug(data.constData(), qMin(data.size(),32), data.size()).constData(), int(bytesWritten));
QtDebugUtils::toPrintable(data.constData(), data.size(), 32).constData(), int(bytesWritten));
#endif
#if defined (Q_OS_WIN)

View File

@ -483,6 +483,7 @@
#ifdef QABSTRACTSOCKET_DEBUG
#include <qdebug.h>
#include <private/qdebug_p.h>
#endif
#include <time.h>
@ -501,42 +502,6 @@ QT_BEGIN_NAMESPACE
static const int DefaultConnectTimeout = 30000;
#if defined QABSTRACTSOCKET_DEBUG
QT_BEGIN_INCLUDE_NAMESPACE
#include <qstring.h>
#include <ctype.h>
QT_END_INCLUDE_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 maxLength)
{
if (!data) return "(null)";
QByteArray out;
for (int i = 0; i < qMin(len, maxLength); ++i) {
char c = data[i];
if (isprint(int(uchar(c)))) {
out += c;
} else switch (c) {
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
QString tmp;
tmp.sprintf("\\%o", c);
out += tmp.toLatin1();
}
}
if (len < maxLength)
out += "...";
return out;
}
#endif
static bool isProxyError(QAbstractSocket::SocketError error)
{
switch (error) {
@ -2459,9 +2424,8 @@ qint64 QAbstractSocket::readData(char *data, qint64 maxSize)
}
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocket::readData(%p \"%s\", %lli) == %lld [engine]",
data, qt_prettyDebug(data, 32, readBytes).data(), maxSize,
readBytes);
qDebug("QAbstractSocket::readData(%p \"%s\", %lli) == %lld [engine]", data,
QtDebugUtils::toPrintable(data, readBytes, 32).constData(), maxSize, readBytes);
#endif
return readBytes;
}
@ -2499,8 +2463,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocket::writeData(%p \"%s\", %lli) == %lli", data,
qt_prettyDebug(data, qMin((int)size, 32), size).data(),
size, written);
QtDebugUtils::toPrintable(data, size, 32).constData(), size, written);
#endif
return written; // written = actually written + what has been buffered
} else if (!d->isBuffered && d->socketType != TcpSocket) {
@ -2511,8 +2474,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocket::writeData(%p \"%s\", %lli) == %lli", data,
qt_prettyDebug(data, qMin((int)size, 32), size).data(),
size, written);
QtDebugUtils::toPrintable(data, size, 32).constData(), size, written);
#endif
if (written >= 0)
d->emitBytesWritten(written);
@ -2533,8 +2495,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocket::writeData(%p \"%s\", %lli) == %lli", data,
qt_prettyDebug(data, qMin((int)size, 32), size).data(),
size, written);
QtDebugUtils::toPrintable(data, size, 32).constData(), size, written);
#endif
return written;
}

View File

@ -63,8 +63,7 @@
#endif
#if defined QNATIVESOCKETENGINE_DEBUG
#include <qstring.h>
#include <ctype.h>
#include <private/qdebug_p.h>
#endif
#include <netinet/tcp.h>
@ -76,38 +75,6 @@
QT_BEGIN_NAMESPACE
#if defined QNATIVESOCKETENGINE_DEBUG
/*
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) {
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:
QString tmp;
tmp.sprintf("\\%o", c);
out += tmp.toLatin1();
}
}
if (len < maxSize)
out += "...";
return out;
}
#endif
/*
Extracts the port and address from a sockaddr, and stores them in
\a port and \a addr if they are non-null.
@ -1063,7 +1030,7 @@ qint64 QNativeSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 maxS
#if defined (QNATIVESOCKETENGINE_DEBUG)
qDebug("QNativeSocketEnginePrivate::nativeReceiveDatagram(%p \"%s\", %lli, %s, %i) == %lli",
data, qt_prettyDebug(data, qMin(recvResult, ssize_t(16)), recvResult).data(), maxSize,
data, QtDebugUtils::toPrintable(data, recvResult, 16).constData(), maxSize,
(recvResult != -1 && options != QAbstractSocketEngine::WantNone)
? header->senderAddress.toString().toLatin1().constData() : "(unknown)",
(recvResult != -1 && options != QAbstractSocketEngine::WantNone)
@ -1192,7 +1159,7 @@ qint64 QNativeSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 l
#if defined (QNATIVESOCKETENGINE_DEBUG)
qDebug("QNativeSocketEngine::sendDatagram(%p \"%s\", %lli, \"%s\", %i) == %lli", data,
qt_prettyDebug(data, qMin<int>(len, 16), len).data(), len,
QtDebugUtils::toPrintable(data, len, 16).constData(), len,
header.destinationAddress.toString().toLatin1().constData(),
header.destinationPort, (qint64) sentBytes);
#endif
@ -1358,9 +1325,8 @@ qint64 QNativeSocketEnginePrivate::nativeWrite(const char *data, qint64 len)
}
#if defined (QNATIVESOCKETENGINE_DEBUG)
qDebug("QNativeSocketEnginePrivate::nativeWrite(%p \"%s\", %llu) == %i",
data, qt_prettyDebug(data, qMin((int) len, 16),
(int) len).data(), len, (int) writtenBytes);
qDebug("QNativeSocketEnginePrivate::nativeWrite(%p \"%s\", %llu) == %i", data,
QtDebugUtils::toPrintable(data, len, 16).constData(), len, (int) writtenBytes);
#endif
return qint64(writtenBytes);
@ -1409,9 +1375,8 @@ qint64 QNativeSocketEnginePrivate::nativeRead(char *data, qint64 maxSize)
}
#if defined (QNATIVESOCKETENGINE_DEBUG)
qDebug("QNativeSocketEnginePrivate::nativeRead(%p \"%s\", %llu) == %zd",
data, qt_prettyDebug(data, qMin(r, ssize_t(16)), r).data(),
maxSize, r);
qDebug("QNativeSocketEnginePrivate::nativeRead(%p \"%s\", %llu) == %zd", data,
QtDebugUtils::toPrintable(data, r, 16).constData(), maxSize, r);
#endif
return qint64(r);

View File

@ -57,8 +57,7 @@
//#define QNATIVESOCKETENGINE_DEBUG
#if defined(QNATIVESOCKETENGINE_DEBUG)
# include <qstring.h>
# include <qbytearray.h>
#include <private/qdebug_p.h>
#endif
QT_BEGIN_NAMESPACE
@ -127,36 +126,6 @@ void verboseWSErrorDebug(int r)
qErrnoWarning(r, "more details");
}
/*
Returns a human readable representation of the first \a len
characters in \a data.
*/
static QByteArray qt_prettyDebug(const char *data, int len, int maxLength)
{
if (!data) return "(null)";
QByteArray out;
for (int i = 0; i < len; ++i) {
char c = data[i];
if (isprint(int(uchar(c)))) {
out += c;
} else switch (c) {
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
QString tmp;
tmp.sprintf("\\%o", c);
out += tmp.toLatin1().constData();
}
}
if (len < maxLength)
out += "...";
return out;
}
#define WS_ERROR_DEBUG(x) verboseWSErrorDebug(x)
#else
@ -1279,7 +1248,7 @@ qint64 QNativeSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 maxL
#if defined (QNATIVESOCKETENGINE_DEBUG)
bool printSender = (ret != -1 && (options & QNativeSocketEngine::WantDatagramSender) != 0);
qDebug("QNativeSocketEnginePrivate::nativeReceiveDatagram(%p \"%s\", %lli, %s, %i) == %lli",
data, qt_prettyDebug(data, qMin<qint64>(ret, 16), ret).data(), maxLength,
data, QtDebugUtils::toPrintable(data, ret, 16).constData(), maxLength,
printSender ? header->senderAddress.toString().toLatin1().constData() : "(unknown)",
printSender ? header->senderPort : 0, ret);
#endif
@ -1412,8 +1381,8 @@ qint64 QNativeSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 l
}
#if defined (QNATIVESOCKETENGINE_DEBUG)
qDebug("QNativeSocketEnginePrivate::nativeSendDatagram(%p \"%s\", %lli, \"%s\", %i) == %lli", data,
qt_prettyDebug(data, qMin<qint64>(len, 16), len).data(), len,
qDebug("QNativeSocketEnginePrivate::nativeSendDatagram(%p \"%s\", %lli, \"%s\", %i) == %lli",
data, QtDebugUtils::toPrintable(data, len, 16).constData(), len,
header.destinationAddress.toString().toLatin1().constData(),
header.destinationPort, ret);
#endif
@ -1472,7 +1441,7 @@ qint64 QNativeSocketEnginePrivate::nativeWrite(const char *data, qint64 len)
#if defined (QNATIVESOCKETENGINE_DEBUG)
qDebug("QNativeSocketEnginePrivate::nativeWrite(%p \"%s\", %lli) == %lli",
data, qt_prettyDebug(data, qMin(int(ret), 16), int(ret)).data(), len, ret);
data, QtDebugUtils::toPrintable(data, ret, 16).constData(), len, ret);
#endif
return ret;
@ -1514,8 +1483,8 @@ qint64 QNativeSocketEnginePrivate::nativeRead(char *data, qint64 maxLength)
#if defined (QNATIVESOCKETENGINE_DEBUG)
if (ret != -2) {
qDebug("QNativeSocketEnginePrivate::nativeRead(%p \"%s\", %lli) == %lli",
data, qt_prettyDebug(data, qMin(int(bytesRead), 16), int(bytesRead)).data(), maxLength, ret);
qDebug("QNativeSocketEnginePrivate::nativeRead(%p \"%s\", %lli) == %lli", data,
QtDebugUtils::toPrintable(data, bytesRead, 16).constData(), maxLength, ret);
} else {
qDebug("QNativeSocketEnginePrivate::nativeRead(%p, %lli) == -2 (WOULD BLOCK)",
data, maxLength);