1
0
mirror of https://github.com/qt/qtmqtt.git synced 2026-10-25 16:16:26 +00:00

Move qWarning to categorized logging

qWarning could potentially quit an application while it is easily
possible to recover from an error state. Hence avoid usage of qWarning
inside the module.

Task-number: QTBUG-69544
Change-Id: I7340f38d80e6716450fbcd0918226e9435d37daa
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
This commit is contained in:
Maurice Kalinowski 2018-07-25 15:32:42 +02:00
parent 8d2f9c3e0e
commit 19475c3403
9 changed files with 76 additions and 60 deletions

View File

@ -99,7 +99,7 @@ int main(int argc, char *argv[])
} else if (versionString == "3") {
clientsub.setVersion(3);
} else {
qWarning() << "Unknown MQTT version";
qInfo() << "Unknown MQTT version";
return -2;
}

View File

@ -30,11 +30,14 @@
#include "qmqttclient.h"
#include "qmqttclient_p.h"
#include <QtCore/QLoggingCategory>
#include <QtCore/QUuid>
#include <QtCore/QtEndian>
QT_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(lcMqttClient, "qt.mqtt.client")
/*!
\class QMqttClient
@ -289,7 +292,7 @@ void QMqttClient::setTransport(QIODevice *device, QMqttClient::TransportType tra
Q_D(QMqttClient);
if (d->m_state != Disconnected) {
qWarning("Changing transport layer while connected is not possible");
qCDebug(lcMqttClient) << "Changing transport layer while connected is not possible.";
return;
}
d->m_connection.setTransport(device, transport);
@ -421,12 +424,12 @@ void QMqttClient::connectToHost(bool encrypted, const QString &sslPeerName)
Q_D(QMqttClient);
if (state() == QMqttClient::Connected) {
qWarning("Already connected to a broker. Rejecting connection request.");
qCDebug(lcMqttClient) << "Already connected to a broker. Rejecting connection request.";
return;
}
if (!d->m_connection.ensureTransport(encrypted)) {
qWarning("Could not ensure connection");
qCDebug(lcMqttClient) << "Could not ensure connection.";
d->setStateAndError(Disconnected, TransportInvalid);
return;
}
@ -437,7 +440,7 @@ void QMqttClient::connectToHost(bool encrypted, const QString &sslPeerName)
d->m_connection.cleanSubscriptions();
if (!d->m_connection.ensureTransportOpen(sslPeerName)) {
qWarning("Could not ensure that connection is open");
qCDebug(lcMqttClient) << "Could not ensure that connection is open.";
d->setStateAndError(Disconnected, TransportInvalid);
return;
}
@ -606,7 +609,7 @@ void QMqttClient::setKeepAlive(quint16 keepAlive)
return;
if (state() != QMqttClient::Disconnected) {
qWarning("Trying to modify keepAlive while connected.");
qCDebug(lcMqttClient) << "Changing keepAlive while connected is not possible.";
return;
}

View File

@ -126,7 +126,7 @@ bool QMqttConnection::ensureTransport(bool createSecureIfNeeded)
// We are asked to create a transport layer
if (m_clientPrivate->m_hostname.isEmpty() || m_clientPrivate->m_port == 0) {
qWarning("Trying to create a transport layer, but no hostname is specified");
qCDebug(lcMqttConnection) << "No hostname specified, not able to create a transport layer.";
return false;
}
auto socket =
@ -158,7 +158,7 @@ bool QMqttConnection::ensureTransportOpen(const QString &sslPeerName)
return sendControlConnect();
if (!m_transport->open(QIODevice::ReadWrite)) {
qWarning("Could not open Transport IO device");
qCDebug(lcMqttConnection) << "Could not open Transport IO device.";
m_internalState = BrokerDisconnected;
return false;
}
@ -185,12 +185,12 @@ bool QMqttConnection::ensureTransportOpen(const QString &sslPeerName)
socket->connectToHostEncrypted(m_clientPrivate->m_hostname, m_clientPrivate->m_port, sslPeerName);
if (!socket->waitForConnected()) {
qWarning("Could not establish socket connection for transport");
qCDebug(lcMqttConnection) << "Could not establish socket connection for transport.";
return false;
}
if (!socket->waitForEncrypted()) {
qWarning("Could not initiate encryption.");
qCDebug(lcMqttConnection) << "Could not initiate encryption.";
return false;
}
}
@ -234,7 +234,7 @@ bool QMqttConnection::sendControlConnect()
if (!m_clientPrivate->m_willMessage.isEmpty()) {
flags |= 1 << 2;
if (m_clientPrivate->m_willQoS > 2) {
qWarning("Will QoS does not have a valid value");
qCDebug(lcMqttConnection) << "Invalid Will QoS specified.";
return false;
}
if (m_clientPrivate->m_willQoS == 1)
@ -283,7 +283,7 @@ bool QMqttConnection::sendControlConnect()
packet.append(m_clientPrivate->m_password.toUtf8());
if (!writePacketToTransport(packet)) {
qWarning("Could not write CONNECT frame to transport");
qCDebug(lcMqttConnection) << "Could not write CONNECT frame to transport.";
return false;
}
@ -392,7 +392,7 @@ QMqttSubscription *QMqttConnection::sendControlSubscribe(const QMqttTopicFilter
// Overflow protection
if (!topic.isValid()) {
qWarning("Subscribed topic filter is not valid.");
qCDebug(lcMqttConnection) << "Invalid subscription topic filter.";
return nullptr;
}
@ -471,7 +471,7 @@ bool QMqttConnection::sendControlPingRequest()
const QMqttControlPacket packet(QMqttControlPacket::PINGREQ);
if (!writePacketToTransport(packet)) {
qWarning("Could not write DISCONNECT frame to transport");
qCDebug(lcMqttConnection) << "Failed to write PINGREQ to transport.";
return false;
}
return true;
@ -487,7 +487,7 @@ bool QMqttConnection::sendControlDisconnect()
const QMqttControlPacket packet(QMqttControlPacket::DISCONNECT);
if (!writePacketToTransport(packet)) {
qWarning("Could not write DISCONNECT frame to transport");
qCDebug(lcMqttConnection) << "Failed to write DISCONNECT to transport.";
return false;
}
m_internalState = BrokerDisconnected;
@ -520,7 +520,7 @@ quint16 QMqttConnection::unusedPacketIdentifier() const
packetIdentifierCounter++;
if (lastIdentifier == packetIdentifierCounter) {
qWarning("Can't generate unique packet identifier.");
qCDebug(lcMqttConnection) << "Could not generate unique packet identifier.";
break;
}
} while (m_pendingSubscriptionAck.contains(packetIdentifierCounter)
@ -553,7 +553,7 @@ void QMqttConnection::transportConnectionEstablished()
}
if (!sendControlConnect()) {
qWarning("Could not send CONNECT to broker");
qCDebug(lcMqttConnection) << "Failed to write CONNECT to transport.";
// ### Who disconnects now? Connection or client?
m_clientPrivate->setStateAndError(QMqttClient::Disconnected, QMqttClient::TransportInvalid);
}
@ -594,7 +594,7 @@ qint32 QMqttConnection::readVariableByteInteger(qint32 *byteCount)
multiplier *= 128;
iteration++;
if (iteration > 4) {
qWarning("Publish message is too big to handle");
qCDebug(lcMqttConnection) << "Overflow trying to read variable integer.";
closeConnection(QMqttClient::ProtocolViolation);
return -1;
}
@ -761,7 +761,7 @@ void QMqttConnection::readConnackProperties()
break;
}
default:
qWarning() << "Unknown property id in CONNACK:" << int(propertyId);
qCDebug(lcMqttConnection) << "Unknown property id in CONNACK:" << int(propertyId);
break;
}
}
@ -839,7 +839,7 @@ void QMqttConnection::readPublishProperties(QMqttPublishProperties &properties)
break;
}
default:
qWarning("Unknown publish property received");
qCDebug(lcMqttConnection) << "Unknown publish property received.";
break;
}
}
@ -873,7 +873,7 @@ void QMqttConnection::readSubscriptionProperties(QMqttSubscription *sub)
break;
}
default:
qWarning("Unknown subscription property received");
qCDebug(lcMqttConnection) << "Unknown subscription property received.";
break;
}
}
@ -1041,7 +1041,7 @@ QByteArray QMqttConnection::writePublishProperties(const QMqttPublishProperties
packet.append(char(0x01));
break;
default:
qWarning("Unknown payload indicator");
qCDebug(lcMqttConnection) << "Unknown payload indicator.";
break;
}
}
@ -1062,8 +1062,9 @@ QByteArray QMqttConnection::writePublishProperties(const QMqttPublishProperties
<< properties.topicAlias();
if (m_clientPrivate->m_serverConnectionProperties.availableProperties() & QMqttServerConnectionProperties::MaximumTopicAlias
&& properties.topicAlias() > m_clientPrivate->m_serverConnectionProperties.maximumTopicAlias()) {
qWarning() << "Invalid topic alias specified: " << properties.topicAlias()
<< " Maximum by server is:" << m_clientPrivate->m_serverConnectionProperties.maximumTopicAlias();
qCDebug(lcMqttConnection) << "Invalid topic alias specified: " << properties.topicAlias()
<< " Maximum by server is:"
<< m_clientPrivate->m_serverConnectionProperties.maximumTopicAlias();
} else {
packet.append(char(0x23));
@ -1156,7 +1157,7 @@ void QMqttConnection::finalize_connack()
m_missingData--;
if (ackFlags > 1) { // MQTT-3.2.2.1
qWarning("Unexpected CONNACK Flags set");
qCDebug(lcMqttConnection) << "Unexpected CONNACK Flags specified:" << QString::number(ackFlags);
readBuffer(quint64(m_missingData));
m_missingData = 0;
closeConnection(QMqttClient::ProtocolViolation);
@ -1168,7 +1169,7 @@ void QMqttConnection::finalize_connack()
if (sessionPresent) {
emit m_clientPrivate->m_client->brokerSessionRestored();
if (m_clientPrivate->m_cleanSession)
qWarning("Connected with a clean session, ack contains session present");
qCDebug(lcMqttConnection) << "Connected with a clean session, ack contains session present.";
} else {
// MQTT-4.1.0.-1 MQTT-4.1.0-2 Session not stored on broker side
// regardless whether cleanSession is false
@ -1178,7 +1179,7 @@ void QMqttConnection::finalize_connack()
quint8 connectResultValue = readBufferTyped<quint8>();
m_missingData--;
if (connectResultValue != 0) {
qWarning("Connection has been rejected");
qCDebug(lcMqttConnection) << "Connection has been rejected.";
// MQTT-3.2.2-5
m_readBuffer.clear();
m_readPosition = 0;
@ -1205,7 +1206,7 @@ void QMqttConnection::finalize_suback()
const quint16 id = readBufferTyped<quint16>();
m_missingData -= 2;
if (!m_pendingSubscriptionAck.contains(id)) {
qWarning("Received SUBACK for unknown subscription request");
qCDebug(lcMqttConnection) << "Received SUBACK for unknown subscription request.";
return;
}
@ -1228,7 +1229,7 @@ void QMqttConnection::finalize_suback()
}
sub->setState(QMqttSubscription::Subscribed);
} else if (reason == 0x80) {
qWarning() << "Subscription for id " << id << " failed.";
qCDebug(lcMqttConnection) << "Subscription for id " << id << " failed.";
sub->setState(QMqttSubscription::Error);
} else {
bool mqtt5reason = false;
@ -1236,35 +1237,35 @@ void QMqttConnection::finalize_suback()
mqtt5reason = true;
switch (reason) {
case 0x83:
qWarning() << "Implementation specific error for id:" << id;
qCDebug(lcMqttConnection) << "SUBACK Reason code: Implementation specific error for id:" << id;
sub->setState(QMqttSubscription::Error);
break;
case 0x87:
qWarning() << "Not authorized for id:" << id;
qCDebug(lcMqttConnection) << "SUBACK Reason code: Not authorized for id:" << id;
sub->setState(QMqttSubscription::Error);
break;
case 0x8F:
qWarning() << "Topic filter invalid:" << id;
qCDebug(lcMqttConnection) << "SUBACK Reason code: Topic filter invalid:" << id;
sub->setState(QMqttSubscription::Error);
break;
case 0x91:
qWarning() << "Packet identifier in use:" << id;
qCDebug(lcMqttConnection) << "SUBACK Reason code: Packet identifier in use:" << id;
sub->setState(QMqttSubscription::Error);
break;
case 0x97:
qWarning() << "Quota exceeded:" << id;
qCDebug(lcMqttConnection) << "SUBACK Reason code: Quota exceeded:" << id;
sub->setState(QMqttSubscription::Error);
break;
case 0x9E:
qWarning() << "Shared subscriptions not supported:" << id;
qCDebug(lcMqttConnection) << "SUBACK Reason code: Shared subscriptions not supported:" << id;
sub->setState(QMqttSubscription::Error);
break;
case 0xA1:
qWarning() << "Subscription Identifiers not supported:" << id;
qCDebug(lcMqttConnection) << "SUBACK Reason code: Subscription Identifiers not supported:" << id;
sub->setState(QMqttSubscription::Error);
break;
case 0xA2:
qWarning() << "Wildcard subscriptions not supported:" << id;
qCDebug(lcMqttConnection) << "SUBACK Reason code: Wildcard subscriptions not supported:" << id;
sub->setState(QMqttSubscription::Error);
break;
default:
@ -1274,7 +1275,7 @@ void QMqttConnection::finalize_suback()
}
if (!mqtt5reason) {
qWarning("Received invalid SUBACK result value");
qCDebug(lcMqttConnection) << "Received invalid SUBACK result value:" << reason;
sub->setState(QMqttSubscription::Error);
}
}
@ -1287,7 +1288,7 @@ void QMqttConnection::finalize_unsuback()
m_missingData -= 2;
qCDebug(lcMqttConnectionVerbose) << "Finalize UNSUBACK: " << id;
if (!m_pendingUnsubscriptions.contains(id)) {
qWarning("Received UNSUBACK for unknown request");
qCDebug(lcMqttConnection) << "Received UNSUBACK for unknown request.";
return;
}
auto sub = m_pendingUnsubscriptions.take(id);
@ -1363,14 +1364,14 @@ void QMqttConnection::finalize_pubAckRecComp()
qCDebug(lcMqttConnectionVerbose) << " PUBCOMP:" << id;
auto pendingRelease = m_pendingReleaseMessages.take(id);
if (!pendingRelease)
qWarning("Received PUBCOMP for unknown released message");
qCDebug(lcMqttConnection) << "Received PUBCOMP for unknown released message.";
emit m_clientPrivate->m_client->messageSent(id);
return;
}
auto pendingMsg = m_pendingMessages.take(id);
if (!pendingMsg) {
qWarning() << QLatin1String("Received PUBACK for unknown message: ") << id;
qCDebug(lcMqttConnection) << "Received PUBACK for unknown message: " << id;
return;
}
if ((m_currentPacket & 0xF0) == QMqttControlPacket::PUBREC) {
@ -1402,7 +1403,7 @@ void QMqttConnection::finalize_pingresp()
m_missingData--;
if (v != 0) {
qWarning("Received a PINGRESP with payload!");
qCDebug(lcMqttConnection) << "Received a PINGRESP including payload.";
closeConnection(QMqttClient::ProtocolViolation);
return;
}
@ -1440,7 +1441,7 @@ void QMqttConnection::processData()
finalize_pubrel();
break;
default:
qWarning("Unknown packet to finalize");
qCDebug(lcMqttConnection) << "Unknown packet to finalize.";
closeConnection(QMqttClient::ProtocolViolation);
return;
}
@ -1480,7 +1481,7 @@ void QMqttConnection::processData()
case QMqttControlPacket::CONNACK: {
qCDebug(lcMqttConnectionVerbose) << "Received CONNACK";
if (m_internalState != BrokerWaitForConnectAck) {
qWarning("Received CONNACK at unexpected time!");
qCDebug(lcMqttConnection) << "Received CONNACK at unexpected time.";
closeConnection(QMqttClient::ProtocolViolation);
return;
}
@ -1488,7 +1489,7 @@ void QMqttConnection::processData()
qint32 payloadSize = readVariableByteInteger();
if (m_clientPrivate->m_protocolVersion != QMqttClient::MQTT_5_0) {
if (payloadSize != 2) {
qWarning("Unexpected FRAME size in CONNACK");
qCDebug(lcMqttConnection) << "Unexpected FRAME size in CONNACK.";
closeConnection(QMqttClient::ProtocolViolation);
return;
}
@ -1528,12 +1529,12 @@ void QMqttConnection::processData()
qCDebug(lcMqttConnectionVerbose) << "Received PUBREL";
const quint8 remaining = readBufferTyped<quint8>();
if (remaining != 0x02) {
qWarning("Received 2 byte message with invalid remaining length");
qCDebug(lcMqttConnection) << "Received 2 byte message with invalid remaining length.";
closeConnection(QMqttClient::ProtocolViolation);
return;
}
if ((m_currentPacket & 0x0F) != 0x02) {
qWarning("Malformed fixed header for PUBREL");
qCDebug(lcMqttConnection) << "Malformed fixed header for PUBREL.";
closeConnection(QMqttClient::ProtocolViolation);
return;
}
@ -1547,13 +1548,13 @@ void QMqttConnection::processData()
case QMqttControlPacket::PUBCOMP: {
qCDebug(lcMqttConnectionVerbose) << "Received UNSUBACK/PUBACK/PUBREC/PUBCOMP";
if ((m_currentPacket & 0x0F) != 0) {
qWarning("Malformed fixed header");
qCDebug(lcMqttConnection) << "Malformed fixed header for UNSUBACK/PUBACK/PUBREC/PUBCOMP.";
closeConnection(QMqttClient::ProtocolViolation);
return;
}
const quint8 remaining = readBufferTyped<quint8>();
if (m_clientPrivate->m_protocolVersion != QMqttClient::MQTT_5_0 && remaining != 0x02) {
qWarning("Received 2 byte message with invalid remaining length");
qCDebug(lcMqttConnection) << "Received 2 byte message with invalid remaining length.";
closeConnection(QMqttClient::ProtocolViolation);
return;
}
@ -1561,7 +1562,7 @@ void QMqttConnection::processData()
break;
}
default:
qWarning("Received unknown command");
qCDebug(lcMqttConnection) << "Received unknown command.";
closeConnection(QMqttClient::ProtocolViolation);
return;
}
@ -1577,7 +1578,7 @@ bool QMqttConnection::writePacketToTransport(const QMqttControlPacket &p)
const QByteArray writeData = p.serialize();
const qint64 res = m_transport->write(writeData.constData(), writeData.size());
if (Q_UNLIKELY(res == -1)) {
qWarning("Could not write frame to transport");
qCDebug(lcMqttConnection) << "Could not write frame to transport.";
return false;
}
return true;

View File

@ -31,10 +31,14 @@
#include "qmqttconnectionproperties_p.h"
#include <QtCore/QLoggingCategory>
#include <limits>
QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(lcMqttConnection)
QMqttLastWillProperties::QMqttLastWillProperties() : data(new QMqttLastWillPropertiesData)
{
}
@ -144,7 +148,7 @@ void QMqttConnectionProperties::setSessionExpiryInterval(quint32 expiry)
void QMqttConnectionProperties::setMaximumReceive(quint16 qos)
{
if (qos == 0) {
qWarning("Maximum Receive is not allowed to be 0.");
qCDebug(lcMqttConnection) << "Maximum Receive is not allowed to be 0.";
return;
}
data->maximumReceive = qos;
@ -153,7 +157,7 @@ void QMqttConnectionProperties::setMaximumReceive(quint16 qos)
void QMqttConnectionProperties::setMaximumPacketSize(quint32 packetSize)
{
if (packetSize == 0) {
qWarning("Packet size is not allowed to be 0.");
qCDebug(lcMqttConnection) << "Packet size is not allowed to be 0.";
return;
}
data->maximumPacketSize = packetSize;

View File

@ -28,10 +28,14 @@
******************************************************************************/
#include "qmqttcontrolpacket_p.h"
#include <QtCore/QtEndian>
#include <QtCore/QLoggingCategory>
QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(lcMqttClient)
QMqttControlPacket::QMqttControlPacket()
{
@ -99,7 +103,7 @@ void QMqttControlPacket::appendRawVariableInteger(quint32 value)
QByteArray data;
// Add length
if (value > 268435455)
qWarning("Variable Integer overflow");
qCDebug(lcMqttClient) << "Attempting to write variable integer overflow.";
do {
quint8 b = value % 128;
value /= 128;
@ -127,7 +131,7 @@ QByteArray QMqttControlPacket::serializePayload() const
// Add length
quint32 msgSize = quint32(m_payload.size());
if (msgSize > 268435455)
qWarning("Publishing a message bigger than maximum size!");
qCDebug(lcMqttClient) << "Publishing a message bigger than maximum size.";
do {
quint8 b = msgSize % 128;
msgSize /= 128;

View File

@ -30,8 +30,12 @@
#include "qmqttpublishproperties.h"
#include "qmqtttype.h"
#include <QtCore/QLoggingCategory>
QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(lcMqttClient)
class QMqttPublishPropertiesData : public QSharedData
{
public:
@ -97,7 +101,7 @@ quint16 QMqttPublishProperties::topicAlias() const
void QMqttPublishProperties::setTopicAlias(quint16 alias)
{
if (alias == 0) {
qWarning("A topic alias with value 0 is not allowed.");
qCDebug(lcMqttClient) << "A topic alias with value 0 is not allowed.";
return;
}
data->details |= QMqttPublishProperties::TopicAlias;
@ -145,7 +149,7 @@ quint32 QMqttPublishProperties::subscriptionIdentifier() const
void QMqttPublishProperties::setSubscriptionIdentifier(quint32 id)
{
if (id == 0) {
qWarning("A subscription identifier with value 0 is not allowed.");
qCDebug(lcMqttClient) << "A subscription identifier with value 0 is not allowed.";
return;
}
data->details |= QMqttPublishProperties::SubscriptionIdentifier;

View File

@ -529,7 +529,7 @@ public:
if (data[0] == 0x10)
written = 1;
else
qWarning() << "Received unknown/invalid data";
qDebug() << "Received unknown/invalid data";
return len;
}
QAtomicInt written{0};

View File

@ -195,7 +195,7 @@ void tst_QMqttConnectionProperties::maximumPacketSize()
QMqttServerConnectionProperties serverProperties = client.serverConnectionProperties();
if (serverProperties.availableProperties() & QMqttServerConnectionProperties::MaximumPacketSize) {
if (serverProperties.maximumPacketSize() < props.maximumPacketSize()) {
qWarning("Server accepts less data than required for this test.");
qDebug() << "Server accepts less data than required for this test.";
}
} else {
qDebug() << "Server has no max packet size specified";

View File

@ -96,7 +96,7 @@ QString invokeOrInitializeBroker(QProcess *gBrokerProcess)
QTest::qWait(5000);
}
qWarning("Could not launch MQTT test broker.");
qWarning() << "Could not launch MQTT test broker.";
return QString();
}