From 0bd3237c0818db5ed323955c03e5c2925e2c3557 Mon Sep 17 00:00:00 2001 From: David Ward Date: Mon, 16 Sep 2024 09:48:52 +1200 Subject: [PATCH 1/3] Renamed g_jsonLoggingOn to g_LogJSONRequests and made it global. The new name is more meaningful and making the flag global means we can modify it from other places in the code. --- include/mega/json.h | 3 +++ src/json.cpp | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/mega/json.h b/include/mega/json.h index e4dac54a98..9fdd95002d 100644 --- a/include/mega/json.h +++ b/include/mega/json.h @@ -283,6 +283,9 @@ class MEGA_API JSONSplitter }; // JSONSplitter +// If true, logs the contents of all JSON requests and responses in full. +extern std::atomic gLogJSONRequests; + } // namespace #endif diff --git a/src/json.cpp b/src/json.cpp index 3ad5ec6b6e..a3eb08e2d6 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -29,8 +29,9 @@ namespace mega { -bool g_jsonLoggingOn = false; -#define JSON_verbose if (g_jsonLoggingOn) LOG_verbose +std::atomic gLogJSONRequests{false}; + +#define JSON_verbose if (gLogJSONRequests) LOG_verbose // store array or object in string s // reposition after object From 844f03eba0197198d82990780ef8c96513833031 Mon Sep 17 00:00:00 2001 From: David Ward Date: Mon, 16 Sep 2024 09:57:52 +1200 Subject: [PATCH 2/3] Don't truncate CS / SC content if g_LogJSON is true. --- src/posix/net.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/posix/net.cpp b/src/posix/net.cpp index 9fb8e2fa80..89d92669d7 100644 --- a/src/posix/net.cpp +++ b/src/posix/net.cpp @@ -1467,7 +1467,7 @@ void CurlHttpIO::send_request(CurlHttpContext* httpctx) } else { - if (req->out->size() < size_t(SimpleLogger::getMaxPayloadLogSize())) + if (gLogJSONRequests || req->out->size() < size_t(SimpleLogger::getMaxPayloadLogSize())) { LOG_debug << httpctx->req->logname << "Sending " << req->out->size() << ": " << DirectMessage(req->out->c_str(), req->out->size()) << " (at ds: " << Waiter::ds << ")"; @@ -2340,7 +2340,8 @@ bool CurlHttpIO::multidoio(CURLM *curlmhandle) } else { - if (req->in.size() < size_t(SimpleLogger::getMaxPayloadLogSize())) + if (gLogJSONRequests || + req->in.size() < size_t(SimpleLogger::getMaxPayloadLogSize())) { LOG_debug << req->logname << "Received " << req->in.size() << ": " << DirectMessage(req->in.c_str(), req->in.size()) << " (at ds: " << Waiter::ds << ")"; From def9d911aa7f8adf2f643cfad1568d5a0418a680 Mon Sep 17 00:00:00 2001 From: David Ward Date: Mon, 16 Sep 2024 10:04:33 +1200 Subject: [PATCH 3/3] Added MegaApi::setLogJSONContent(...). Using this function, the application can control whether or not JSON content for requests and responses are logged in full. --- include/megaapi.h | 14 ++++++++++++++ include/megaapi_impl.h | 1 + src/megaapi.cpp | 5 +++++ src/megaapi_impl.cpp | 5 +++++ 4 files changed, 25 insertions(+) diff --git a/include/megaapi.h b/include/megaapi.h index 966b6a4a6c..2d703008f7 100644 --- a/include/megaapi.h +++ b/include/megaapi.h @@ -12413,6 +12413,20 @@ class MegaApi */ static void setLogToConsole(bool enable); + /** + * @brief + * Enable full JSON logging. + * + * This function allows an application to control whether JSON + * requests and responses are logged in full. When enable is true, + * JSON content will always be logged and will not be truncated. + * When false, JSON may be logged in some situations but only if + * the content is less than the logger's maximum payload size. + * + * @see MegaApi::setMaxPayloadLogSize + */ + static void setLogJSONContent(bool enable); + /** * @brief Add a MegaLogger implementation to receive SDK logs * diff --git a/include/megaapi_impl.h b/include/megaapi_impl.h index 18db962367..5453ccb7c5 100644 --- a/include/megaapi_impl.h +++ b/include/megaapi_impl.h @@ -3167,6 +3167,7 @@ class MegaApiImpl : public MegaApp static void addLoggerClass(MegaLogger *megaLogger, bool singleExclusiveLogger); static void removeLoggerClass(MegaLogger *megaLogger, bool singleExclusiveLogger); static void setLogToConsole(bool enable); + static void setLogJSONContent(bool enable); static void log(int logLevel, const char* message, const char *filename = NULL, int line = -1); void setLoggingName(const char* loggingName); diff --git a/src/megaapi.cpp b/src/megaapi.cpp index 37c01289c8..c87681dc71 100644 --- a/src/megaapi.cpp +++ b/src/megaapi.cpp @@ -2148,6 +2148,11 @@ void MegaApi::setLogToConsole(bool enable) MegaApiImpl::setLogToConsole(enable); } +void MegaApi::setLogJSONContent(bool enable) +{ + MegaApiImpl::setLogJSONContent(enable); +} + void MegaApi::addLoggerObject(MegaLogger *megaLogger, bool singleExclusiveLogger) { MegaApiImpl::addLoggerClass(megaLogger, singleExclusiveLogger); diff --git a/src/megaapi_impl.cpp b/src/megaapi_impl.cpp index 9af3d77953..c7ef79141a 100644 --- a/src/megaapi_impl.cpp +++ b/src/megaapi_impl.cpp @@ -6713,6 +6713,11 @@ void MegaApiImpl::setLogToConsole(bool enable) g_externalLogger.setLogToConsole(enable); } +void MegaApiImpl::setLogJSONContent(bool enable) +{ + gLogJSONRequests = enable; +} + void MegaApiImpl::log(int logLevel, const char *message, const char *filename, int line) { SimpleLogger::postLog(LogLevel(logLevel), message, filename, line);