From f6c5a05c8e0f7f4d55877c064d33ad7e6bfda0b4 Mon Sep 17 00:00:00 2001 From: sehraf Date: Fri, 17 Jun 2016 11:09:59 +0200 Subject: [PATCH] switched to caller providing all needed information instead of saving those on callee side --- libretroshare/src/util/rsdebug.cc | 85 ++++++++++++++++--------------- libretroshare/src/util/rsdebug.h | 34 ++++++++++--- 2 files changed, 70 insertions(+), 49 deletions(-) diff --git a/libretroshare/src/util/rsdebug.cc b/libretroshare/src/util/rsdebug.cc index 312d2a877..e2536a7cb 100644 --- a/libretroshare/src/util/rsdebug.cc +++ b/libretroshare/src/util/rsdebug.cc @@ -37,8 +37,8 @@ const int RS_DEBUG_LOGCRASH = 3; /* minimal logfile stored after crashes */ const int RS_DEBUG_LOGC_MAX = 100000; /* max length of crashfile log */ const int RS_DEBUG_LOGC_MIN_SAVE = 100; /* min length of crashfile log */ -static std::map zoneLevel; -static int defaultLevel = RSL_WARNING; +//static std::map zoneLevel; +static RsLog::logLvl defaultLevel = RsLog::Warning; static FILE *ofd = stderr; static int debugMode = RS_DEBUG_STDERR; @@ -49,7 +49,6 @@ static int debugTS = 0; static RsMutex logMtx("logMtx"); int locked_setDebugFile(const char *fname); -int locked_getZoneLevel(int zone); int setDebugCrashMode(const char *cfile) { @@ -138,7 +137,6 @@ int clearDebugCrashLog() } #endif - int setDebugFile(const char *fname) { RsStackMutex stack(logMtx); /******** LOCKED ****************/ @@ -162,45 +160,51 @@ int locked_setDebugFile(const char *fname) } } +//int setOutputLevel(RsLog::logLvl lvl) +//{ +// RsStackMutex stack(logMtx); /******** LOCKED ****************/ +// return defaultLevel = lvl; +//} -int setOutputLevel(int lvl) +//#ifdef deadcode // this code is not used by RS +//int setZoneLevel(int lvl, int zone) +//{ +// RsStackMutex stack(logMtx); /******** LOCKED ****************/ +// zoneLevel[zone] = lvl; +// return zone; +//} +//#endif + +//int getZoneLevel(int zone) +//{ +// RsStackMutex stack(logMtx); /******** LOCKED ****************/ +// return locked_getZoneLevel(zone); +//} + +//int locked_getZoneLevel(int /*zone*/) +//{ +//#ifdef deadcode // this code is not used by RS +// std::map::iterator it = zoneLevel.find(zone); +// if (it == zoneLevel.end()) +// { +// return defaultLevel; +// } +// return it -> second; +//#endif +// return defaultLevel; +//} + +void rslog(const RsLog::logLvl lvl, const RsLog::logInfo *info, const std::string &msg) { + // skipp when log level is set to 'None' + // NB: when default is set to 'None' the later check will always fail -> no need to check it here + if(info->lvl == RsLog::None) + return; + RsStackMutex stack(logMtx); /******** LOCKED ****************/ - return defaultLevel = lvl; -} -#ifdef deadcode // this code is not used by RS -int setZoneLevel(int lvl, int zone) -{ - RsStackMutex stack(logMtx); /******** LOCKED ****************/ - zoneLevel[zone] = lvl; - return zone; -} -#endif - -int getZoneLevel(int zone) -{ - RsStackMutex stack(logMtx); /******** LOCKED ****************/ - return locked_getZoneLevel(zone); -} - -int locked_getZoneLevel(int /*zone*/) -{ -#ifdef deadcode // this code is not used by RS - std::map::iterator it = zoneLevel.find(zone); - if (it == zoneLevel.end()) - { - return defaultLevel; - } - return it -> second; -#endif - return defaultLevel; -} - -int rslog(unsigned int lvl, int zone, const std::string &msg) -{ - RsStackMutex stack(logMtx); /******** LOCKED ****************/ - if ((signed) lvl <= locked_getZoneLevel(zone)) + bool process = info-lvl == RsLog::Default ? (lvl <= defaultLevel) : lvl <= info->lvl; + if(process) { time_t t = time(NULL); @@ -232,11 +236,10 @@ int rslog(unsigned int lvl, int zone, const std::string &msg) std::string timestr2 = timestr.substr(0,timestr.length()-1); /* remove the endl */ fprintf(ofd, "(%s Z: %d, lvl:%u): %s \n", - timestr2.c_str(), zone, lvl, msg.c_str()); + timestr2.c_str(), zone, (unsigned int)info->lvl, msg.c_str()); fflush(ofd); lineCount++; } - return 1; } diff --git a/libretroshare/src/util/rsdebug.h b/libretroshare/src/util/rsdebug.h index 1e2d58b52..1d8f3cae0 100644 --- a/libretroshare/src/util/rsdebug.h +++ b/libretroshare/src/util/rsdebug.h @@ -30,8 +30,6 @@ #ifndef RS_LOG_DEBUG_H #define RS_LOG_DEBUG_H - - #define RSL_NONE -1 #define RSL_ALERT 1 #define RSL_ERROR 3 @@ -40,17 +38,37 @@ #define RSL_DEBUG_BASIC 8 #define RSL_DEBUG_ALL 10 - #include +namespace RsLog { + enum logLvl { + None = -1, + Default = 0, + Alert = 1, + Error = 3, + Warning = 5, + Debug_Alert = 6, + Debug_Basic = 8, + Debug_All = 10 + }; + + // this struct must be provided by the caller (to rslog()) + struct logInfo { + // module name + const std::string name; + // module specific log lvl + logLvl lvl; + }; +} + int setDebugCrashMode(const char *cfile); -int clearDebugCrashLog(); +//int clearDebugCrashLog(); int setDebugFile(const char *fname); -int setOutputLevel(int lvl); -int setZoneLevel(int lvl, int zone); -int getZoneLevel(int zone); -int rslog(unsigned int lvl, int zone, const std::string &msg); +int setOutputLevel(RsLog::logLvl lvl); +//int setZoneLevel(int lvl, int zone); +//int getZoneLevel(int zone); +void rslog(const RsLog::logLvl lvl, const RsLog::logInfo *info, const std::string &msg);