diff options
author | Kari Oikarinen <kari.oikarinen@qt.io> | 2016-11-21 14:51:22 +0200 |
---|---|---|
committer | Kari Oikarinen <kari.oikarinen@qt.io> | 2016-11-23 12:36:24 +0000 |
commit | 2750685b372b78330a9139a98d084bb7a60e367e (patch) | |
tree | 138d48b393bd1d060dcaf4755a9371a2239364d5 | |
parent | fd46f3107674723545e86a9b145c107d33d14f72 (diff) |
Categorize logging
Also use qFormatLogMessage so that QT_MESSAGE_PATTERN is taken into
account in host server's own log message handler.
Change-Id: I2ae6f70021cbe1ce4cd31ed599bbcc5035081b12
Reviewed-by: Samuli Piippo <samuli.piippo@qt.io>
-rw-r--r-- | libqdb/protocol/qdbtransport.cpp | 6 | ||||
-rw-r--r-- | libqdb/stream.cpp | 1 | ||||
-rw-r--r-- | qdb/client/client.cpp | 4 | ||||
-rw-r--r-- | qdb/server/connection.cpp | 24 | ||||
-rw-r--r-- | qdb/server/deviceinformationfetcher.cpp | 17 | ||||
-rw-r--r-- | qdb/server/devicemanager.cpp | 23 | ||||
-rw-r--r-- | qdb/server/echoservice.cpp | 5 | ||||
-rw-r--r-- | qdb/server/handshakeservice.cpp | 5 | ||||
-rw-r--r-- | qdb/server/hostserver.cpp | 18 | ||||
-rw-r--r-- | qdb/server/hostservlet.cpp | 30 | ||||
-rw-r--r-- | qdb/server/logging.cpp | 4 | ||||
-rw-r--r-- | qdb/server/networkmanagercontrol.cpp | 88 | ||||
-rw-r--r-- | qdb/server/service.cpp | 2 | ||||
-rw-r--r-- | qdb/server/usb-host/libusbcontext.cpp | 5 | ||||
-rw-r--r-- | qdb/server/usb-host/usbconnection.cpp | 20 | ||||
-rw-r--r-- | qdb/server/usb-host/usbconnectionreader.cpp | 5 | ||||
-rw-r--r-- | qdb/server/usb-host/usbdeviceenumerator.cpp | 15 | ||||
-rw-r--r-- | qdbd/echoexecutor.cpp | 5 | ||||
-rw-r--r-- | qdbd/handshakeexecutor.cpp | 13 | ||||
-rw-r--r-- | qdbd/main.cpp | 8 | ||||
-rw-r--r-- | qdbd/server.cpp | 24 | ||||
-rw-r--r-- | qdbd/usb-gadget/usbgadget.cpp | 21 | ||||
-rw-r--r-- | qdbd/usb-gadget/usbgadgetreader.cpp | 13 | ||||
-rw-r--r-- | qdbd/usb-gadget/usbgadgetwriter.cpp | 7 |
24 files changed, 204 insertions, 159 deletions
diff --git a/libqdb/protocol/qdbtransport.cpp b/libqdb/protocol/qdbtransport.cpp index d95432b..43353b0 100644 --- a/libqdb/protocol/qdbtransport.cpp +++ b/libqdb/protocol/qdbtransport.cpp @@ -26,7 +26,7 @@ #include <QtCore/qdebug.h> #include <QtCore/qloggingcategory.h> -Q_LOGGING_CATEGORY(transportC, "transport"); +Q_LOGGING_CATEGORY(transportC, "qdb.transport"); QdbTransport::QdbTransport(QIODevice *io) : m_io{io} @@ -54,7 +54,7 @@ bool QdbTransport::send(const QdbMessage &message) int count = m_io->write(buf.constData(), messageSize); if (count != messageSize) { - qCritical() << "QdbTransport::send() could not write entire message of" << messageSize << ", only wrote" << count; + qCCritical(transportC) << "Could not write entire message of" << messageSize << "bytes, only wrote" << count; return false; } @@ -67,7 +67,7 @@ QdbMessage QdbTransport::receive() QByteArray buf{qdbMessageSize, '\0'}; int count = m_io->read(buf.data(), buf.size()); if (count < qdbHeaderSize) { - qDebug() << "QdbTransport::receive() could only read" << count << "out of package header's" << qdbHeaderSize; + qCCritical(transportC) << "Could only read" << count << "bytes out of package header's" << qdbHeaderSize; return QdbMessage{QdbMessage::Invalid, 0, 0}; } QDataStream stream{buf}; diff --git a/libqdb/stream.cpp b/libqdb/stream.cpp index 940d16b..eabe905 100644 --- a/libqdb/stream.cpp +++ b/libqdb/stream.cpp @@ -24,7 +24,6 @@ #include "protocol/protocol.h" #include <QtCore/qdatastream.h> -#include <QtCore/qdebug.h> QByteArray wrapPacket(const StreamPacket &packet) { diff --git a/qdb/client/client.cpp b/qdb/client/client.cpp index 831884d..4381e48 100644 --- a/qdb/client/client.cpp +++ b/qdb/client/client.cpp @@ -188,8 +188,8 @@ void Client::handleWatchMessage() const auto type = responseType(document.object()); if (type != ResponseType::NewDevice && type != ResponseType::DisconnectedDevice) { - qDebug() << "Shutting down due to unexpected response:" << responseBytes; - shutdown(0); + std::cerr << "Shutting down due to unexpected response:" << responseBytes.data(); + shutdown(1); } } } diff --git a/qdb/server/connection.cpp b/qdb/server/connection.cpp index b536db5..c2d7edd 100644 --- a/qdb/server/connection.cpp +++ b/qdb/server/connection.cpp @@ -28,7 +28,7 @@ #include <QtCore/qdebug.h> #include <QtCore/qloggingcategory.h> -Q_LOGGING_CATEGORY(connectionC, "connection"); +Q_LOGGING_CATEGORY(connectionC, "qdb.connection"); Connection::Connection(QdbTransport *transport, QObject *parent) : AbstractConnection{transport, parent}, @@ -104,19 +104,19 @@ void Connection::handleMessage() qFatal("Connection received QdbMessage::Open, which is not supported!"); if (message.command() == QdbMessage::Invalid) { - qCritical("Connection received invalid message!"); + qCCritical(connectionC) << "Connection received invalid message!"; resetConnection(false); return; } switch (m_state) { case ConnectionState::Disconnected: - qWarning() << "Connection got a message in Disconnected state"; + qCWarning(connectionC) << "Connection got a message in Disconnected state"; resetConnection(true); break; case ConnectionState::WaitingForConnection: if (message.command() != QdbMessage::Connect) { - qWarning() << "Connection got a non-Connect message in WaitingForConnection state"; + qCWarning(connectionC) << "Connection got a non-Connect message in WaitingForConnection state"; resetConnection(true); break; } @@ -128,7 +128,7 @@ void Connection::handleMessage() case ConnectionState::Connected: switch (message.command()) { case QdbMessage::Connect: - qWarning() << "Connection received QdbMessage::Connect while already connected. Reconnecting."; + qCWarning(connectionC) << "Connection received QdbMessage::Connect while already connected. Reconnecting."; resetConnection(true); break; case QdbMessage::Write: @@ -138,7 +138,7 @@ void Connection::handleMessage() closeStream(message.hostStream()); break; case QdbMessage::Ok: - qWarning() << "Connection received QdbMessage::Ok in connected state"; + qCWarning(connectionC) << "Connection received QdbMessage::Ok in connected state"; break; case QdbMessage::Open: //[[fallthrough]] @@ -150,7 +150,7 @@ void Connection::handleMessage() case ConnectionState::Waiting: switch (message.command()) { case QdbMessage::Connect: - qWarning() << "Connection received QdbMessage::Connect while already connected and waiting. Reconnecting."; + qCWarning(connectionC) << "Connection received QdbMessage::Connect while already connected and waiting. Reconnecting."; resetConnection(true); break; case QdbMessage::Write: @@ -184,7 +184,7 @@ void Connection::acknowledge(StreamId hostId, StreamId deviceId) QdbMessage message{QdbMessage::Ok, hostId, deviceId}; if (!m_transport->send(message)) { - qCritical() << "Connection could not send" << message; + qCCritical(connectionC) << "Connection could not send" << message; resetConnection(false); return; } @@ -212,7 +212,7 @@ void Connection::processQueue() "Tried to send invalid message"); if (!m_transport->send(message)) { - qCritical() << "Connection could not send" << message; + qCCritical(connectionC) << "Connection could not send" << message; resetConnection(false); return; } @@ -283,7 +283,7 @@ void Connection::finishCreateStream(StreamId hostId, StreamId deviceId) void Connection::handleWrite(const QdbMessage &message) { if (m_streams.find(message.hostStream()) == m_streams.end()) { - qWarning() << "Connection received message to non-existing stream" << message.hostStream(); + qCWarning(connectionC) << "Connection received message to non-existing stream" << message.hostStream(); enqueueMessage(QdbMessage{QdbMessage::Close, message.hostStream(), message.deviceStream()}); return; } @@ -301,8 +301,8 @@ bool Connection::checkVersion(const QdbMessage &message) dataStream >> protocolVersion; if (protocolVersion != qdbProtocolVersion) { - qCritical() << "Device offered protocol version" << protocolVersion << ", but only version" - << qdbProtocolVersion << "is supported"; + qCCritical(connectionC) << "Device offered protocol version" << protocolVersion + << ", but only version" << qdbProtocolVersion << "is supported"; return false; } return true; diff --git a/qdb/server/deviceinformationfetcher.cpp b/qdb/server/deviceinformationfetcher.cpp index 23fd9a6..9a85b7c 100644 --- a/qdb/server/deviceinformationfetcher.cpp +++ b/qdb/server/deviceinformationfetcher.cpp @@ -26,6 +26,9 @@ #include "usb-host/usbconnection.h" #include <QtCore/qdebug.h> +#include <QtCore/qloggingcategory.h> + +Q_LOGGING_CATEGORY(deviceInfoC, "qdb.devices.info"); bool operator==(const DeviceInformation &lhs, const DeviceInformation &rhs) { @@ -49,19 +52,19 @@ DeviceInformationFetcher::DeviceInformationFetcher(UsbDevice device) connect(this, &DeviceInformationFetcher::fetched, m_connection, &QObject::deleteLater); if (!m_connection->initialize()) { - qCritical() << "DeviceInformationFetcher: Could not initialize connection to" << device.serial; + qCCritical(deviceInfoC) << "Could not initialize connection to" << device.serial << "for fetching device information"; return; } m_connection->connect(); m_connected = true; - qDebug() << "Initialized connection to" << device.serial; + qCDebug(deviceInfoC) << "Initialized connection to" << device.serial; } void DeviceInformationFetcher::fetch() { if (!m_connected) { - qDebug() << "Not fetching device information due to no connection"; + qCWarning(deviceInfoC) << "Not fetching device information due to no connection"; emit fetched(DeviceInformation{"", "", "", m_deviceAddress}); return; } @@ -81,9 +84,9 @@ void DeviceInformationFetcher::fetch() void DeviceInformationFetcher::handshakeResponse(QString serial, QString hostMac, QString ipAddress) { - qDebug() << "Handshakeservice responded:"; - qDebug() << " Device serial:" << serial; - qDebug() << " Host-side MAC address:" << hostMac; - qDebug() << " Device IP address:" << ipAddress; + qCDebug(deviceInfoC) << "Fetched device information:"; + qCDebug(deviceInfoC) << " Device serial:" << serial; + qCDebug(deviceInfoC) << " Host-side MAC address:" << hostMac; + qCDebug(deviceInfoC) << " Device IP address:" << ipAddress; emit fetched(DeviceInformation{serial, hostMac, ipAddress, m_deviceAddress}); } diff --git a/qdb/server/devicemanager.cpp b/qdb/server/devicemanager.cpp index 09dd89a..3314546 100644 --- a/qdb/server/devicemanager.cpp +++ b/qdb/server/devicemanager.cpp @@ -23,8 +23,11 @@ #include "networkmanagercontrol.h" #include <QtCore/qdebug.h> +#include <QtCore/qloggingcategory.h> #include <QtDBus/QDBusObjectPath> +Q_LOGGING_CATEGORY(devicesC, "qdb.devices"); + DeviceManager::DeviceManager(QObject *parent) : QObject{parent}, m_deviceEnumerator{}, @@ -53,19 +56,19 @@ void DeviceManager::handleDeviceInformation(DeviceInformation deviceInfo) { m_fetching = false; if (deviceInfo.hostMac.isEmpty()) { - qWarning() << "Could not fetch device information from" << m_fetchingDevice.serial; + qCWarning(devicesC) << "Could not fetch device information from" << m_fetchingDevice.serial; return; // Discard the device } - qDebug() << "Configuring network for" << deviceInfo.serial; + qCDebug(devicesC) << "Configuring network for" << deviceInfo.serial; configureUsbNetwork(deviceInfo.serial, deviceInfo.hostMac); if (deviceInfo.ipAddress.isEmpty()) { - qDebug() << "Incomplete information received for" << deviceInfo.serial; + qCDebug(devicesC) << "Incomplete information received for" << deviceInfo.serial; m_incompleteDevices.enqueue(m_fetchingDevice); QTimer::singleShot(1000, this, &DeviceManager::fetchIncomplete); } else { - qDebug() << "Complete info received for" << deviceInfo.serial; + qCDebug(devicesC) << "Complete info received for" << deviceInfo.serial; } auto iter = std::find_if(m_deviceInfos.begin(), m_deviceInfos.end(), @@ -73,11 +76,11 @@ void DeviceManager::handleDeviceInformation(DeviceInformation deviceInfo) return deviceInfo.serial == oldInfo.serial; }); if (iter == m_deviceInfos.end()) { - qDebug() << "Added new info for" << deviceInfo.serial; + qCDebug(devicesC) << "Added new info for" << deviceInfo.serial; m_deviceInfos.push_back(deviceInfo); emit newDeviceInfo(deviceInfo); } else if (*iter != deviceInfo) { - qDebug() << "Replaced old info for" << deviceInfo.serial; + qCDebug(devicesC) << "Replaced old info for" << deviceInfo.serial; *iter = deviceInfo; emit newDeviceInfo(deviceInfo); } @@ -88,7 +91,7 @@ void DeviceManager::handleDeviceInformation(DeviceInformation deviceInfo) void DeviceManager::handlePluggedInDevice(UsbDevice device) { - qDebug() << "Device" << device.serial << "plugged in at" << device.address.busNumber << ":" << device.address.deviceAddress; + qCDebug(devicesC) << "Device" << device.serial << "plugged in at" << device.address.busNumber << ":" << device.address.deviceAddress; if (m_fetching) m_newDevices.enqueue(device); else @@ -97,7 +100,7 @@ void DeviceManager::handlePluggedInDevice(UsbDevice device) void DeviceManager::handleUnpluggedDevice(UsbAddress address) { - qDebug() << "Device unplugged from" << address.busNumber << ":" << address.deviceAddress; + qCDebug(devicesC) << "Device unplugged from" << address.busNumber << ":" << address.deviceAddress; auto deviceAddressMatches = [&](const UsbDevice &device) { return device.address == address; @@ -128,7 +131,7 @@ void DeviceManager::handleUnpluggedDevice(UsbAddress address) void DeviceManager::fetchDeviceInformation(UsbDevice device) { - qDebug() << "Fetching device information for" << device.serial; + qCDebug(devicesC) << "Fetching device information for" << device.serial; Q_ASSERT(!m_fetching); m_fetching = true; m_fetchingDevice = device; @@ -145,7 +148,7 @@ void DeviceManager::fetchIncomplete() return; if (m_fetching) { - qDebug() << "Delaying fetching incomplete information due to fetch in progress"; + qCDebug(devicesC) << "Delaying fetching incomplete information due to fetch in progress"; QTimer::singleShot(500, this, &DeviceManager::fetchIncomplete); return; } diff --git a/qdb/server/echoservice.cpp b/qdb/server/echoservice.cpp index b69a7c2..0999b85 100644 --- a/qdb/server/echoservice.cpp +++ b/qdb/server/echoservice.cpp @@ -25,6 +25,9 @@ #include "libqdb/stream.h" #include <QtCore/qdebug.h> +#include <QtCore/qloggingcategory.h> + +Q_LOGGING_CATEGORY(echoC, "qdb.services.echo"); EchoService::EchoService(Connection *connection) : m_connection{connection} @@ -53,7 +56,7 @@ bool EchoService::hasStream() const void EchoService::send(const QString &string) { if (!m_stream) { - qCritical() << "No valid stream in EchoService when trying to send"; + qCCritical(echoC) << "No valid stream in EchoService when trying to send"; return; } StreamPacket packet{string.toUtf8()}; diff --git a/qdb/server/handshakeservice.cpp b/qdb/server/handshakeservice.cpp index 48a5124..c9f3df1 100644 --- a/qdb/server/handshakeservice.cpp +++ b/qdb/server/handshakeservice.cpp @@ -25,6 +25,9 @@ #include "libqdb/stream.h" #include <QtCore/qdebug.h> +#include <QtCore/qloggingcategory.h> + +Q_LOGGING_CATEGORY(handshakeC, "qdb.services.handshake"); HandshakeService::HandshakeService(Connection *connection) : m_connection{connection}, @@ -54,7 +57,7 @@ bool HandshakeService::hasStream() const void HandshakeService::ask() { if (!m_stream) { - qCritical() << "No valid stream in HandshakeService when trying to send"; + qCCritical(handshakeC) << "No valid stream in HandshakeService when trying to send"; return; } StreamPacket packet{}; diff --git a/qdb/server/hostserver.cpp b/qdb/server/hostserver.cpp index 0d436ba..93feec2 100644 --- a/qdb/server/hostserver.cpp +++ b/qdb/server/hostserver.cpp @@ -30,6 +30,8 @@ #include <QtCore/qloggingcategory.h> #include <QtNetwork/qlocalsocket.h> +Q_LOGGING_CATEGORY(hostServerC, "qdb.hostserver"); + int execHostServer(const QCoreApplication &app, const QCommandLineParser &parser) { setupLogging(); @@ -67,13 +69,13 @@ void HostServer::listen() QFile::remove(socketPath); #endif if (!m_localServer.listen(qdbSocketName)) { - qCritical() << "Could not start listening with QLocalServer: " - << m_localServer.errorString(); + qCCritical(hostServerC) << "Could not start listening with QLocalServer: " + << m_localServer.errorString(); close(); return; } connect(&m_localServer, &QLocalServer::newConnection, this, &HostServer::handleClient); - qDebug() << "Host server started listening."; + qCDebug(hostServerC) << "Started listening"; connect(&m_deviceManager, &DeviceManager::newDeviceInfo, this, &HostServer::handleNewDeviceInfo); connect(&m_deviceManager, &DeviceManager::disconnectedDevice, this, &HostServer::handleDisconnectedDevice); @@ -82,7 +84,7 @@ void HostServer::listen() void HostServer::close() { - qDebug() << "Shutting QDB host server down"; + qCDebug(hostServerC) << "Shutting down"; m_localServer.close(); while (!m_servlets.empty()) m_servlets.front().close(); // closing results in being erased from the list @@ -93,7 +95,7 @@ void HostServer::handleClient() { QLocalSocket *socket = m_localServer.nextPendingConnection(); if (!socket) { - qCritical() << "Did not get a connection from client"; + qCCritical(hostServerC) << "Did not get a connection from client"; close(); return; } @@ -116,15 +118,15 @@ void HostServer::handleDoneClient(ServletId servletId) if (iter != m_servlets.end()) m_servlets.erase(iter); else - qWarning() << "Could not find done servlet" << servletId << "when trying to remove it"; + qCWarning(hostServerC) << "Could not find done servlet" << servletId << "when trying to remove it"; } void HostServer::handleNewDeviceInfo(DeviceInformation info) { - qDebug() << "New device information about" << info.serial; + qCDebug(hostServerC) << "New device information about" << info.serial; } void HostServer::handleDisconnectedDevice(QString serial) { - qDebug() << "Disconnected" << serial; + qCDebug(hostServerC) << "Disconnected" << serial; } diff --git a/qdb/server/hostservlet.cpp b/qdb/server/hostservlet.cpp index 4bd5c2d..8e34285 100644 --- a/qdb/server/hostservlet.cpp +++ b/qdb/server/hostservlet.cpp @@ -24,8 +24,11 @@ #include <QtCore/qjsonarray.h> #include <QtCore/qjsonobject.h> +#include <QtCore/qloggingcategory.h> #include <QtNetwork/qlocalsocket.h> +Q_DECLARE_LOGGING_CATEGORY(hostServerC); + QJsonObject deviceInformationToJsonObject(const DeviceInformation &deviceInfo) { QJsonObject info; @@ -73,6 +76,7 @@ void HostServlet::handleDisconnection() void HostServlet::handleRequest() { + qCDebug(hostServerC) << "Got request from client" << m_id; const auto requestBytes = m_socket->readLine(); const auto request = QJsonDocument::fromJson(requestBytes); const auto type = requestType(request.object()); @@ -88,7 +92,7 @@ void HostServlet::handleRequest() stopServer(); break; case RequestType::Unknown: - qWarning() << "Got invalid request from client:" << requestBytes; + qCWarning(hostServerC) << "Request from client" << m_id << "is invalid:" << requestBytes; const QJsonObject response = initializeResponse(ResponseType::InvalidRequest); m_socket->write(serialiseResponse(response)); close(); @@ -107,11 +111,11 @@ void HostServlet::replyDevices() response["devices"] = infoArray; if (!m_socket || !m_socket->isWritable()) { - qWarning() << "Could not reply to the client"; + qCWarning(hostServerC) << "Could not reply to client" << m_id; return; } m_socket->write(serialiseResponse(response)); - qDebug() << "Replied device information to the client"; + qCDebug(hostServerC) << "Replied device information to client" << m_id; close(); } @@ -121,11 +125,11 @@ void HostServlet::replyNewDevice(const DeviceInformation &deviceInfo) response["device"] = deviceInformationToJsonObject(deviceInfo); if (!m_socket || !m_socket->isWritable()) { - qWarning() << "Could not reply to the client"; + qCWarning(hostServerC) << "Could not send new device information to client" << m_id; return; } m_socket->write(serialiseResponse(response)); - qDebug() << "Sent new device information to the client"; + qCDebug(hostServerC) << "Sent new device information to client" << m_id; } void HostServlet::replyDisconnectedDevice(const QString &serial) @@ -134,23 +138,23 @@ void HostServlet::replyDisconnectedDevice(const QString &serial) response["serial"] = serial; if (!m_socket || !m_socket->isWritable()) { - qWarning() << "Could not reply to the client"; + qCWarning(hostServerC) << "Could not send disconnected device information to client" << m_id; return; } m_socket->write(serialiseResponse(response)); - qDebug() << "Sent disconnected device information to the client"; + qCDebug(hostServerC) << "Sent disconnected device information to client" << m_id; } void HostServlet::startWatchingDevices() { - qDebug() << "Starting to watch devices"; + qCDebug(hostServerC) << "Starting to watch devices for client" << m_id; connect(&m_deviceManager, &DeviceManager::newDeviceInfo, this, &HostServlet::replyNewDevice); connect(&m_deviceManager, &DeviceManager::disconnectedDevice, this, &HostServlet::replyDisconnectedDevice); const auto deviceInfos = m_deviceManager.listDevices(); for (const auto &deviceInfo : deviceInfos) replyNewDevice(deviceInfo); - qDebug() << "Reported initial devices to watcher"; + qCDebug(hostServerC) << "Reported initial devices to client" << m_id; } void HostServlet::stopServer() @@ -158,11 +162,11 @@ void HostServlet::stopServer() QJsonObject response = initializeResponse(ResponseType::Stopping); if (!m_socket || !m_socket->isWritable()) { - qWarning() << "Could not reply to the client"; - return; + qCWarning(hostServerC) << "Could not acknowledge stopping to client" << m_id; + } else { + m_socket->write(serialiseResponse(response)); + qCDebug(hostServerC) << "Acknowledged stopping to client" << m_id; } - m_socket->write(serialiseResponse(response)); - qDebug() << "Acknowledged stopping"; emit serverStopRequested(); // All servlets, including this one will be closed during shutdown diff --git a/qdb/server/logging.cpp b/qdb/server/logging.cpp index 6566537..0c7d0c6 100644 --- a/qdb/server/logging.cpp +++ b/qdb/server/logging.cpp @@ -31,7 +31,6 @@ static QFile logFile; void hostServerMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { - Q_UNUSED(context); if (!logFile.isWritable()) { if (!logFile.open(QFile::WriteOnly | QIODevice::Unbuffered)) { // Fall back to default handler @@ -64,7 +63,8 @@ void hostServerMessageHandler(QtMsgType type, const QMessageLogContext &context, break; } - auto fullMsg = QString{"%1 %2\n"}.arg(prefix).arg(msg).toUtf8(); + const auto message = qFormatLogMessage(type, context, msg); + const auto fullMsg = QString{"%1 %2\n"}.arg(prefix).arg(message).toUtf8(); auto written = logFile.write(fullMsg); if (written != fullMsg.size()) { qInstallMessageHandler(nullptr); diff --git a/qdb/server/networkmanagercontrol.cpp b/qdb/server/networkmanagercontrol.cpp index 32fe031..162e620 100644 --- a/qdb/server/networkmanagercontrol.cpp +++ b/qdb/server/networkmanagercontrol.cpp @@ -20,10 +20,13 @@ ******************************************************************************/ #include "networkmanagercontrol.h" +#include <QtCore/qloggingcategory.h> #include <QtDBus> #include <algorithm> +Q_LOGGING_CATEGORY(networkC, "qdb.network"); + using SettingsMap = QMap<QString, QMap<QString, QDBusVariant>>; const QString networkManagerServiceName = "org.freedesktop.NetworkManager"; @@ -35,23 +38,23 @@ SettingsMap demarshallSettings(const QDBusArgument &settingsArgument); void configureUsbNetwork(const QString &serial, const QString &macAddress) { - qDebug() << "Configuring network for" << serial << "at" << macAddress; + qCDebug(networkC) << "Configuring network for" << serial << "at" << macAddress; NetworkManagerControl networkManager; auto deviceResult = networkManager.findNetworkDeviceByMac(macAddress); if (!deviceResult.isValid()) { - qWarning() << "Could not find network device" << macAddress; + qCWarning(networkC) << "Could not find network device" << macAddress; return; } else { const auto networkCard = deviceResult.toString(); if (networkManager.isActivated(networkCard)) { - qDebug() << networkCard << "is activated"; + qCDebug(networkC) << networkCard << "is activated"; if (networkManager.isDeviceUsingLinkLocal(networkCard)) { - qInfo() << networkCard << "is already using a link-local IP"; + qCInfo(networkC) << networkCard << "is already using a link-local IP"; return; } } if (!networkManager.activateOrCreateConnection(QDBusObjectPath{networkCard}, serial, macAddress)) - qWarning() << "Could not setup network settings for the USB Ethernet interface"; + qCWarning(networkC) << "Could not setup network settings for the USB Ethernet interface"; } } @@ -60,7 +63,7 @@ QVariant connectionMac(QDBusConnection &bus, const QDBusObjectPath &connectionPa const auto result = connectionSettings(bus, connectionPath); if (!result.isValid()) { - qWarning() << "Could not get MAC address for" << connectionPath.path(); + qCWarning(networkC) << "Could not get MAC address for" << connectionPath.path(); return QVariant{}; } const auto settings = result.value<SettingsMap>(); @@ -71,7 +74,7 @@ QVariant connectionMac(QDBusConnection &bus, const QDBusObjectPath &connectionPa QVariant connectionSettings(QDBusConnection &bus, const QDBusObjectPath &connectionPath) { if (!bus.isConnected()) { - qWarning() << "Could not connect to D-Bus system bus: " << bus.lastError(); + qCWarning(networkC) << "Could not connect to D-Bus system bus: " << bus.lastError(); return QVariant{}; } QDBusInterface connectionInterface{networkManagerServiceName, @@ -79,13 +82,13 @@ QVariant connectionSettings(QDBusConnection &bus, const QDBusObjectPath &connect networkManagerInterfaceName + ".Settings.Connection", bus}; if (!connectionInterface.isValid()) { - qWarning() << "Could not find NetworkManager Connection:" << connectionPath.path() << connectionInterface.lastError(); + qCWarning(networkC) << "Could not find NetworkManager Connection:" << connectionPath.path() << connectionInterface.lastError(); return QVariant{}; } QDBusMessage result = connectionInterface.call("GetSettings"); if (result.type() != QDBusMessage::ReplyMessage) { - qWarning() << "Could not get connection settings for" << connectionPath.path() << ":" << result.errorMessage(); + qCWarning(networkC) << "Could not get connection settings for" << connectionPath.path() << ":" << result.errorMessage(); return QVariant{}; } Q_ASSERT(result.arguments().size() == 1); @@ -96,7 +99,7 @@ QVariant connectionSettings(QDBusConnection &bus, const QDBusObjectPath &connect QVariant connectionSettingsFromDevice(QDBusConnection &bus, const QString &devicePath) { if (!bus.isConnected()) { - qWarning() << "Could not connect to D-Bus system bus: " << bus.lastError(); + qCWarning(networkC) << "Could not connect to D-Bus system bus: " << bus.lastError(); return QVariant{}; } QDBusInterface deviceInterface{networkManagerServiceName, @@ -104,13 +107,13 @@ QVariant connectionSettingsFromDevice(QDBusConnection &bus, const QString &devic networkManagerInterfaceName + ".Device", bus}; if (!deviceInterface.isValid()) { - qWarning() << "Could not find NetworkManager Device:" << devicePath << deviceInterface.lastError(); + qCWarning(networkC) << "Could not find NetworkManager Device:" << devicePath << deviceInterface.lastError(); return QVariant{}; } QDBusMessage result = deviceInterface.call("GetAppliedConnection", 0u); if (result.type() != QDBusMessage::ReplyMessage) { - qWarning() << "Could not get connection settings for" << devicePath << ":" << result.errorMessage(); + qCWarning(networkC) << "Could not get connection settings for" << devicePath << ":" << result.errorMessage(); return QVariant{}; } Q_ASSERT(result.arguments().size() == 2); @@ -152,10 +155,10 @@ NetworkManagerControl::NetworkManagerControl() bool NetworkManagerControl::activateOrCreateConnection(const QDBusObjectPath &devicePath, const QString &serial, const QString &macAddress) { - qDebug() << "Activating or creating a connection"; + qCDebug(networkC) << "Activating or creating a connection"; const auto connectionsResult = findConnectionsByMac(macAddress); if (!connectionsResult.isValid()) { - qWarning() << "Could not list NetworkManager connections"; + qCWarning(networkC) << "Could not list NetworkManager connections"; return false; } const auto connections = connectionsResult.value<QList<QDBusObjectPath>>(); @@ -166,17 +169,17 @@ bool NetworkManagerControl::activateOrCreateConnection(const QDBusObjectPath &de QDBusObjectPath connectionPath; if (it != connections.end()) { - qDebug() << "Found existing connection:" << it->path(); + qCDebug(networkC) << "Found existing connection:" << it->path(); connectionPath = *it; } else { - qDebug() << "Creating new connection"; + qCDebug(networkC) << "Creating new connection"; const auto result = createConnection(serial, macAddress); if (!result.isValid()) { - qWarning() << "Could not create a NetworkManager connection for" << macAddress; + qCWarning(networkC) << "Could not create a NetworkManager connection for" << macAddress; return false; } connectionPath = result.value<QDBusObjectPath>(); - qDebug() << "New connection:" << connectionPath.path(); + qCDebug(networkC) << "New connection:" << connectionPath.path(); } QDBusInterface networkManagerInterface{networkManagerServiceName, @@ -185,8 +188,8 @@ bool NetworkManagerControl::activateOrCreateConnection(const QDBusObjectPath &de m_bus}; if (!networkManagerInterface.isValid()) { - qWarning() << "Could not find NetworkManager D-Bus interface:" - << networkManagerInterface.lastError(); + qCWarning(networkC) << "Could not find NetworkManager D-Bus interface:" + << networkManagerInterface.lastError(); return false; } @@ -197,7 +200,7 @@ bool NetworkManagerControl::activateOrCreateConnection(const QDBusObjectPath &de if (response.type() != QDBusMessage::ReplyMessage) return false; - qDebug() << "Successfully activated" << connectionPath.path(); + qCDebug(networkC) << "Successfully activated" << connectionPath.path(); return true; } @@ -236,7 +239,7 @@ QVariant NetworkManagerControl::createConnection(const QString &serial, const QS settings["ipv4"] = ipv4Map; if (!m_bus.isConnected()) { - qWarning() << "Could not connect to D-Bus system bus: " << m_bus.lastError(); + qCWarning(networkC) << "Could not connect to D-Bus system bus: " << m_bus.lastError(); return QVariant{}; } @@ -246,8 +249,8 @@ QVariant NetworkManagerControl::createConnection(const QString &serial, const QS m_bus}; if (!connectionSettingsInterface.isValid()) { - qWarning() << "Could not find NetworkManager Settings D-Bus interface:" - << connectionSettingsInterface.lastError(); + qCWarning(networkC) << "Could not find NetworkManager Settings D-Bus interface:" + << connectionSettingsInterface.lastError(); return QVariant{}; } @@ -263,7 +266,7 @@ QVariant NetworkManagerControl::createConnection(const QString &serial, const QS QVariant NetworkManagerControl::findConnectionsByMac(const QString &macAddress) { if (!m_bus.isConnected()) { - qWarning() << "Could not connect to D-Bus system bus: " << m_bus.lastError(); + qCWarning(networkC) << "Could not connect to D-Bus system bus: " << m_bus.lastError(); return QVariant{}; } @@ -273,14 +276,14 @@ QVariant NetworkManagerControl::findConnectionsByMac(const QString &macAddress) m_bus}; if (!settingsInterface.isValid()) { - qWarning() << "Could not find NetworkManager D-Bus interface:" - << settingsInterface.lastError(); + qCWarning(networkC) << "Could not find NetworkManager D-Bus interface:" + << settingsInterface.lastError(); return QVariant{}; } QVariant result = settingsInterface.property("Connections"); if (!result.isValid()) { - qWarning() << "Could not fetch the NetworkManager connections via D-Bus" << settingsInterface.lastError(); + qCWarning(networkC) << "Could not fetch the NetworkManager connections via D-Bus" << settingsInterface.lastError(); return QVariant{}; } @@ -294,9 +297,9 @@ QVariant NetworkManagerControl::findConnectionsByMac(const QString &macAddress) const auto mac = result.toByteArray(); return macAddressToByteArray(macAddress) == mac; }); - qDebug() << "Existing connections for" << macAddress << ":"; + qCDebug(networkC) << "Existing connections for" << macAddress << ":"; for (const auto &connection : matchingConnections) - qDebug() << " " << connection.path(); + qCDebug(networkC) << " " << connection.path(); return QVariant::fromValue(matchingConnections); } @@ -312,7 +315,7 @@ QVariant NetworkManagerControl::findNetworkDeviceByMac(const QString &macAddress networkManagerInterfaceName + ".Device.Wired", m_bus}; if (!wiredDeviceInterface.isValid()) { - qWarning() << "Could not find NetworkManager Device:" << device.path() << wiredDeviceInterface.lastError(); + qCWarning(networkC) << "Could not find NetworkManager Device:" << device.path() << wiredDeviceInterface.lastError(); continue; } QVariant macResult = wiredDeviceInterface.property("HwAddress"); @@ -321,12 +324,12 @@ QVariant NetworkManagerControl::findNetworkDeviceByMac(const QString &macAddress // Non-wired devices result into errors due to no MAC address and need not be warned about. // For some reason the error type in this case can be either UnknownInterface or InvalidArgs. if (error.type() != QDBusError::UnknownInterface && error.type() != QDBusError::InvalidArgs) - qWarning() << "Could not fetch hw address for" << device.path() << error; + qCWarning(networkC) << "Could not fetch hw address for" << device.path() << error; continue; } if (macResult.toString().toUpper() == normalizedMac) { - qDebug() << macAddress << "is" << device.path(); + qCDebug(networkC) << macAddress << "is" << device.path(); return device.path(); } } @@ -336,7 +339,7 @@ QVariant NetworkManagerControl::findNetworkDeviceByMac(const QString &macAddress bool NetworkManagerControl::isActivated(const QString &devicePath) { if (!m_bus.isConnected()) { - qWarning() << "Could not connect to D-Bus system bus: " << m_bus.lastError(); + qCWarning(networkC) << "Could not connect to D-Bus system bus: " << m_bus.lastError(); // TODO: separate error value? return false; } @@ -346,15 +349,15 @@ bool NetworkManagerControl::isActivated(const QString &devicePath) networkManagerInterfaceName + ".Device", m_bus}; if (!deviceInterface.isValid()) { - qWarning() << "Could not find NetworkManager Device:" << devicePath << deviceInterface.lastError(); + qCWarning(networkC) << "Could not find NetworkManager Device:" << devicePath << deviceInterface.lastError(); // TODO: separate error value? return false; } const QVariant result = deviceInterface.property("State"); if (!result.isValid()) { - qWarning() << "Could not check activation status of " << devicePath - << "via D-Bus:" << deviceInterface.lastError(); + qCWarning(networkC) << "Could not check activation status of " << devicePath + << "via D-Bus:" << deviceInterface.lastError(); } const auto state = result.toUInt(); @@ -405,7 +408,7 @@ bool NetworkManagerControl::isActivated(const QString &devicePath) case NM_DEVICE_STATE_FAILED: return false; default: - qCritical() << "Unrecognized NetworkManager device state" << state; + qCCritical(networkC) << "Unrecognized NetworkManager device state" << state; return false; } } @@ -438,7 +441,7 @@ bool NetworkManagerControl::isDeviceUsingLinkLocal(const QString &devicePath) QVariant NetworkManagerControl::listNetworkDevices() { if (!m_bus.isConnected()) { - qWarning() << "Could not connect to D-Bus system bus: " << m_bus.lastError(); + qCWarning(networkC) << "Could not connect to D-Bus system bus: " << m_bus.lastError(); return QVariant{}; } @@ -448,14 +451,15 @@ QVariant NetworkManagerControl::listNetworkDevices() m_bus}; if (!networkManagerInterface.isValid()) { - qWarning() << "Could not find NetworkManager D-Bus interface:" - << networkManagerInterface.lastError(); + qCWarning(networkC) << "Could not find NetworkManager D-Bus interface:" + << networkManagerInterface.lastError(); return QVariant{}; } QVariant result = networkManagerInterface.property("Devices"); if (!result.isValid()) { - qWarning() << "Could not fetch the NetworkManager devices via D-Bus" << networkManagerInterface.lastError(); + qCWarning(networkC) << "Could not fetch the NetworkManager devices via D-Bus" + << networkManagerInterface.lastError(); } return result; } diff --git a/qdb/server/service.cpp b/qdb/server/service.cpp index b381550..dc839da 100644 --- a/qdb/server/service.cpp +++ b/qdb/server/service.cpp @@ -22,8 +22,6 @@ #include "libqdb/stream.h" -#include <QtCore/qdebug.h> - Service::Service() : m_stream{nullptr} { diff --git a/qdb/server/usb-host/libusbcontext.cpp b/qdb/server/usb-host/libusbcontext.cpp index 6976e4e..a21a366 100644 --- a/qdb/server/usb-host/libusbcontext.cpp +++ b/qdb/server/usb-host/libusbcontext.cpp @@ -20,10 +20,13 @@ ******************************************************************************/ #include "usbcommon.h" +#include <QtCore/qloggingcategory.h> #include <QtGlobal> #include <libusb.h> +Q_DECLARE_LOGGING_CATEGORY(usbC); + struct LibUsbContext { LibUsbContext() @@ -31,7 +34,7 @@ struct LibUsbContext { int ret = libusb_init(&context); if (ret) { - qCritical("Could not initialize libusb"); + qCCritical(usbC) << "Could not initialize libusb"; } } diff --git a/qdb/server/usb-host/usbconnection.cpp b/qdb/server/usb-host/usbconnection.cpp index ed0f1e3..ad09ecc 100644 --- a/qdb/server/usb-host/usbconnection.cpp +++ b/qdb/server/usb-host/usbconnection.cpp @@ -27,10 +27,13 @@ #include "usbconnectionreader.h" #include <QtCore/qdebug.h> +#include <QtCore/qloggingcategory.h> #include <QtCore/qthread.h> #include <libusb.h> +Q_LOGGING_CATEGORY(usbC, "qdb.usb"); + UsbConnection::UsbConnection(const UsbDevice &device) : m_device{device.usbDevice}, m_handle{nullptr}, @@ -64,8 +67,7 @@ bool UsbConnection::open(OpenMode mode) libusb_config_descriptor *config; int ret = libusb_get_active_config_descriptor(m_device.pointer(), &config); if (ret) { - qDebug("could not get config descriptor: %s\n", - libusb_error_name(ret)); + qCWarning(usbC) << "Could not get config descriptor:" << libusb_error_name(ret); return false; } ScopeGuard configGuard = [&]() { @@ -74,22 +76,22 @@ bool UsbConnection::open(OpenMode mode) ret = libusb_open(m_device.pointer(), &m_handle); if (ret) { - qDebug("cannot open device: %s\n", libusb_error_name(ret)); + qCDebug(usbC) << "Could not open device:" << libusb_error_name(ret); return false; } if (libusb_kernel_driver_active(m_handle, m_interfaceInfo.number) == 1) { - qDebug() << "Detached kernel driver"; + qCDebug(usbC) << "Detached kernel driver"; m_detachedKernel = true; libusb_detach_kernel_driver(m_handle, m_interfaceInfo.number); } ret = libusb_claim_interface(m_handle, m_interfaceInfo.number); if (ret) { - qDebug("cannot claim interface: %s", libusb_error_name(ret)); + qCDebug(usbC) << "Could not claim interface:" << libusb_error_name(ret); return false; } - qDebug("claimed interface %d", m_interfaceInfo.number); + qCDebug(usbC) << "Claimed interface" << m_interfaceInfo.number; startReader(m_handle, m_interfaceInfo.inAddress); @@ -99,7 +101,7 @@ bool UsbConnection::open(OpenMode mode) qint64 UsbConnection::readData(char *data, qint64 maxSize) { if (m_reads.isEmpty()) { - qDebug() << "UsbConnection read queue empty in readData"; + qCWarning(usbC) << "UsbConnection read queue empty while trying to read"; return -1; } QByteArray read = m_reads.dequeue(); @@ -117,7 +119,7 @@ qint64 UsbConnection::writeData(const char *data, qint64 maxSize) int transferred = 0; int ret = libusb_bulk_transfer(m_handle, m_interfaceInfo.outAddress, (unsigned char*)data, size, &transferred, 0); if (ret) { - qDebug() << "writeData error:" << libusb_error_name(ret); + qCCritical(usbC) << "Could not write message header:" << libusb_error_name(ret); return -1; } Q_ASSERT(transferred == size); // TODO: handle partial transfers of header @@ -127,7 +129,7 @@ qint64 UsbConnection::writeData(const char *data, qint64 maxSize) int rest = maxSize - size; int ret = libusb_bulk_transfer(m_handle, m_interfaceInfo.outAddress, (unsigned char*)data + size, rest, &transferred, 0); if (ret) { - qDebug() << "writeData error:" << libusb_error_name(ret); + qCCritical(usbC) << "Could not write message payload:" << libusb_error_name(ret); return -1; } } diff --git a/qdb/server/usb-host/usbconnectionreader.cpp b/qdb/server/usb-host/usbconnectionreader.cpp index b7f975e..3411a40 100644 --- a/qdb/server/usb-host/usbconnectionreader.cpp +++ b/qdb/server/usb-host/usbconnectionreader.cpp @@ -23,11 +23,14 @@ #include "libqdb/protocol/protocol.h" #include <QtCore/qdebug.h> +#include <QtCore/qloggingcategory.h> #include <QtCore/qthread.h> #include <QtCore/qtimer.h> #include <libusb.h> +Q_DECLARE_LOGGING_CATEGORY(usbC); + // Amount of milliseconds between yielding control to the event loop of the reading thread static const int quitCheckingTimeout = 500; @@ -47,7 +50,7 @@ void UsbConnectionReader::executeRead() buffer.size(), &transferred, quitCheckingTimeout); if (ret) { if (ret != LIBUSB_ERROR_TIMEOUT) { - qWarning() << "Error reading from USB connection:" << libusb_error_name(ret); + qCWarning(usbC) << "Could not read from USB connection:" << libusb_error_name(ret); ++m_errorCount; if (m_errorCount == 5) { emit newRead(QByteArray{}); diff --git a/qdb/server/usb-host/usbdeviceenumerator.cpp b/qdb/server/usb-host/usbdeviceenumerator.cpp index 7095db8..04d97f9 100644 --- a/qdb/server/usb-host/usbdeviceenumerator.cpp +++ b/qdb/server/usb-host/usbdeviceenumerator.cpp @@ -26,9 +26,12 @@ #include "usbdeviceenumerator.h" #include <QtCore/qdebug.h> +#include <QtCore/qloggingcategory.h> #include <libusb.h> +Q_DECLARE_LOGGING_CATEGORY(usbC); + bool isQdbInterface(const libusb_interface &interface) { const libusb_interface_descriptor *descriptor = &interface.altsetting[0]; @@ -40,7 +43,7 @@ std::pair<bool, UsbInterfaceInfo> findQdbInterface(libusb_device *device) libusb_config_descriptor *config; const int ret = libusb_get_active_config_descriptor(device, &config); if (ret) { - qCritical() << "Could not get config descriptor" << libusb_error_name(ret); + qCCritical(usbC) << "Could not get config descriptor:" << libusb_error_name(ret); return std::make_pair(false, UsbInterfaceInfo{}); } ScopeGuard configGuard = [&]() { @@ -79,7 +82,7 @@ QString getSerialNumber(libusb_device *device, libusb_device_handle *handle) libusb_device_descriptor desc; int ret = libusb_get_device_descriptor(device, &desc); if (ret) { - qCritical() << "Could not get device descriptor" << libusb_error_name(ret); + qCCritical(usbC) << "Could not get device descriptor" << libusb_error_name(ret); return serial; } auto serialIndex = desc.iSerialNumber; @@ -89,7 +92,7 @@ QString getSerialNumber(libusb_device *device, libusb_device_handle *handle) unsigned char buffer[bufferSize]; int length = libusb_get_string_descriptor(handle, serialIndex, englishUsLangId, buffer, bufferSize); if (length <= 0) { - qWarning() << "Could not get string descriptor of serial number:" << libusb_error_name(length); + qCWarning(usbC) << "Could not get string descriptor of serial number:" << libusb_error_name(length); return serial; } // length is the length in bytes and UTF-16 characters consist of two bytes @@ -114,7 +117,7 @@ std::pair<bool, UsbDevice> makeUsbDeviceIfQdbDevice(libusb_device *device) libusb_device_handle *handle; int ret = libusb_open(device, &handle); if (ret) { - qDebug() << "Could not open USB device for checking serial number:" << libusb_error_name(ret); + qCWarning(usbC) << "Could not open USB device for checking serial number:" << libusb_error_name(ret); return std::make_pair(false, UsbDevice{}); } ScopeGuard deviceGuard = [=]() { @@ -130,7 +133,7 @@ std::pair<bool, UsbDevice> makeUsbDeviceIfQdbDevice(libusb_device *device) std::vector<UsbDevice> makeUsbDevices() { if (!libUsbContext()) { - qDebug() << "Not initialized libusb in UsbDeviceEnumerator"; + qCCritical(usbC) << "Uninitialized libusb in UsbDeviceEnumerator"; return std::vector<UsbDevice>{}; } @@ -141,7 +144,7 @@ std::vector<UsbDevice> makeUsbDevices() }; if (deviceCount < 0) { - qCritical() << "USB devices could not be listed:" << libusb_error_name(deviceCount); + qCCritical(usbC) << "Could not list USB devices:" << libusb_error_name(deviceCount); return std::vector<UsbDevice>{}; } diff --git a/qdbd/echoexecutor.cpp b/qdbd/echoexecutor.cpp index 325df7c..b2024c2 100644 --- a/qdbd/echoexecutor.cpp +++ b/qdbd/echoexecutor.cpp @@ -23,6 +23,9 @@ #include "libqdb/stream.h" #include <QtCore/qdebug.h> +#include <QtCore/qloggingcategory.h> + +Q_LOGGING_CATEGORY(echoC, "qdb.executors.echo"); EchoExecutor::EchoExecutor(Stream *stream) : m_stream{stream} @@ -33,6 +36,6 @@ EchoExecutor::EchoExecutor(Stream *stream) void EchoExecutor::receive(StreamPacket packet) { - qDebug() << "EchoExecutor received:" << packet.buffer(); + qCDebug(echoC) << "EchoExecutor received:" << packet.buffer(); m_stream->write(packet); } diff --git a/qdbd/handshakeexecutor.cpp b/qdbd/handshakeexecutor.cpp index 45149f1..88960c1 100644 --- a/qdbd/handshakeexecutor.cpp +++ b/qdbd/handshakeexecutor.cpp @@ -25,8 +25,11 @@ #include <QtCore/qdebug.h> #include <QtCore/qfile.h> +#include <QtCore/qloggingcategory.h> #include <QtNetwork/qnetworkinterface.h> +Q_LOGGING_CATEGORY(handshakeC, "qdb.executors.handshake"); + QString rndisFunctionPath() { return Configuration::gadgetConfigFsDir() + "/functions/" + Configuration::rndisFunctionName(); @@ -36,7 +39,7 @@ QString deviceIpAddress() { QFile file{rndisFunctionPath() + "/ifname"}; if (!file.open(QIODevice::ReadOnly)) { - qCritical() << "Could not find network interface name from RNDIS configuration at" << rndisFunctionPath(); + qCCritical(handshakeC) << "Could not find network interface name from RNDIS configuration at" << rndisFunctionPath(); return ""; } @@ -47,7 +50,7 @@ QString deviceIpAddress() for (const auto &entry : addressEntries) { const auto ip = entry.ip(); if (ip.protocol() == QAbstractSocket::IPv4Protocol) { - qDebug() << "Device IP address:" << ip.toString(); + qCDebug(handshakeC) << "Device IP address:" << ip.toString(); return ip.toString(); } } @@ -58,7 +61,7 @@ QString deviceSerial() { QFile file{Configuration::gadgetConfigFsDir() + "/strings/0x409/serialnumber"}; if (!file.open(QIODevice::ReadOnly)) { - qCritical() << "Could not find device serial number from configfs configuration at" << Configuration::gadgetConfigFsDir(); + qCCritical(handshakeC) << "Could not find device serial number from configfs configuration at" << Configuration::gadgetConfigFsDir(); return ""; } return QString{file.readAll()}.trimmed(); @@ -68,7 +71,7 @@ QString hostSideMac() { QFile file{rndisFunctionPath() + "/host_addr"}; if (!file.open(QIODevice::ReadOnly)) { - qCritical() << "Could not find host MAC address from RNDIS configuration at" << rndisFunctionPath(); + qCCritical(handshakeC) << "Could not find host MAC address from RNDIS configuration at" << rndisFunctionPath(); return ""; } return QString{file.readAll()}.trimmed(); @@ -85,10 +88,10 @@ void HandshakeExecutor::receive(StreamPacket packet) { Q_UNUSED(packet); - qDebug() << "Responding to handshake with device information."; StreamPacket response; response << deviceSerial(); response << hostSideMac(); response << deviceIpAddress(); m_stream->write(response); + qCDebug(handshakeC) << "Responded to handshake with device information"; } diff --git a/qdbd/main.cpp b/qdbd/main.cpp index f7b289c..6ebaa42 100644 --- a/qdbd/main.cpp +++ b/qdbd/main.cpp @@ -52,18 +52,18 @@ int main(int argc, char *argv[]) QString filterRules; if (!parser.isSet("debug-transport")) { - filterRules.append("transport=false\n"); + filterRules.append("transport.debug=false\n"); } if (!parser.isSet("debug-connection")) { - filterRules.append("connection=false\n"); + filterRules.append("connection.debug=false\n"); } QLoggingCategory::setFilterRules(filterRules); Server server{new QdbTransport{new UsbGadget{}}}; if (server.initialize()) { - qDebug() << "initialized server"; + qDebug() << "Initialized device server"; } else { - qDebug() << "could not initialize server"; + qCritical() << "Could not initialize device server"; return 1; } diff --git a/qdbd/server.cpp b/qdbd/server.cpp index 2757c70..9e47528 100644 --- a/qdbd/server.cpp +++ b/qdbd/server.cpp @@ -33,7 +33,7 @@ #include <algorithm> -Q_LOGGING_CATEGORY(connectionC, "connection"); +Q_LOGGING_CATEGORY(connectionC, "qdb.connection"); Server::Server(QdbTransport *transport, QObject *parent) : AbstractConnection{transport, parent}, @@ -54,7 +54,7 @@ void Server::handleMessage() switch (m_state) { case ServerState::Disconnected: if (message.command() != QdbMessage::Connect) { - qWarning() << "Server got non-Connect message in Disconnected state. Resetting."; + qCWarning(connectionC) << "Server got non-Connect message in Disconnected state. Resetting."; resetServer(false); break; } @@ -64,7 +64,7 @@ void Server::handleMessage() case ServerState::Connected: switch (message.command()) { case QdbMessage::Connect: - qWarning() << "Server received QdbMessage::Connect while already connected. Resetting."; + qCWarning(connectionC()) << "Server received QdbMessage::Connect while already connected. Resetting."; checkVersion(message); resetServer(true); break; @@ -78,7 +78,7 @@ void Server::handleMessage() closeStream(message.deviceStream()); break; case QdbMessage::Ok: - qWarning() << "Server received QdbMessage::Ok in connected state"; + qCWarning(connectionC) << "Server received QdbMessage::Ok in connected state"; break; case QdbMessage::Invalid: Q_UNREACHABLE(); @@ -88,7 +88,7 @@ void Server::handleMessage() case ServerState::Waiting: switch (message.command()) { case QdbMessage::Connect: - qWarning() << "Server received QdbMessage::Connect while already connected and waiting. Resetting."; + qCWarning(connectionC()) << "Server received QdbMessage::Connect while already connected and waiting. Resetting."; resetServer(true); break; case QdbMessage::Open: @@ -138,7 +138,7 @@ void Server::processQueue() "Tried to send invalid message"); if (!m_transport->send(message)) { - qCritical() << "Server could not send" << message; + qCCritical(connectionC) << "Server could not send" << message; m_state = ServerState::Disconnected; return; } @@ -157,9 +157,9 @@ void Server::processQueue() // onto the next message in the queue, otherwise it would only be sent // after host sends us something. case QdbMessage::Connect: - [[fallthrough]] + //[[fallthrough]] case QdbMessage::Close: - [[fallthrough]] + //[[fallthrough]] case QdbMessage::Ok: if (!m_outgoingMessages.isEmpty()) { processQueue(); @@ -196,7 +196,7 @@ void Server::resetServer(bool hostConnected) void Server::handleWrite(const QdbMessage &message) { if (m_streams.find(message.deviceStream()) == m_streams.end()) { - qWarning() << "Server received message to non-existing stream" << message.deviceStream(); + qCWarning(connectionC) << "Server received message to non-existing stream" << message.deviceStream(); enqueueMessage(QdbMessage{QdbMessage::Close, message.hostStream(), message.deviceStream()}); return; } @@ -207,7 +207,7 @@ void Server::handleWrite(const QdbMessage &message) void Server::closeStream(StreamId id) { if (m_streams.find(id) == m_streams.end()) { - qWarning() << "Server received Close to a non-existing stream"; + qCWarning(connectionC) << "Server received Close to a non-existing stream" << id; return; } @@ -234,8 +234,8 @@ void Server::checkVersion(const QdbMessage &message) dataStream >> protocolVersion; if (protocolVersion != qdbProtocolVersion) { - qWarning() << "Protocol version" << protocolVersion << "requested, but only version" - << qdbProtocolVersion << "is known"; + qCCritical(connectionC()) << "Protocol version" << protocolVersion << "requested, but only version" + << qdbProtocolVersion << "is known"; } } diff --git a/qdbd/usb-gadget/usbgadget.cpp b/qdbd/usb-gadget/usbgadget.cpp index 0b1b530..af5eca2 100644 --- a/qdbd/usb-gadget/usbgadget.cpp +++ b/qdbd/usb-gadget/usbgadget.cpp @@ -28,10 +28,13 @@ #include <QtCore/qdatastream.h> #include <QtCore/qdebug.h> +#include <QtCore/qloggingcategory.h> #include <QtCore/qthread.h> #include <linux/usb/functionfs.h> +Q_LOGGING_CATEGORY(usbC, "qdb.usb"); + usb_interface_descriptor makeInterfaceDescriptor() { usb_interface_descriptor interface; @@ -144,26 +147,26 @@ bool UsbGadget::open(QIODevice::OpenMode mode) qint64 bytes = m_controlEndpoint.write(reinterpret_cast<const char*>(&descriptors), sizeof(descriptors)); if (bytes == -1) { - qDebug() << "Failed to write USB descriptors:" << m_controlEndpoint.errorString(); + qCCritical(usbC) << "Failed to write USB descriptors:" << m_controlEndpoint.errorString(); return false; } bytes = m_controlEndpoint.write(reinterpret_cast<const char*>(&strings), sizeof(strings)); if (bytes == -1) { - qDebug() << "Failed to write USB strings:" << m_controlEndpoint.errorString(); + qCCritical(usbC) << "Failed to write USB strings:" << m_controlEndpoint.errorString(); return false; } if (!m_outEndpoint.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) { - qDebug() << "Failed to open endpoint from host to gadget"; + qCCritical(usbC) << "Failed to open endpoint from host to gadget"; return false; } if (!m_inEndpoint.open(QIODevice::WriteOnly | QIODevice::Unbuffered)) { - qDebug() << "Failed to open endpoint from gadget to host"; + qCCritical(usbC) << "Failed to open endpoint from gadget to host"; return false; } - qDebug() << "Initialized function fs"; + qCDebug(usbC) << "Initialized function fs"; startReadThread(); startWriteThread(); @@ -174,7 +177,7 @@ bool UsbGadget::open(QIODevice::OpenMode mode) qint64 UsbGadget::readData(char *data, qint64 maxSize) { if (m_reads.isEmpty()) { - qDebug() << "UsbGadget read queue empty in readData"; + qCWarning(usbC) << "UsbGadget read queue empty while trying to read"; return -1; } QByteArray read = m_reads.dequeue(); @@ -191,7 +194,7 @@ qint64 UsbGadget::writeData(const char *data, qint64 size) return size; } - qDebug() << "Tried to send to host through closed endpoint"; + qCCritical(usbC) << "Tried to send to host through closed endpoint"; return -1; } @@ -229,11 +232,11 @@ void UsbGadget::startWriteThread() bool UsbGadget::openControlEndpoint() { if (!QFile::exists(m_controlEndpoint.fileName())) { - qCritical() << "USB ffs control endpoint" << m_controlEndpoint.fileName() << "does not exist"; + qCCritical(usbC) << "USB ffs control endpoint" << m_controlEndpoint.fileName() << "does not exist"; return false; } if (!m_controlEndpoint.open(QIODevice::ReadWrite | QIODevice::Unbuffered)) { - qCritical() << "Failed to open control endpoint" << m_controlEndpoint.fileName(); + qCCritical(usbC) << "Failed to open control endpoint" << m_controlEndpoint.fileName(); return false; } return true; diff --git a/qdbd/usb-gadget/usbgadgetreader.cpp b/qdbd/usb-gadget/usbgadgetreader.cpp index dfb5600..e8e1500 100644 --- a/qdbd/usb-gadget/usbgadgetreader.cpp +++ b/qdbd/usb-gadget/usbgadgetreader.cpp @@ -25,8 +25,11 @@ #include <QtCore/qdebug.h> #include <QtCore/qfile.h> +#include <QtCore/qloggingcategory.h> #include <QtCore/qtimer.h> +Q_DECLARE_LOGGING_CATEGORY(usbC); + UsbGadgetReader::UsbGadgetReader(QFile *readEndpoint) : m_readEndpoint{readEndpoint} { @@ -36,7 +39,7 @@ UsbGadgetReader::UsbGadgetReader(QFile *readEndpoint) void UsbGadgetReader::executeRead() { if (!m_readEndpoint->isOpen()) { - qWarning() << "tried to receive from host through closed endpoint"; + qCCritical(usbC) << "Tried to receive from host through closed endpoint"; return; } @@ -45,10 +48,10 @@ void UsbGadgetReader::executeRead() QByteArray headerBuffer{qdbHeaderSize, '\0'}; int count = m_readEndpoint->read(headerBuffer.data(), headerBuffer.size()); if (count == -1) { - qWarning() << "error in reading message header from endpoint"; + qCWarning(usbC) << "Could not read message header from endpoint"; return; } else if (count < headerBuffer.size()) { - qWarning() << "error: read" << count << "out of" << headerBuffer.size() << "byte header from endpoint"; + qCWarning(usbC) << "Could only read" << count << "out of" << headerBuffer.size() << "byte header from endpoint"; return; } @@ -62,10 +65,10 @@ void UsbGadgetReader::executeRead() QByteArray dataBuffer{dataSize, '\0'}; count = m_readEndpoint->read(dataBuffer.data(), dataBuffer.size()); if (count == -1) { - qWarning() << "error in reading data from endpoint"; + qCWarning(usbC) << "Could not read message payload from endpoint"; return; } else if (count < dataBuffer.size()) { - qWarning() << "error: read" << count << "out of" << headerBuffer.size() << "byte data from endpoint"; + qCWarning(usbC) << "Could only read" << count << "out of" << headerBuffer.size() << "byte payload from endpoint"; return; } diff --git a/qdbd/usb-gadget/usbgadgetwriter.cpp b/qdbd/usb-gadget/usbgadgetwriter.cpp index 3e48349..a764d42 100644 --- a/qdbd/usb-gadget/usbgadgetwriter.cpp +++ b/qdbd/usb-gadget/usbgadgetwriter.cpp @@ -25,8 +25,11 @@ #include <QtCore/qdebug.h> #include <QtCore/qfile.h> +#include <QtCore/qloggingcategory.h> #include <QtCore/qtimer.h> +Q_DECLARE_LOGGING_CATEGORY(usbC); + UsbGadgetWriter::UsbGadgetWriter(QFile *writeEndpoint) : m_writeEndpoint{writeEndpoint} { @@ -36,14 +39,14 @@ UsbGadgetWriter::UsbGadgetWriter(QFile *writeEndpoint) void UsbGadgetWriter::write(QByteArray data) { if (!m_writeEndpoint->isOpen()) { - qWarning() << "UsbGadgetWriter: Tried to write to a closed endpoint"; + qCCritical(usbC) << "Tried to write to a closed endpoint"; emit writeDone(false); return; } auto written = m_writeEndpoint->write(data); if (written != data.size()) { - qWarning() << "UsbGadgetWriter: Write to endpoint failed"; + qCCritical(usbC) << "Could not write to endpoint"; emit writeDone(false); } emit writeDone(true); |