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. /*! 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 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,48 +41,11 @@ //#define QPROCESS_DEBUG #include +#include #include #include #if defined(Q_OS_WIN) #include -#endif -#if defined QPROCESS_DEBUG -#include -#include - -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" @@ -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 - -/* - 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 #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 diff --git a/src/corelib/serialization/qtextstream.cpp b/src/corelib/serialization/qtextstream.cpp index 137d736b30..392a6bfeaa 100644 --- a/src/corelib/serialization/qtextstream.cpp +++ b/src/corelib/serialization/qtextstream.cpp @@ -230,6 +230,7 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384; #include "qfile.h" #include "qnumeric.h" #include "qvarlengtharray.h" +#include #include #include "private/qlocale_p.h" @@ -239,48 +240,6 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384; #include #include -#if defined QTEXTSTREAM_DEBUG -#include -#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) diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp index 16819404f4..56ffb678e4 100644 --- a/src/network/socket/qabstractsocket.cpp +++ b/src/network/socket/qabstractsocket.cpp @@ -483,6 +483,7 @@ #ifdef QABSTRACTSOCKET_DEBUG #include +#include #endif #include @@ -501,42 +502,6 @@ QT_BEGIN_NAMESPACE static const int DefaultConnectTimeout = 30000; -#if defined QABSTRACTSOCKET_DEBUG -QT_BEGIN_INCLUDE_NAMESPACE -#include -#include -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; } diff --git a/src/network/socket/qnativesocketengine_unix.cpp b/src/network/socket/qnativesocketengine_unix.cpp index 09d16fa908..e6704a3ca4 100644 --- a/src/network/socket/qnativesocketengine_unix.cpp +++ b/src/network/socket/qnativesocketengine_unix.cpp @@ -63,8 +63,7 @@ #endif #if defined QNATIVESOCKETENGINE_DEBUG -#include -#include +#include #endif #include @@ -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(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); diff --git a/src/network/socket/qnativesocketengine_win.cpp b/src/network/socket/qnativesocketengine_win.cpp index ac4e725af0..7b5e674fbb 100644 --- a/src/network/socket/qnativesocketengine_win.cpp +++ b/src/network/socket/qnativesocketengine_win.cpp @@ -57,8 +57,7 @@ //#define QNATIVESOCKETENGINE_DEBUG #if defined(QNATIVESOCKETENGINE_DEBUG) -# include -# include +#include #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(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(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);