From bda60c5a18a885cfc3543cc596a0b4756e3936b4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 8 Sep 2020 17:20:46 -0500 Subject: [PATCH] feat: add 'TR_RPC_VERBOSE' env variable in Qt app. (#1435) When set, this will log the RPC messages sent between the GUI and the backend. This replaces the old `#ifdef DEBUG_HTTP` as something that can be enabled without needing to recompile. --- qt/RpcClient.cc | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/qt/RpcClient.cc b/qt/RpcClient.cc index 7bf0b09c4..d50af5bde 100644 --- a/qt/RpcClient.cc +++ b/qt/RpcClient.cc @@ -24,8 +24,6 @@ #include "VariantHelpers.h" -// #define DEBUG_HTTP - using ::trqt::variant_helpers::dictAdd; using ::trqt::variant_helpers::dictFind; using ::trqt::variant_helpers::variantInit; @@ -36,6 +34,8 @@ namespace char const constexpr* const RequestDataPropertyKey { "requestData" }; char const constexpr* const RequestFutureinterfacePropertyKey { "requestReplyFutureInterface" }; +bool const Verbose = tr_env_key_exists("TR_RPC_VERBOSE"); + void destroyVariant(tr_variant* json) { tr_variantFree(json); @@ -155,16 +155,17 @@ void RpcClient::sendNetworkRequest(TrVariantPtr json, QFutureInterfacerawHeaderList()) + { + std::cerr << b.constData() << ": " << request_->rawHeader(b).constData() << std::endl; + } + + std::cerr << "Body:\n" << json_data.constData() << std::endl; + } } void RpcClient::sendLocalRequest(TrVariantPtr json, QFutureInterface const& promise, int64_t tag) @@ -233,16 +234,17 @@ void RpcClient::networkRequestFinished(QNetworkReply* reply) auto promise = reply->property(RequestFutureinterfacePropertyKey). value>(); -#ifdef DEBUG_HTTP - std::cerr << "http response header: " << std::endl; - - for (QByteArray const& b : reply->rawHeaderList()) + if (Verbose) { - std::cerr << b.constData() << ": " << reply->rawHeader(b).constData() << std::endl; - } + std::cerr << "http response header: " << std::endl; - std::cerr << "json:\n" << reply->peek(reply->bytesAvailable()).constData() << std::endl; -#endif + for (QByteArray const& b : reply->rawHeaderList()) + { + std::cerr << b.constData() << ": " << reply->rawHeader(b).constData() << std::endl; + } + + std::cerr << "json:\n" << reply->peek(reply->bytesAvailable()).constData() << std::endl; + } if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409 && reply->hasRawHeader(TR_RPC_SESSION_ID_HEADER))