Make warnings of QIODevice more verbose.

Include class name, object name and file name when available.
For the bug in question:

QIODevice::read: device not open

becomes

QIODevice::read (QTcpSocket, "QFtpDTP Passive state socket"): device not open

Adding a static function also makes it easier to set a breakpoint
and find the culprit.

Task-number: QTBUG-46112
Change-Id: Ic181d8ab292912d1acbcc3cb84d9679fe4842ca0
Reviewed-by: Laszlo Papp <lpapp@kde.org>
Reviewed-by: Alex Trotsenko <alex1973tr@gmail.com>
Reviewed-by: Kai Koehne <kai.koehne@theqtcompany.com>
This commit is contained in:
Friedemann Kleint 2015-05-18 16:19:48 +02:00
parent cab7e7858a
commit 06de0da1e8
5 changed files with 39 additions and 19 deletions

View File

@ -38,6 +38,7 @@
#include "qiodevice_p.h"
#include "qfile.h"
#include "qstringlist.h"
#include "qdir.h"
#include <algorithm>
#include <limits.h>
@ -80,10 +81,29 @@ void debugBinaryString(const char *data, qint64 maxlen)
#define Q_VOID
static void checkWarnMessage(const QIODevice *device, const char *function, const char *what)
{
QDebug d = qWarning();
d.noquote();
d.nospace();
d << "QIODevice::" << function;
#ifndef QT_NO_QOBJECT
d << " (" << device->metaObject()->className();
if (!device->objectName().isEmpty())
d << ", \"" << device->objectName() << '"';
if (const QFile *f = qobject_cast<const QFile *>(device))
d << ", \"" << QDir::toNativeSeparators(f->fileName()) << '"';
d << ')';
#else
Q_UNUSED(device)
#endif // !QT_NO_QOBJECT
d << ": " << what;
}
#define CHECK_MAXLEN(function, returnType) \
do { \
if (maxSize < 0) { \
qWarning("QIODevice::"#function": Called with maxSize < 0"); \
checkWarnMessage(this, #function, "Called with maxSize < 0"); \
return returnType; \
} \
} while (0)
@ -92,10 +112,10 @@ void debugBinaryString(const char *data, qint64 maxlen)
do { \
if ((d->openMode & WriteOnly) == 0) { \
if (d->openMode == NotOpen) { \
qWarning("QIODevice::"#function": device not open"); \
checkWarnMessage(this, #function, "device not open"); \
return returnType; \
} \
qWarning("QIODevice::"#function": ReadOnly device"); \
checkWarnMessage(this, #function, "ReadOnly device"); \
return returnType; \
} \
} while (0)
@ -104,10 +124,10 @@ void debugBinaryString(const char *data, qint64 maxlen)
do { \
if ((d->openMode & ReadOnly) == 0) { \
if (d->openMode == NotOpen) { \
qWarning("QIODevice::"#function": device not open"); \
checkWarnMessage(this, #function, "device not open"); \
return returnType; \
} \
qWarning("QIODevice::"#function": WriteOnly device"); \
checkWarnMessage(this, #function, "WriteOnly device"); \
return returnType; \
} \
} while (0)
@ -462,7 +482,7 @@ void QIODevice::setTextModeEnabled(bool enabled)
{
Q_D(QIODevice);
if (!isOpen()) {
qWarning("QIODevice::setTextModeEnabled: The device is not open");
checkWarnMessage(this, "setTextModeEnabled", "The device is not open");
return;
}
if (enabled)
@ -621,11 +641,11 @@ bool QIODevice::seek(qint64 pos)
{
Q_D(QIODevice);
if (d->isSequential()) {
qWarning("QIODevice::seek: Cannot call seek on a sequential device");
checkWarnMessage(this, "seek", "Cannot call seek on a sequential device");
return false;
}
if (d->openMode == NotOpen) {
qWarning("QIODevice::seek: The device is not open");
checkWarnMessage(this, "seek", "The device is not open");
return false;
}
if (pos < 0) {
@ -923,7 +943,7 @@ QByteArray QIODevice::read(qint64 maxSize)
#endif
if (maxSize != qint64(int(maxSize))) {
qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit");
checkWarnMessage(this, "read", "maxSize argument exceeds QByteArray size limit");
maxSize = INT_MAX;
}
@ -1055,7 +1075,7 @@ qint64 QIODevice::readLine(char *data, qint64 maxSize)
{
Q_D(QIODevice);
if (maxSize < 2) {
qWarning("QIODevice::readLine: Called with maxSize < 2");
checkWarnMessage(this, "readLine", "Called with maxSize < 2");
return qint64(-1);
}

View File

@ -151,7 +151,7 @@ void tst_QBuffer::readBlock()
QCOMPARE(b.bytesAvailable(), (qint64) arraySize);
b.open(QIODevice::WriteOnly);
QCOMPARE(b.bytesAvailable(), (qint64) arraySize);
QTest::ignoreMessage(QtWarningMsg, "QIODevice::read: WriteOnly device");
QTest::ignoreMessage(QtWarningMsg, "QIODevice::read (QBuffer): WriteOnly device");
QCOMPARE(b.read(a, arraySize), (qint64) -1); // no read access
b.close();

View File

@ -2334,7 +2334,7 @@ void tst_QFile::readFromWriteOnlyFile()
QFile file("writeonlyfile");
QVERIFY(file.open(QFile::WriteOnly));
char c;
QTest::ignoreMessage(QtWarningMsg, "QIODevice::read: WriteOnly device");
QTest::ignoreMessage(QtWarningMsg, "QIODevice::read (QFile, \"writeonlyfile\"): WriteOnly device");
QCOMPARE(file.read(&c, 1), qint64(-1));
}
@ -2343,7 +2343,7 @@ void tst_QFile::writeToReadOnlyFile()
QFile file("readonlyfile");
QVERIFY(file.open(QFile::ReadOnly));
char c = 0;
QTest::ignoreMessage(QtWarningMsg, "QIODevice::write: ReadOnly device");
QTest::ignoreMessage(QtWarningMsg, "QIODevice::write (QFile, \"readonlyfile\"): ReadOnly device");
QCOMPARE(file.write(&c, 1), qint64(-1));
}

View File

@ -224,7 +224,7 @@ void tst_QIODevice::unget()
buf[0] = '@';
buf[1] = '@';
QTest::ignoreMessage(QtWarningMsg,
"QIODevice::readLine: Called with maxSize < 2");
"QIODevice::readLine (QBuffer): Called with maxSize < 2");
QCOMPARE(buffer.readLine(buf, 1), qint64(-1));
QCOMPARE(buffer.readLine(buf, 2), qint64(i < 4 ? 1 : -1));
switch (i) {

View File

@ -401,8 +401,8 @@ void tst_QSslSocket::proxyAuthenticationRequired(const QNetworkProxy &, QAuthent
void tst_QSslSocket::constructing()
{
const char readNotOpenMessage[] = "QIODevice::read: device not open";
const char writeNotOpenMessage[] = "QIODevice::write: device not open";
const char readNotOpenMessage[] = "QIODevice::read (QSslSocket): device not open";
const char writeNotOpenMessage[] = "QIODevice::write (QSslSocket): device not open";
if (!QSslSocket::supportsSsl())
return;
@ -440,13 +440,13 @@ void tst_QSslSocket::constructing()
QCOMPARE(socket.read(0, 0), qint64(-1));
QTest::ignoreMessage(QtWarningMsg, readNotOpenMessage);
QVERIFY(socket.readAll().isEmpty());
QTest::ignoreMessage(QtWarningMsg, "QIODevice::readLine: Called with maxSize < 2");
QTest::ignoreMessage(QtWarningMsg, "QIODevice::readLine (QSslSocket): Called with maxSize < 2");
QCOMPARE(socket.readLine(0, 0), qint64(-1));
char buf[10];
QCOMPARE(socket.readLine(buf, sizeof(buf)), qint64(-1));
QTest::ignoreMessage(QtWarningMsg, "QIODevice::seek: Cannot call seek on a sequential device");
QTest::ignoreMessage(QtWarningMsg, "QIODevice::seek (QSslSocket): Cannot call seek on a sequential device");
QVERIFY(!socket.reset());
QTest::ignoreMessage(QtWarningMsg, "QIODevice::seek: Cannot call seek on a sequential device");
QTest::ignoreMessage(QtWarningMsg, "QIODevice::seek (QSslSocket): Cannot call seek on a sequential device");
QVERIFY(!socket.seek(2));
QCOMPARE(socket.size(), qint64(0));
QVERIFY(!socket.waitForBytesWritten(10));