Skip to content

Commit

Permalink
Merge branch 'task/cht-1332-reduce-logs' into 'develop'
Browse files Browse the repository at this point in the history
CHT-1332. Reduce logs-per-chat logging upon initialization

See merge request megachat/MEGAchat!1984
jgandres committed Sep 4, 2024
2 parents 6582b0e + 63543b0 commit d67f4ec
Showing 20 changed files with 676 additions and 386 deletions.
4 changes: 4 additions & 0 deletions bindings/Objective-C/MEGAChatSdk.mm
Original file line number Diff line number Diff line change
@@ -2072,6 +2072,10 @@ + (void)setLogLevel:(MEGAChatLogLevel)level {
MegaChatApi::setLogLevel((int)level);
}

+ (void)setInternalMaxLogLevel:(MEGAChatLogLevel)level {
MegaChatApi::setInternalMaxLogLevel((int)level);
}

+ (void)setLogToConsole:(BOOL)enable {
MegaChatApi::setLogToConsole(enable);
}
1 change: 1 addition & 0 deletions bindings/Objective-C/include/MEGAChatSdk.h
Original file line number Diff line number Diff line change
@@ -541,6 +541,7 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - Debug log messages

+ (void)setLogLevel:(MEGAChatLogLevel)level;
+ (void)setInternalMaxLogLevel:(MEGAChatLogLevel)level;
+ (void)setLogToConsole:(BOOL)enable;
+ (void)setLogObject:(nullable id<MEGAChatLoggerDelegate>)delegate;
+ (void)setLogWithColors:(BOOL)userColors;
70 changes: 57 additions & 13 deletions bindings/java/nz/mega/sdk/MegaChatApiJava.java
Original file line number Diff line number Diff line change
@@ -25,7 +25,6 @@ public class MegaChatApiJava {
public final static int LOG_LEVEL_WARNING = MegaChatApi.LOG_LEVEL_WARNING;
// Mainly useful to represent current progress of application.
public final static int LOG_LEVEL_INFO = MegaChatApi.LOG_LEVEL_INFO;
public final static int LOG_LEVEL_VERBOSE = MegaChatApi.LOG_LEVEL_VERBOSE;
// Informational logs, that aMegaChatPresenceConfigre useful for developers. Only applicable if DEBUG is defined.
public final static int LOG_LEVEL_DEBUG = MegaChatApi.LOG_LEVEL_DEBUG;
public final static int LOG_LEVEL_MAX = MegaChatApi.LOG_LEVEL_MAX;
@@ -4686,32 +4685,77 @@ public int loadAttachments(long chatid, int count){
}

/**
* Set the active log level.
* Set the active log level
* <p>
* This function sets the log level of the logging system. If you set a log listener using
* MegaApiJava.setLoggerObject(), you will receive logs with the same or a lower level than
* MegaApi::setLoggerObject, you will receive logs with the same or a lower level than
* the one passed to this function.
*
* @param logLevel
* Active log level. These are the valid values for this parameter: <br>
* Valid values are:
* @param logLevel Active log level
* <p>
* Valid values are:
* - MegaChatApi::LOG_LEVEL_ERROR = 1
* - MegaChatApi::LOG_LEVEL_WARNING = 2
* - MegaChatApi::LOG_LEVEL_INFO = 3
* - MegaChatApi::LOG_LEVEL_VERBOSE = 4
* - MegaChatApi::LOG_LEVEL_DEBUG = 5
* - MegaChatApi::LOG_LEVEL_DEBUG = 5
* - MegaChatApi::LOG_LEVEL_MAX = 6
* - MegaApiJava.LOG_LEVEL_FATAL = 0. <br>
* - MegaApiJava.LOG_LEVEL_ERROR = 1. <br>
* - MegaApiJava.LOG_LEVEL_WARNING = 2. <br>
* - MegaApiJava.LOG_LEVEL_INFO = 3. <br>
* - MegaApiJava.LOG_LEVEL_DEBUG = 4. <br>
* - MegaApiJava.LOG_LEVEL_MAX = 5.
* - MegaChatApi::LOG_LEVEL_DEBUG = 4
* - MegaChatApi::LOG_LEVEL_MAX = 5
*/
public static void setLogLevel(int logLevel) {
MegaChatApi.setLogLevel(logLevel);
}

/**
* Get the MAX value of log level that is allowed for MEGAchat internal logs (higher
* values are ignored)
* <p>
* Internal MEGAChat logs will be printed, if log level of the line is "lower or equal" than
* value returned by this function
*
* @return ERROR_ACCESS if MegaChatApi::setLogLevel nor MegaChatApi::setLoggerClass have been
* called previously, otherwise any of the values above.
* @note: line above is valid, always there's no other default value configured at
* KR_LOGGER_CONFIG_START
* <p>
* Valid values are:
* - MegaChatApi::LOG_LEVEL_ERROR = 1
* - MegaChatApi::LOG_LEVEL_WARNING = 2
* - MegaChatApi::LOG_LEVEL_INFO = 3
* - MegaChatApi::LOG_LEVEL_DEBUG = 4
* - MegaChatApi::LOG_LEVEL_MAX = 5
*/
public static int getInternalMaxLogLevel() {
return MegaChatApi.getInternalMaxLogLevel();
}

/**
* Set new MAX value of log level that is allowed for MEGAchat internal logs (higher
* values are ignored)
* <p>
* Internal MEGAChat logs will be printed, if log level of the line is "lower or equal" than
* value returned by this function
*
* @param logLevel new MAX value of log level that is allowed
* Valid values are:
* - MegaChatApi::LOG_LEVEL_ERROR = 1
* - MegaChatApi::LOG_LEVEL_WARNING = 2
* - MegaChatApi::LOG_LEVEL_INFO = 3
* - MegaChatApi::LOG_LEVEL_DEBUG = 4
* - MegaChatApi::LOG_LEVEL_MAX = 5
* @return False if MegaChatApi::setLogLevel nor MegaChatApi::setLoggerClass have been
* called previously, or provided logLevel is invalid, otherwise True.
* Note: line above is valid, always there's no other default value configured at
* KR_LOGGER_CONFIG_START
* <p>
* You need to call MegaChatApiImpl::setLogLevel or MegaChatApiImpl::setLoggerClass before
* calling this method
*/
public static boolean setInternalMaxLogLevel(int logLevel) {
return MegaChatApi.setInternalMaxLogLevel(logLevel);
}

/**
* Set a MegaLogger implementation to receive SDK logs.
* <p>
5 changes: 2 additions & 3 deletions bindings/java/nz/mega/sdk/MegaChatLoggerInterface.java
Original file line number Diff line number Diff line change
@@ -36,9 +36,8 @@ public interface MegaChatLoggerInterface {
* - MegaChatApi::LOG_LEVEL_ERROR = 1
* - MegaChatApi::LOG_LEVEL_WARNING = 2
* - MegaChatApi::LOG_LEVEL_INFO = 3
* - MegaChatApi::LOG_LEVEL_VERBOSE = 4
* - MegaChatApi::LOG_LEVEL_DEBUG = 5
* - MegaChatApi::LOG_LEVEL_MAX = 6
* - MegaChatApi::LOG_LEVEL_DEBUG = 4
* - MegaChatApi::LOG_LEVEL_MAX = 5
*
* For logs generated inside the SDK, this will contain the source file and the line of code.
* The SDK retains the ownership of this string, it won't be valid after this funtion returns.
33 changes: 22 additions & 11 deletions examples/qtmegachatapi/megaLoggerApplication.cpp
Original file line number Diff line number Diff line change
@@ -28,22 +28,33 @@ void MegaLoggerApplication::postLog(const char *message)
cout << message << endl;
}

void MegaLoggerApplication::log(int loglevel, const char *message)
void MegaLoggerApplication::log(int loglevel, const char* message)
{
string levelStr;
switch (loglevel)
{
case MegaChatApi::LOG_LEVEL_ERROR: levelStr = "err"; break;
case MegaChatApi::LOG_LEVEL_WARNING: levelStr = "warn"; break;
case MegaChatApi::LOG_LEVEL_INFO: levelStr = "info"; break;
case MegaChatApi::LOG_LEVEL_VERBOSE: levelStr = "verb"; break;
case MegaChatApi::LOG_LEVEL_DEBUG: levelStr = "debug"; break;
case MegaChatApi::LOG_LEVEL_MAX: levelStr = "debug-verbose"; break;
default: levelStr = ""; break;
case MegaChatApi::LOG_LEVEL_ERROR:
levelStr = "err";
break;
case MegaChatApi::LOG_LEVEL_WARNING:
levelStr = "warn";
break;
case MegaChatApi::LOG_LEVEL_INFO:
levelStr = "info";
break;
case MegaChatApi::LOG_LEVEL_DEBUG:
levelStr = "debug";
break;
case MegaChatApi::LOG_LEVEL_MAX:
levelStr = "verbose";
break;
default:
levelStr = "";
break;
}
testlog << message;
if(mLogConsole)
cout << message << endl;
testlog << message;
if (mLogConsole)
cout << message << endl;
}

bool MegaLoggerApplication::getLogConsole() const
57 changes: 35 additions & 22 deletions src/base/logger.cpp
Original file line number Diff line number Diff line change
@@ -31,18 +31,20 @@
inline struct tm *gmtime_r(const time_t *timep, struct tm *result)
{ return gmtime(timep); }
#endif
extern "C"
{
//this must be in sync with the enums in logger.h
typedef const char* KarereLogLevelName[2];
KRLOGGER_DLLEXPORT KarereLogLevelName krLogLevelNames[krLogLevelLast+1] =
{
{NULL, "off"}, {"ERR", "error"}, {"WRN", "warn"}, {"nfo", "info"},
{"vrb", "verbose"}, {"dbg", "debug"},{"dbg", "debugv"}
};


}
krLogLevel karere::Logger::mKarereMaxLogLevel = krLogLevelMax;
extern "C"
{
// this must be in sync with the enums in logger.h
typedef const char* KarereLogLevelName[2];
KRLOGGER_DLLEXPORT KarereLogLevelName krLogLevelNames[krLogLevelMax + 1] = {
{NULL, "off" },
{"ERR", "error" },
{"WRN", "warn" },
{"nfo", "info" },
{"dbg", "debug" },
{"vrb", "verbose"}
};
}
namespace karere
{
/** Copies maximum maxCount chars from src to dest.
@@ -53,6 +55,16 @@ namespace karere
*/
static size_t myStrncpy(char* dest, const char* src, size_t maxCount);

void Logger::setKarereMaxLogLevel(const unsigned int v)
{
const auto newVal = static_cast<krLogLevel>(v);
if (newVal == mKarereMaxLogLevel || newVal < krLogLevelError || newVal > krLogLevelMax)
{
return;
}
mKarereMaxLogLevel = newVal;
}

void Logger::logToConsole(bool enable)
{
LockGuard lock(mMutex);
@@ -407,18 +419,19 @@ KRLOGGER_DLLEXPORT Logger gLogger;

extern "C"
{
KRLOGGER_DLLEXPORT KarereLogChannel* krLoggerChannels = karere::gLogger.logChannels;
KRLOGGER_DLLEXPORT krLogLevel krLogLevelStrToNum(const char* strLevel)
{
for (krLogLevel n = 0; n<=krLogLevelLast; n++)
KRLOGGER_DLLEXPORT KarereLogChannel* krLoggerChannels = karere::gLogger.logChannels;

KRLOGGER_DLLEXPORT krLogLevel krLogLevelStrToNum(const char* strLevel)
{
auto& name = krLogLevelNames[n];
if ((strcasecmp(strLevel, name[1]) == 0)
|| (name[0] && (strcasecmp(strLevel, name[0]) == 0)))
return n;
for (krLogLevel n = 0; n <= krLogLevelMax; ++n)
{
auto& name = krLogLevelNames[n];
if ((strcasecmp(strLevel, name[1]) == 0) ||
(name[0] && (strcasecmp(strLevel, name[0]) == 0)))
return n;
}
return (krLogLevel)-1;
}
return (krLogLevel)-1;
}

KRLOGGER_DLLEXPORT void krLoggerLog(krLogChannelNo channel, krLogLevel level,
const char* fmtString, ...)
Loading

0 comments on commit d67f4ec

Please sign in to comment.