diff --git a/CHANGELOG.md b/CHANGELOG.md index e57bba73a..8f29536b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Bugfix: Fixed comma appended to username completion when not at the beginning of the message. (#3060) - Dev: Ubuntu packages are now available (#2936) - Dev: Disabled update checker on Flatpak. (#3051) +- Dev: Add logging for HTTP requests (#2991) ## 2.3.3 diff --git a/src/common/NetworkCommon.hpp b/src/common/NetworkCommon.hpp index 5f90a95a0..cb37cf43d 100644 --- a/src/common/NetworkCommon.hpp +++ b/src/common/NetworkCommon.hpp @@ -24,6 +24,13 @@ enum class NetworkRequestType { Delete, Patch, }; +const static std::vector networkRequestTypes{ + "GET", // + "POST", // + "PUT", // + "DELETE", // + "PATCH", // +}; // parseHeaderList takes a list of headers in string form, // where each header pair is separated by semicolons (;) and the header name and value is divided by a colon (:) diff --git a/src/common/NetworkPrivate.cpp b/src/common/NetworkPrivate.cpp index b141cd853..c49fcff97 100644 --- a/src/common/NetworkPrivate.cpp +++ b/src/common/NetworkPrivate.cpp @@ -145,6 +145,11 @@ void loadUncached(const std::shared_ptr &data) data->timer_, &QTimer::timeout, worker, [reply, data]() { qCDebug(chatterinoCommon) << "Aborted!"; reply->abort(); + qCDebug(chatterinoHTTP) + << QString("%1 [timed out] %2") + .arg(networkRequestTypes.at( + int(data->requestType_)), + data->request_.url().toString()); if (data->onError_) { @@ -181,6 +186,11 @@ void loadUncached(const std::shared_ptr &data) QNetworkReply::NetworkError::OperationCanceledError) { // Operation cancelled, most likely timed out + qCDebug(chatterinoHTTP) + << QString("%1 [cancelled] %2") + .arg(networkRequestTypes.at( + int(data->requestType_)), + data->request_.url().toString()); return; } @@ -188,6 +198,25 @@ void loadUncached(const std::shared_ptr &data) { auto status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute); + if (data->requestType_ == NetworkRequestType::Get) + { + qCDebug(chatterinoHTTP) + << QString("%1 %2 %3") + .arg(networkRequestTypes.at( + int(data->requestType_)), + QString::number(status.toInt()), + data->request_.url().toString()); + } + else + { + qCDebug(chatterinoHTTP) + << QString("%1 %2 %3 %4") + .arg(networkRequestTypes.at( + int(data->requestType_)), + QString::number(status.toInt()), + data->request_.url().toString(), + QString(data->payload_)); + } // TODO: Should this always be run on the GUI thread? postToThread([data, code = status.toInt()] { data->onError_(NetworkResult({}, code)); @@ -227,6 +256,23 @@ void loadUncached(const std::shared_ptr &data) reply->deleteLater(); + if (data->requestType_ == NetworkRequestType::Get) + { + qCDebug(chatterinoHTTP) + << QString("%1 %2 %3") + .arg(networkRequestTypes.at(int(data->requestType_)), + QString::number(status.toInt()), + data->request_.url().toString()); + } + else + { + qCDebug(chatterinoHTTP) + << QString("%1 %3 %2 %4") + .arg(networkRequestTypes.at(int(data->requestType_)), + data->request_.url().toString(), + QString::number(status.toInt()), + QString(data->payload_)); + } if (data->finally_) { if (data->executeConcurrently_) @@ -286,6 +332,10 @@ void loadCached(const std::shared_ptr &data) QByteArray bytes = cachedFile.readAll(); NetworkResult result(bytes, 200); + qCDebug(chatterinoHTTP) + << QString("%1 [CACHED] 200 %2") + .arg(networkRequestTypes.at(int(data->requestType_)), + data->request_.url().toString()); if (data->onSuccess_) { if (data->executeConcurrently_ || isGuiThread()) diff --git a/src/common/QLogging.cpp b/src/common/QLogging.cpp index 742cbe243..a54b3296c 100644 --- a/src/common/QLogging.cpp +++ b/src/common/QLogging.cpp @@ -15,6 +15,7 @@ Q_LOGGING_CATEGORY(chatterinoCommon, "chatterino.common", logThreshold); Q_LOGGING_CATEGORY(chatterinoEmoji, "chatterino.emoji", logThreshold); Q_LOGGING_CATEGORY(chatterinoFfzemotes, "chatterino.ffzemotes", logThreshold); Q_LOGGING_CATEGORY(chatterinoHelper, "chatterino.helper", logThreshold); +Q_LOGGING_CATEGORY(chatterinoHTTP, "chatterino.http", logThreshold); Q_LOGGING_CATEGORY(chatterinoImage, "chatterino.image", logThreshold); Q_LOGGING_CATEGORY(chatterinoIrc, "chatterino.irc", logThreshold); Q_LOGGING_CATEGORY(chatterinoIvr, "chatterino.ivr", logThreshold); diff --git a/src/common/QLogging.hpp b/src/common/QLogging.hpp index b157d79a6..e588ad48a 100644 --- a/src/common/QLogging.hpp +++ b/src/common/QLogging.hpp @@ -11,6 +11,7 @@ Q_DECLARE_LOGGING_CATEGORY(chatterinoCommon); Q_DECLARE_LOGGING_CATEGORY(chatterinoEmoji); Q_DECLARE_LOGGING_CATEGORY(chatterinoFfzemotes); Q_DECLARE_LOGGING_CATEGORY(chatterinoHelper); +Q_DECLARE_LOGGING_CATEGORY(chatterinoHTTP); Q_DECLARE_LOGGING_CATEGORY(chatterinoImage); Q_DECLARE_LOGGING_CATEGORY(chatterinoIrc); Q_DECLARE_LOGGING_CATEGORY(chatterinoIvr);