diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2019-03-07 17:22:33 +0100 |
---|---|---|
committer | Aapo Keskimolo <aapo.keskimolo@qt.io> | 2019-03-19 19:50:48 +0000 |
commit | 7a3552189ca2493bba025012243763a2cf5ebb06 (patch) | |
tree | 21541f2727daaf575fa108762d4dbac4834b73f2 | |
parent | d11f60a69547d49b2dc3b6948eba28d54b535ebb (diff) |
Improve logging of the CoAP module
- Introduced different log categories and categorized
log messages accordingly.
- Improved logs from console example by:
* Changing debug messages to info or warning,
* Removing extra spaces from the log messages.
Change-Id: Ia6b2006db5e7cce853e59fb7d0a1c20064444c8e
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r-- | examples/testapp/coaphandler.cpp | 21 | ||||
-rw-r--r-- | src/coap/qcoapclient.cpp | 11 | ||||
-rw-r--r-- | src/coap/qcoapconnection.cpp | 6 | ||||
-rw-r--r-- | src/coap/qcoapinternalmessage.cpp | 6 | ||||
-rw-r--r-- | src/coap/qcoapinternalrequest.cpp | 7 | ||||
-rw-r--r-- | src/coap/qcoapoption.cpp | 8 | ||||
-rw-r--r-- | src/coap/qcoapprotocol.cpp | 28 | ||||
-rw-r--r-- | src/coap/qcoapqudpconnection.cpp | 31 | ||||
-rw-r--r-- | src/coap/qcoapreply.cpp | 9 | ||||
-rw-r--r-- | src/coap/qcoaprequest.cpp | 9 |
10 files changed, 86 insertions, 50 deletions
diff --git a/examples/testapp/coaphandler.cpp b/examples/testapp/coaphandler.cpp index 7b09fe6..04ef6f3 100644 --- a/examples/testapp/coaphandler.cpp +++ b/examples/testapp/coaphandler.cpp @@ -31,10 +31,13 @@ #include "coaphandler.h" #include <QDebug> +#include <QLoggingCategory> #include <QCoapClient> #include <QCoapReply> #include <QCoapDiscoveryReply> +Q_LOGGING_CATEGORY(lcCoapClient, "qt.coap.client") + CoapHandler::CoapHandler(QObject *parent) : QObject(parent) { connect(&m_coapClient, &QCoapClient::finished, this, &CoapHandler::onFinished); @@ -86,9 +89,9 @@ bool CoapHandler::runDiscover(const QUrl &url) void CoapHandler::onFinished(QCoapReply *reply) { if (reply->errorReceived() == QtCoap::NoError) - qDebug() << "Request finished with payload: " << reply->readAll(); + qCInfo(lcCoapClient) << "Request finished with payload:" << reply->readAll(); else - qDebug() << "Request failed"; + qCWarning(lcCoapClient, "Request failed"); // Don't forget to remove the reply reply->deleteLater(); @@ -99,7 +102,7 @@ void CoapHandler::onNotified(QCoapReply *reply, QCoapMessage message) Q_UNUSED(message) // You can alternatively use `message.payload();` - qDebug() << "Received Observe notification with payload: " << reply->readAll(); + qCInfo(lcCoapClient) << "Received Observe notification with payload:" << reply->readAll(); } void CoapHandler::onDiscovered(QCoapDiscoveryReply *reply, QVector<QCoapResource> resources) @@ -107,23 +110,23 @@ void CoapHandler::onDiscovered(QCoapDiscoveryReply *reply, QVector<QCoapResource Q_UNUSED(reply) for (const QCoapResource &res : qAsConst(resources)) - qDebug() << "Discovered resource: " << res.path() << res.title(); + qCInfo(lcCoapClient) << "Discovered resource:" << res.path() << res.title(); } void CoapHandler::onResponseToMulticast(QCoapReply *reply, const QCoapMessage& message, const QHostAddress &sender) { if (reply->errorReceived() == QtCoap::NoError) - qDebug() << "Got a response for multicast request from:" << sender.toString() - << "with payload:" << message.payload(); + qCInfo(lcCoapClient) << "Got a response for multicast request from:" << sender.toString() + << "with payload:" << message.payload(); else - qDebug() << "Multicast request failed"; + qCWarning(lcCoapClient, "Multicast request failed"); } void CoapHandler::onError(QCoapReply *reply, QtCoap::Error error) { if (reply) - qDebug() << "CoAP reply error: " << reply->errorString(); + qCInfo(lcCoapClient) << "CoAP reply error:" << reply->errorString(); else - qDebug() << "CoAP error: " << error; + qCWarning(lcCoapClient) << "CoAP error:" << error; } diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index b58444d..8689734 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -37,10 +37,13 @@ #include "qcoapqudpconnection.h" #include <QtCore/qiodevice.h> #include <QtCore/qurl.h> +#include <QtCore/qloggingcategory.h> #include <QtNetwork/qudpsocket.h> QT_BEGIN_NAMESPACE +Q_LOGGING_CATEGORY(lcCoapClient, "qt.coap.client") + QCoapClientPrivate::QCoapClientPrivate(QCoapProtocol *protocol, QCoapConnection *connection) : protocol(protocol) , connection(connection) @@ -551,12 +554,12 @@ bool QCoapClientPrivate::send(QCoapReply *reply) { const auto scheme = connection->isSecure() ? QLatin1String("coaps") : QLatin1String("coap"); if (reply->request().url().scheme() != scheme) { - qWarning("QCoapClient: Failed to send request, URL has an incorrect scheme."); + qCWarning(lcCoapClient, "Failed to send request, URL has an incorrect scheme."); return false; } if (!QCoapRequest::isUrlValid(reply->request().url())) { - qWarning("QCoapClient: Failed to send request for an invalid URL."); + qCWarning(lcCoapClient, "Failed to send request for an invalid URL."); return false; } @@ -564,8 +567,8 @@ bool QCoapClientPrivate::send(QCoapReply *reply) // multicast requests MUST be Non-confirmable. if (QHostAddress(reply->url().host()).isMulticast() && reply->request().type() == QCoapMessage::Confirmable) { - qWarning("QCoapClient: Failed to send request, " - "multicast requests must be non-confirmable."); + qCWarning(lcCoapClient, "Failed to send request, " + "multicast requests must be non-confirmable."); return false; } diff --git a/src/coap/qcoapconnection.cpp b/src/coap/qcoapconnection.cpp index 2e6e367..2d70bf5 100644 --- a/src/coap/qcoapconnection.cpp +++ b/src/coap/qcoapconnection.cpp @@ -29,8 +29,12 @@ #include "qcoapconnection_p.h" +#include <QtCore/qloggingcategory.h> + QT_BEGIN_NAMESPACE +Q_LOGGING_CATEGORY(lcCoapConnection, "qt.coap.connection") + /*! \class QCoapConnection \inmodule QtCoap @@ -223,7 +227,7 @@ void QCoapConnection::setSecurityConfiguration(const QCoapSecurityConfiguration d->securityConfiguration = configuration; emit securityConfigurationChanged(); } else { - qWarning("QtCoap: Security is disabled, security configuration will be ignored"); + qCWarning(lcCoapConnection, "Security is disabled, security configuration will be ignored."); } } diff --git a/src/coap/qcoapinternalmessage.cpp b/src/coap/qcoapinternalmessage.cpp index 8bb07c6..85c2263 100644 --- a/src/coap/qcoapinternalmessage.cpp +++ b/src/coap/qcoapinternalmessage.cpp @@ -31,8 +31,12 @@ #include "qcoapinternalmessage_p.h" #include <QtCoap/qcoaprequest.h> +#include <QtCore/qloggingcategory.h> + QT_BEGIN_NAMESPACE +Q_LOGGING_CATEGORY(lcCoapExchange, "qt.coap.exchange") + /*! \internal @@ -132,7 +136,7 @@ void QCoapInternalMessage::setFromDescriptiveBlockOption(const QCoapOption &opti d->blockSize = static_cast<uint>(1u << ((lastByte & 0x7) + 4)); if (d->blockSize > 1024) - qWarning("QtCoap: Received a block size larger than 1024, something may be wrong."); + qCWarning(lcCoapExchange, "Received a block size larger than 1024, something may be wrong."); } /*! diff --git a/src/coap/qcoapinternalrequest.cpp b/src/coap/qcoapinternalrequest.cpp index ec37969..b8eb51e 100644 --- a/src/coap/qcoapinternalrequest.cpp +++ b/src/coap/qcoapinternalrequest.cpp @@ -34,10 +34,13 @@ #include <QtCore/qmath.h> #include <QtCore/qrandom.h> #include <QtCore/qregularexpression.h> +#include <QtCore/qloggingcategory.h> #include <QtNetwork/QHostAddress> QT_BEGIN_NAMESPACE +Q_DECLARE_LOGGING_CATEGORY(lcCoapExchange) + /*! \internal @@ -292,8 +295,8 @@ void QCoapInternalRequest::setToSendBlock(uint blockNumber, uint blockSize) bool QCoapInternalRequest::checkBlockNumber(uint blockNumber) { if (blockNumber >> 20) { - qWarning() << "QtCoap: Block number" << blockNumber << "is too large." - " It should fit in 20 bits."; + qCWarning(lcCoapExchange) << "Block number" << blockNumber + << "is too large. It should fit in 20 bits."; return false; } diff --git a/src/coap/qcoapoption.cpp b/src/coap/qcoapoption.cpp index 834d282..ddb42fd 100644 --- a/src/coap/qcoapoption.cpp +++ b/src/coap/qcoapoption.cpp @@ -28,11 +28,15 @@ ** ****************************************************************************/ -#include <QtCore/qdebug.h> #include "qcoapoption_p.h" +#include <QtCore/qdebug.h> +#include <QtCore/qloggingcategory.h> + QT_BEGIN_NAMESPACE +Q_LOGGING_CATEGORY(lcCoapOption, "qt.coap.option") + /*! \class QCoapOption \inmodule QtCoap @@ -323,7 +327,7 @@ void QCoapOption::setValue(const QByteArray &value) } if (oversized) - qWarning() << "QCoapOption::setValue: value is probably too big for option" << d->name; + qCWarning(lcCoapOption) << "Value" << value << "is probably too big for option" << d->name; d->value = value; } diff --git a/src/coap/qcoapprotocol.cpp b/src/coap/qcoapprotocol.cpp index 8e0b5f2..90bcee5 100644 --- a/src/coap/qcoapprotocol.cpp +++ b/src/coap/qcoapprotocol.cpp @@ -35,10 +35,13 @@ #include <QtCore/qrandom.h> #include <QtCore/qthread.h> +#include <QtCore/qloggingcategory.h> #include <QtNetwork/qnetworkdatagram.h> QT_BEGIN_NAMESPACE +Q_LOGGING_CATEGORY(lcCoapProtocol, "qt.coap.protocol") + /*! \class QCoapProtocol \inmodule QtCoap @@ -195,7 +198,7 @@ void QCoapProtocolPrivate::sendRequest(QCoapInternalRequest *request, const QStr Q_ASSERT(QThread::currentThread() == q->thread()); if (!request || !request->connection()) { - qWarning("QtCoap: Request null or not bound to any connection: aborted."); + qCWarning(lcCoapProtocol, "Request null or not bound to any connection: aborted."); return; } @@ -264,8 +267,8 @@ void QCoapProtocolPrivate::onMulticastRequestExpired(QCoapInternalRequest *reque QMetaObject::invokeMethod(userReply, "_q_setFinished", Qt::QueuedConnection, Q_ARG(QtCoap::Error, QtCoap::NoError)); } else { - qWarning().nospace() << "QtCoap: Reply for token '" << request->token() - << "' is not registered, reply is null."; + qCWarning(lcCoapProtocol).nospace() << "Reply for token '" << request->token() + << "' is not registered, reply is null."; } forgetExchange(request); } @@ -341,9 +344,9 @@ void QCoapProtocolPrivate::onFrameReceived(const QByteArray &data, const QHostAd QHostAddress originalTarget(request->targetUri().host()); if (!originalTarget.isMulticast() && !originalTarget.isEqual(sender)) { - qDebug().nospace() << "QtCoap: Answer received from incorrect host (" - << sender << " instead of " - << originalTarget << ")"; + qCDebug(lcCoapProtocol).nospace() << "QtCoap: Answer received from incorrect host (" + << sender << " instead of " + << originalTarget << ")"; return; } @@ -769,7 +772,8 @@ bool QCoapProtocolPrivate::addReply(const QCoapToken &token, QSharedPointer<QCoapInternalReply> reply) { if (!isTokenRegistered(token) || !reply) { - qWarning() << "QtCoap: Reply token '" << token << "' not registered, or reply is null."; + qCWarning(lcCoapProtocol).nospace() << "Reply token '" << token + << "' not registered, or reply is null."; return false; } @@ -1036,7 +1040,7 @@ void QCoapProtocol::setAckRandomFactor(double ackRandomFactor) { Q_D(QCoapProtocol); if (ackRandomFactor < 1) - qWarning() << "QtCoap: The Ack random factor should be >= 1"; + qCWarning(lcCoapProtocol, "The acknowledgment random factor should be >= 1"); d->ackRandomFactor = qMax(1., ackRandomFactor); } @@ -1053,7 +1057,7 @@ void QCoapProtocol::setMaxRetransmit(uint maxRetransmit) Q_D(QCoapProtocol); if (maxRetransmit > 25) { - qWarning("QtCoap: Max retransmit count is capped at 25."); + qCWarning(lcCoapProtocol, "Max retransmit count is capped at 25."); maxRetransmit = 25; } @@ -1073,13 +1077,13 @@ void QCoapProtocol::setBlockSize(quint16 blockSize) Q_D(QCoapProtocol); if ((blockSize & (blockSize - 1)) != 0) { - qWarning("QtCoap: Block size should be a power of 2"); + qCWarning(lcCoapProtocol, "Block size should be a power of 2"); return; } if (blockSize != 0 && (blockSize < 16 || blockSize > 1024)) { - qWarning("QtCoap: Block size should be set to zero," - "or to a power of 2 from 16 through 1024"); + qCWarning(lcCoapProtocol, "Block size should be set to zero," + "or to a power of 2 from 16 through 1024"); return; } diff --git a/src/coap/qcoapqudpconnection.cpp b/src/coap/qcoapqudpconnection.cpp index ecba9b5..27a23e0 100644 --- a/src/coap/qcoapqudpconnection.cpp +++ b/src/coap/qcoapqudpconnection.cpp @@ -30,6 +30,7 @@ #include "qcoapqudpconnection_p.h" +#include <QtCore/qloggingcategory.h> #include <QtNetwork/qnetworkdatagram.h> #if QT_CONFIG(dtls) @@ -41,6 +42,8 @@ QT_BEGIN_NAMESPACE +Q_DECLARE_LOGGING_CATEGORY(lcCoapConnection) + /*! \class QCoapQUdpConnection \inmodule QtCoap @@ -99,7 +102,8 @@ QCoapQUdpConnection::QCoapQUdpConnection(QCoapQUdpConnectionPrivate &dd, QObject switch (d->securityMode) { case QtCoap::RawPublicKey: - qWarning("QtCoap: RawPublicKey security is not supported yet, disabling security"); + qCWarning(lcCoapConnection, "RawPublicKey security is not supported yet," + "disabling security"); d->securityMode = QtCoap::NoSec; break; case QtCoap::PreSharedKey: @@ -121,7 +125,7 @@ QCoapQUdpConnection::QCoapQUdpConnection(QCoapQUdpConnectionPrivate &dd, QObject break; } #else - qWarning("QtCoap: DTLS is disabled, falling back to QtCoap::NoSec mode."); + qCWarning(lcCoapConnection, "DTLS is disabled, falling back to QtCoap::NoSec mode."); d->securityMode = QtCoap::NoSec; #endif } @@ -145,7 +149,8 @@ void QCoapQUdpConnection::createSocket() }); connect(d->udpSocket, QOverload<QAbstractSocket::SocketError>::of(&QUdpSocket::error), [this](QAbstractSocket::SocketError socketError) { - qWarning() << "CoAP UDP socket error" << socketError << socket()->errorString(); + qCWarning(lcCoapConnection) << "CoAP UDP socket error" << socketError + << socket()->errorString(); emit error(socketError); }); } @@ -206,7 +211,7 @@ void QCoapQUdpConnection::bind(const QString &host, quint16 port) socket()->bind(); d->dtls->setPeer(QHostAddress(host), port); if (!d->dtls->doHandshake(d->socket())) - qWarning() << "QtCoap: handshake error: " << d->dtls->dtlsErrorString(); + qCWarning(lcCoapConnection) << "Handshake error: " << d->dtls->dtlsErrorString(); } #else Q_UNUSED(host); @@ -247,15 +252,15 @@ void QCoapQUdpConnectionPrivate::writeToSocket(const QByteArray &data, const QSt if (!socket()->isWritable()) { bool opened = socket()->open(socket()->openMode() | QIODevice::WriteOnly); if (!opened) { - qWarning("QtCoap: Failed to open the UDP socket with write permission"); + qCWarning(lcCoapConnection, "Failed to open the UDP socket with write permission"); return; } } QHostAddress hostAddress(host); if (hostAddress.isNull()) { - qWarning() << "QtCoap: Invalid host IP address" << host - << "- only IPv4/IPv6 destination addresses are supported."; + qCWarning(lcCoapConnection) << "Invalid host IP address" << host + << "- only IPv4/IPv6 destination addresses are supported."; return; } @@ -266,7 +271,7 @@ void QCoapQUdpConnectionPrivate::writeToSocket(const QByteArray &data, const QSt socket()->writeDatagram(data, hostAddress, port); if (bytesWritten < 0) - qWarning() << "QtCoap: Failed to write datagram:" << socket()->errorString(); + qCWarning(lcCoapConnection) << "Failed to write datagram:" << socket()->errorString(); } /*! @@ -282,7 +287,7 @@ void QCoapQUdpConnectionPrivate::socketReadyRead() if (!socket()->isReadable()) { bool opened = socket()->open(socket()->openMode() | QIODevice::ReadOnly); if (!opened) { - qWarning("QtCoap: Failed to open the UDP socket with read permission"); + qCWarning(lcCoapConnection, "Failed to open the UDP socket with read permission"); return; } } @@ -343,7 +348,7 @@ void QCoapQUdpConnectionPrivate::setSecurityConfiguration( QSslKey opaqueKey(configuration.privateKey().handle()); dtlsConfig.setPrivateKey(opaqueKey); } else { - qWarning("QtCoap: Failed to set private key, the provided key is invalid"); + qCWarning(lcCoapConnection, "Failed to set private key, the provided key is invalid"); } } @@ -377,9 +382,9 @@ void QCoapQUdpConnection::handshakeTimeout() { Q_D(QCoapQUdpConnection); - qWarning("QtCoap: handshake timeout, trying to re-transmit"); + qCWarning(lcCoapConnection, "Handshake timeout, trying to re-transmit"); if (!d->dtls->handleTimeout(d->udpSocket)) - qWarning() << "QtCoap: failed to re-transmit" << d->dtls->dtlsErrorString(); + qCWarning(lcCoapConnection) << "Failed to re-transmit" << d->dtls->dtlsErrorString(); } /*! @@ -413,7 +418,7 @@ void QCoapQUdpConnectionPrivate::handleEncryptedDatagram() emit q->readyRead(datagram.data(), datagram.senderAddress()); } else { if (!dtls->doHandshake(socket(), socket()->receiveDatagram().data())) { - qWarning() << "QtCoap: handshake error: " << dtls->dtlsErrorString(); + qCWarning(lcCoapConnection) << "Handshake error: " << dtls->dtlsErrorString(); return; } diff --git a/src/coap/qcoapreply.cpp b/src/coap/qcoapreply.cpp index f3db863..1770deb 100644 --- a/src/coap/qcoapreply.cpp +++ b/src/coap/qcoapreply.cpp @@ -31,9 +31,12 @@ #include "qcoapreply_p.h" #include "qcoapinternalreply_p.h" #include <QtCore/qmath.h> +#include <QtCore/qloggingcategory.h> QT_BEGIN_NAMESPACE +Q_DECLARE_LOGGING_CATEGORY(lcCoapExchange) + /*! \internal Constructor. @@ -286,9 +289,9 @@ qint64 QCoapReply::readData(char *data, qint64 maxSize) size_t len = static_cast<size_t>(maxSize); if (sizeof(qint64) > sizeof(size_t) && maxSize > static_cast<qint64>(std::numeric_limits<size_t>::max())) { - qWarning() << "QCoapReply::readData: Cannot read more than" - << std::numeric_limits<size_t>::max() - << "at a time"; + qCWarning(lcCoapExchange) << "Cannot read more than" + << std::numeric_limits<size_t>::max() + << "at a time"; len = std::numeric_limits<size_t>::max(); } diff --git a/src/coap/qcoaprequest.cpp b/src/coap/qcoaprequest.cpp index c1ab691..64e0963 100644 --- a/src/coap/qcoaprequest.cpp +++ b/src/coap/qcoaprequest.cpp @@ -32,10 +32,13 @@ #include <QtCore/qmath.h> #include <QtCore/qdatetime.h> +#include <QtCore/qloggingcategory.h> #include <QtCore/QDebug> QT_BEGIN_NAMESPACE +Q_DECLARE_LOGGING_CATEGORY(lcCoapExchange) + namespace { const auto CoapScheme = QLatin1String("coap"); const auto CoapSecureScheme = QLatin1String("coaps"); @@ -69,7 +72,7 @@ void QCoapRequestPrivate::setUrl(const QUrl &url) // Make first checks before editing the URL, to avoid editing it // in a wrong way (e.g. when adding the scheme) if (!url.isValid()) { - qWarning() << "QCoapRequest: Invalid CoAP url" << url.toString(); + qCWarning(lcCoapExchange) << "Invalid CoAP url" << url.toString(); return; } @@ -83,8 +86,8 @@ void QCoapRequestPrivate::setUrl(const QUrl &url) if (url.port() == -1) finalizedUrl.setPort(QtCoap::DefaultSecurePort); } else { - qWarning() << "QCoapRequest: Request URL's scheme" << url.scheme() - << "isn't valid for CoAP"; + qCWarning(lcCoapExchange) << "QCoapRequest: Request URL's scheme" << url.scheme() + << "isn't valid for CoAP"; return; } } |