Skip to content

Commit

Permalink
Merge branch 'task/SDK-3980-log-communication-between-client-and-api'…
Browse files Browse the repository at this point in the history
… into 'develop'

SDK-3980. Allow log all communication between client and API when it's desired

Closes SDK-3980

See merge request sdk/sdk!5822
  • Loading branch information
dwmega committed Sep 17, 2024
2 parents c9e91b9 + def9d91 commit 7d706c6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
3 changes: 3 additions & 0 deletions include/mega/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> gLogJSONRequests;

} // namespace

#endif
14 changes: 14 additions & 0 deletions include/megaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions include/megaapi_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@

namespace mega {

bool g_jsonLoggingOn = false;
#define JSON_verbose if (g_jsonLoggingOn) LOG_verbose
std::atomic<bool> gLogJSONRequests{false};

#define JSON_verbose if (gLogJSONRequests) LOG_verbose

// store array or object in string s
// reposition after object
Expand Down
5 changes: 5 additions & 0 deletions src/megaapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/megaapi_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/posix/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << ")";
Expand Down Expand Up @@ -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 << ")";
Expand Down

0 comments on commit 7d706c6

Please sign in to comment.