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 "qiodevice_p.h"
#include "qfile.h" #include "qfile.h"
#include "qstringlist.h" #include "qstringlist.h"
#include "qdir.h"
#include <algorithm> #include <algorithm>
#include <limits.h> #include <limits.h>
@ -80,10 +81,29 @@ void debugBinaryString(const char *data, qint64 maxlen)
#define Q_VOID #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) \ #define CHECK_MAXLEN(function, returnType) \
do { \ do { \
if (maxSize < 0) { \ if (maxSize < 0) { \
qWarning("QIODevice::"#function": Called with maxSize < 0"); \ checkWarnMessage(this, #function, "Called with maxSize < 0"); \
return returnType; \ return returnType; \
} \ } \
} while (0) } while (0)
@ -92,10 +112,10 @@ void debugBinaryString(const char *data, qint64 maxlen)
do { \ do { \
if ((d->openMode & WriteOnly) == 0) { \ if ((d->openMode & WriteOnly) == 0) { \
if (d->openMode == NotOpen) { \ if (d->openMode == NotOpen) { \
qWarning("QIODevice::"#function": device not open"); \ checkWarnMessage(this, #function, "device not open"); \
return returnType; \ return returnType; \
} \ } \
qWarning("QIODevice::"#function": ReadOnly device"); \ checkWarnMessage(this, #function, "ReadOnly device"); \
return returnType; \ return returnType; \
} \ } \
} while (0) } while (0)
@ -104,10 +124,10 @@ void debugBinaryString(const char *data, qint64 maxlen)
do { \ do { \
if ((d->openMode & ReadOnly) == 0) { \ if ((d->openMode & ReadOnly) == 0) { \
if (d->openMode == NotOpen) { \ if (d->openMode == NotOpen) { \
qWarning("QIODevice::"#function": device not open"); \ checkWarnMessage(this, #function, "device not open"); \
return returnType; \ return returnType; \
} \ } \
qWarning("QIODevice::"#function": WriteOnly device"); \ checkWarnMessage(this, #function, "WriteOnly device"); \
return returnType; \ return returnType; \
} \ } \
} while (0) } while (0)
@ -462,7 +482,7 @@ void QIODevice::setTextModeEnabled(bool enabled)
{ {
Q_D(QIODevice); Q_D(QIODevice);
if (!isOpen()) { if (!isOpen()) {
qWarning("QIODevice::setTextModeEnabled: The device is not open"); checkWarnMessage(this, "setTextModeEnabled", "The device is not open");
return; return;
} }
if (enabled) if (enabled)
@ -621,11 +641,11 @@ bool QIODevice::seek(qint64 pos)
{ {
Q_D(QIODevice); Q_D(QIODevice);
if (d->isSequential()) { 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; return false;
} }
if (d->openMode == NotOpen) { if (d->openMode == NotOpen) {
qWarning("QIODevice::seek: The device is not open"); checkWarnMessage(this, "seek", "The device is not open");
return false; return false;
} }
if (pos < 0) { if (pos < 0) {
@ -923,7 +943,7 @@ QByteArray QIODevice::read(qint64 maxSize)
#endif #endif
if (maxSize != qint64(int(maxSize))) { 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; maxSize = INT_MAX;
} }
@ -1055,7 +1075,7 @@ qint64 QIODevice::readLine(char *data, qint64 maxSize)
{ {
Q_D(QIODevice); Q_D(QIODevice);
if (maxSize < 2) { if (maxSize < 2) {
qWarning("QIODevice::readLine: Called with maxSize < 2"); checkWarnMessage(this, "readLine", "Called with maxSize < 2");
return qint64(-1); return qint64(-1);
} }

View File

@ -151,7 +151,7 @@ void tst_QBuffer::readBlock()
QCOMPARE(b.bytesAvailable(), (qint64) arraySize); QCOMPARE(b.bytesAvailable(), (qint64) arraySize);
b.open(QIODevice::WriteOnly); b.open(QIODevice::WriteOnly);
QCOMPARE(b.bytesAvailable(), (qint64) arraySize); 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 QCOMPARE(b.read(a, arraySize), (qint64) -1); // no read access
b.close(); b.close();

View File

@ -2334,7 +2334,7 @@ void tst_QFile::readFromWriteOnlyFile()
QFile file("writeonlyfile"); QFile file("writeonlyfile");
QVERIFY(file.open(QFile::WriteOnly)); QVERIFY(file.open(QFile::WriteOnly));
char c; 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)); QCOMPARE(file.read(&c, 1), qint64(-1));
} }
@ -2343,7 +2343,7 @@ void tst_QFile::writeToReadOnlyFile()
QFile file("readonlyfile"); QFile file("readonlyfile");
QVERIFY(file.open(QFile::ReadOnly)); QVERIFY(file.open(QFile::ReadOnly));
char c = 0; 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)); QCOMPARE(file.write(&c, 1), qint64(-1));
} }

View File

@ -224,7 +224,7 @@ void tst_QIODevice::unget()
buf[0] = '@'; buf[0] = '@';
buf[1] = '@'; buf[1] = '@';
QTest::ignoreMessage(QtWarningMsg, 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, 1), qint64(-1));
QCOMPARE(buffer.readLine(buf, 2), qint64(i < 4 ? 1 : -1)); QCOMPARE(buffer.readLine(buf, 2), qint64(i < 4 ? 1 : -1));
switch (i) { switch (i) {

View File

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