diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index a7be8ecb6..2e58b8f4b 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -41,8 +41,6 @@ #include "util/rsprint.h" #include "util/rsdebug.h" #include "util/rsstring.h" -const int p3connectzone = 3431; - #include "serialiser/rsconfigitems.h" #include "retroshare/rsiface.h" @@ -52,6 +50,8 @@ const int p3connectzone = 3431; /* Network setup States */ +static struct RsLog::logInfo p3connectzoneInfo = {RsLog::Default, "p3connect"}; +#define p3connectzone &p3connectzoneInfo /**** * #define LINKMGR_DEBUG 1 diff --git a/libretroshare/src/pqi/p3netmgr.cc b/libretroshare/src/pqi/p3netmgr.cc index 501046f51..620175c25 100644 --- a/libretroshare/src/pqi/p3netmgr.cc +++ b/libretroshare/src/pqi/p3netmgr.cc @@ -39,7 +39,8 @@ //#include "util/rsprint.h" //#include "util/rsdebug.h" -const int p3netmgrzone = 7563; +struct RsLog::logInfo p3netmgrzoneInfo = {RsLog::Default, "p3netmgr"}; +#define p3netmgrzone &p3netmgrzoneInfo #include "serialiser/rsconfigitems.h" #include "retroshare/rsiface.h" diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index 4481e7a50..53ac4f12f 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -41,7 +41,6 @@ #include "util/rsprint.h" #include "util/rsstring.h" #include "util/rsdebug.h" -const int p3peermgrzone = 9531; #include "serialiser/rsconfigitems.h" @@ -64,6 +63,9 @@ const uint32_t MIN_TIME_BETWEEN_NET_RESET = 5; const uint32_t PEER_IP_CONNECT_STATE_MAX_LIST_SIZE = 4; +static struct RsLog::logInfo p3peermgrzoneInfo = {RsLog::Default, "p3peermgr"}; +#define p3peermgrzone &p3peermgrzoneInfo + /**** * #define PEER_DEBUG 1 ***/ diff --git a/libretroshare/src/pqi/pqihandler.cc b/libretroshare/src/pqi/pqihandler.cc index c9c87826b..718e877c1 100644 --- a/libretroshare/src/pqi/pqihandler.cc +++ b/libretroshare/src/pqi/pqihandler.cc @@ -58,7 +58,8 @@ static double getCurrentTS() } #endif -const int pqihandlerzone = 34283; +struct RsLog::logInfo pqihandlerzoneInfo = {RsLog::Default, "pqihandler"}; +#define pqihandlerzone &pqihandlerzoneInfo static const int PQI_HANDLER_NB_PRIORITY_LEVELS = 10 ; static const float PQI_HANDLER_NB_PRIORITY_RATIO = 2 ; diff --git a/libretroshare/src/pqi/pqinetwork.cc b/libretroshare/src/pqi/pqinetwork.cc index e0cb9af90..4937aef64 100644 --- a/libretroshare/src/pqi/pqinetwork.cc +++ b/libretroshare/src/pqi/pqinetwork.cc @@ -41,7 +41,8 @@ #include "util/rsstring.h" #include "util/rsnet.h" -static const int pqinetzone = 96184; +static struct RsLog::logInfo pqinetzoneInfo = {RsLog::Default, "pqinet"}; +#define pqinetzone &pqinetzoneInfo /***** * #define NET_DEBUG 1 diff --git a/libretroshare/src/pqi/pqiperson.cc b/libretroshare/src/pqi/pqiperson.cc index 5bfe49b8f..ea81c7f8c 100644 --- a/libretroshare/src/pqi/pqiperson.cc +++ b/libretroshare/src/pqi/pqiperson.cc @@ -27,13 +27,13 @@ #include "pqi/pqiperson.h" #include "pqi/pqipersongrp.h" #include "pqi/pqissl.h" - - -const int pqipersonzone = 82371; #include "util/rsdebug.h" #include "util/rsstring.h" #include "retroshare/rspeers.h" +static struct RsLog::logInfo pqipersonzoneInfo = {RsLog::Default, "pqiperson"}; +#define pqipersonzone &pqipersonzoneInfo + /**** * #define PERSON_DEBUG 1 ****/ @@ -67,7 +67,7 @@ int pqiperson::SendItem(RsItem *i,uint32_t& serialized_size) // check if debug output is wanted, to avoid unecessary work // getZoneLevel() locks a global mutex and does a lookup in a map or returns a default value // (not sure if this is a performance problem) - if (PQL_DEBUG_BASIC <= getZoneLevel(pqipersonzone)) + if (PQL_DEBUG_BASIC <= pqipersonzoneInfo.lvl) { std::string out = "pqiperson::SendItem() Active: Sending On\n"; i->print_string(out, 5); // this can be very expensive @@ -80,7 +80,7 @@ int pqiperson::SendItem(RsItem *i,uint32_t& serialized_size) } else { - if (PQL_DEBUG_BASIC <= getZoneLevel(pqipersonzone)) + if (PQL_DEBUG_BASIC <= pqipersonzoneInfo.lvl) { std::string out = "pqiperson::SendItem()"; out += " Not Active: Used to put in ToGo Store\n"; diff --git a/libretroshare/src/pqi/pqipersongrp.cc b/libretroshare/src/pqi/pqipersongrp.cc index 8ccd67442..a30eb2738 100644 --- a/libretroshare/src/pqi/pqipersongrp.cc +++ b/libretroshare/src/pqi/pqipersongrp.cc @@ -31,7 +31,8 @@ #include -const int pqipersongrpzone = 354; +static struct RsLog::logInfo pqipersongrpzoneInfo = {RsLog::Default, "pqipersongrp"}; +#define pqipersongrpzone &pqipersongrpzoneInfo #ifdef WINDOWS_SYS /////////////////////////////////////////////////////////// diff --git a/libretroshare/src/pqi/pqissl.cc b/libretroshare/src/pqi/pqissl.cc index 317963336..031cd2d7f 100644 --- a/libretroshare/src/pqi/pqissl.cc +++ b/libretroshare/src/pqi/pqissl.cc @@ -44,7 +44,8 @@ #include "rsserver/p3face.h" -const int pqisslzone = 37714; +static struct RsLog::logInfo pqisslzoneInfo = {RsLog::Default, "pqisslzone"}; +#define pqisslzone &pqisslzoneInfo /********* #define WAITING_NOT 0 diff --git a/libretroshare/src/pqi/pqissllistener.cc b/libretroshare/src/pqi/pqissllistener.cc index 2f6371801..189eb5dce 100644 --- a/libretroshare/src/pqi/pqissllistener.cc +++ b/libretroshare/src/pqi/pqissllistener.cc @@ -38,7 +38,8 @@ #include "retroshare/rsbanlist.h" #include -const int pqissllistenzone = 49787; +static struct RsLog::logInfo pqissllistenzoneInfo = {RsLog::Default, "p3peermgr"}; +#define pqissllistenzone &pqissllistenzoneInfo /* NB: This #define makes the listener open 0.0.0.0:X port instead * of a specific port - this might help retroshare work on PCs with diff --git a/libretroshare/src/pqi/pqisslpersongrp.cc b/libretroshare/src/pqi/pqisslpersongrp.cc index 898b28bb5..a1173fd93 100644 --- a/libretroshare/src/pqi/pqisslpersongrp.cc +++ b/libretroshare/src/pqi/pqisslpersongrp.cc @@ -29,8 +29,8 @@ #include "pqi/pqisslpersongrp.h" #include "pqi/authssl.h" - -const int pqipersongrpzone = 354; +static struct RsLog::logInfo pqipersongrpzoneInfo = {RsLog::Default, "pqipersongrp"}; +#define pqipersongrpzone &pqipersongrpzoneInfo /**** * #define PQI_DISABLE_UDP 1 diff --git a/libretroshare/src/pqi/pqisslproxy.cc b/libretroshare/src/pqi/pqisslproxy.cc index 9af132b4e..90444bcf9 100755 --- a/libretroshare/src/pqi/pqisslproxy.cc +++ b/libretroshare/src/pqi/pqisslproxy.cc @@ -38,7 +38,8 @@ #include "pqi/p3linkmgr.h" -const int pqisslproxyzone = 3517; +static struct RsLog::logInfo pqisslproxyzoneInfo = {RsLog::Default, "pqisslproxy"}; +#define pqisslproxyzone &pqisslproxyzoneInfo // #define PROXY_DEBUG 1 diff --git a/libretroshare/src/pqi/pqissludp.cc b/libretroshare/src/pqi/pqissludp.cc index 5b097fad3..9519e2af3 100644 --- a/libretroshare/src/pqi/pqissludp.cc +++ b/libretroshare/src/pqi/pqissludp.cc @@ -40,7 +40,8 @@ #include "pqi/p3linkmgr.h" #include -const int pqissludpzone = 3144; +static struct RsLog::logInfo pqissludpzoneInfo = {RsLog::Default, "pqissludp"}; +#define pqissludpzone &pqissludpzoneInfo /* a final timeout, to ensure this never blocks completely * 300 secs to complete udp/tcp/ssl connection. diff --git a/libretroshare/src/pqi/pqistore.cc b/libretroshare/src/pqi/pqistore.cc index 887c931eb..ca6d23820 100644 --- a/libretroshare/src/pqi/pqistore.cc +++ b/libretroshare/src/pqi/pqistore.cc @@ -53,7 +53,8 @@ // #define PQISTORE_DEBUG // -const int pqistorezone = 9511; +static struct RsLog::logInfo pqistorezoneInfo = {RsLog::Default, "pqistore"}; +#define pqistorezone &pqistorezoneInfo pqistore::pqistore(RsSerialiser *rss, const RsPeerId& srcId, BinInterface *bio_in, int bio_flags_in) :PQInterface(RsPeerId()), rsSerialiser(rss), bio_flags(bio_flags_in), diff --git a/libretroshare/src/pqi/pqistreamer.cc b/libretroshare/src/pqi/pqistreamer.cc index b947bb723..f15bc3eee 100644 --- a/libretroshare/src/pqi/pqistreamer.cc +++ b/libretroshare/src/pqi/pqistreamer.cc @@ -37,7 +37,8 @@ #include "serialiser/rsserial.h" -const int pqistreamerzone = 8221; +static struct RsLog::logInfo pqistreamerzoneInfo = {RsLog::Default, "pqistreamer"}; +#define pqistreamerzone &pqistreamerzoneInfo static const int PQISTREAM_ABS_MAX = 100000000; /* 100 MB/sec (actually per loop) */ static const int PQISTREAM_AVG_PERIOD = 5; // update speed estimate every 5 seconds diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index ab2c30dea..733009932 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -221,7 +221,7 @@ void RsInit::InitRsConfig() /* Setup the Debugging */ // setup debugging for desired zones. - setOutputLevel(PQL_WARNING); // default to Warnings. + setOutputLevel(RsLog::Warning); // default to Warnings. // For Testing purposes. // We can adjust everything under Linux. @@ -418,7 +418,7 @@ int RsInit::InitRetroShare(int argcIgnored, char **argvIgnored, bool strictCheck } #endif - setOutputLevel(rsInitConfig->debugLevel); + setOutputLevel((RsLog::logLvl)rsInitConfig->debugLevel); // // set the default Debug Level... // if (rsInitConfig->haveDebugLevel) diff --git a/libretroshare/src/services/p3msgservice.cc b/libretroshare/src/services/p3msgservice.cc index 063d98c3e..d64c020d6 100644 --- a/libretroshare/src/services/p3msgservice.cc +++ b/libretroshare/src/services/p3msgservice.cc @@ -67,7 +67,8 @@ using namespace Rs::Msgs; -const int msgservicezone = 54319; +static struct RsLog::logInfo msgservicezoneInfo = {RsLog::Default, "msgservice"}; +#define msgservicezone &msgservicezoneInfo static const uint32_t RS_MSG_DISTANT_MESSAGE_HASH_KEEP_TIME = 2*30*86400 ; // keep msg hashes for 2 months to avoid re-sent msgs diff --git a/libretroshare/src/tcponudp/tcpstream.cc b/libretroshare/src/tcponudp/tcpstream.cc index 6e2f3b01b..d789def44 100644 --- a/libretroshare/src/tcponudp/tcpstream.cc +++ b/libretroshare/src/tcponudp/tcpstream.cc @@ -43,8 +43,8 @@ #include "util/rsstring.h" #include "util/rsrandom.h" -const int rstcpstreamzone = 28455; - +static struct RsLog::logInfo rstcpstreamzoneInfo = {RsLog::Default, "rstcpstream"}; +#define rstcpstreamzone &rstcpstreamzoneInfo /* * #define DEBUG_TCP_STREAM 1 diff --git a/libretroshare/src/util/rsdebug.cc b/libretroshare/src/util/rsdebug.cc index 8dcf2bf3f..ab40c3035 100644 --- a/libretroshare/src/util/rsdebug.cc +++ b/libretroshare/src/util/rsdebug.cc @@ -37,8 +37,7 @@ 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 RsLog::logLvl defaultLevel = RsLog::Warning; static FILE *ofd = stderr; static int debugMode = RS_DEBUG_STDERR; @@ -49,7 +48,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) { @@ -111,33 +109,6 @@ int setDebugCrashMode(const char *cfile) return 1; } - -/* this is called when we exit normally */ -int clearDebugCrashLog() -{ - RsStackMutex stack(logMtx); /******** LOCKED ****************/ - /* check we are in crashLog Mode */ - if (debugMode != RS_DEBUG_LOGCRASH) - { - fprintf(stderr, "Not in CrashLog Mode - nothing to clear!\n"); - return 1; - } - - fprintf(stderr, "clearDebugCrashLog() Cleaning up\n"); - /* shutdown crashLog Mode */ - fclose(ofd); - ofd = stderr; - debugMode = RS_DEBUG_STDERR; - - /* just open the file, and then close */ - FILE *tmpin = RsDirUtil::rs_fopen(crashfile.c_str(), "w"); - fclose(tmpin); - - return 1; -} - - - int setDebugFile(const char *fname) { RsStackMutex stack(logMtx); /******** LOCKED ****************/ @@ -161,41 +132,23 @@ int locked_setDebugFile(const char *fname) } } - -int setOutputLevel(int lvl) +int setOutputLevel(RsLog::logLvl lvl) { RsStackMutex stack(logMtx); /******** LOCKED ****************/ return defaultLevel = lvl; } -int setZoneLevel(int lvl, int zone) +void rslog(const RsLog::logLvl lvl, 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 ****************/ - zoneLevel[zone] = lvl; - return zone; -} - -int getZoneLevel(int zone) -{ - RsStackMutex stack(logMtx); /******** LOCKED ****************/ - return locked_getZoneLevel(zone); -} - -int locked_getZoneLevel(int zone) -{ - std::map::iterator it = zoneLevel.find(zone); - if (it == zoneLevel.end()) - { - return defaultLevel; - } - return it -> second; -} - -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); @@ -226,12 +179,11 @@ int rslog(unsigned int lvl, int zone, const std::string &msg) std::string timestr = ctime(&t); 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()); + fprintf(ofd, "(%s Z: %s, lvl: %u): %s \n", + timestr2.c_str(), info->name.c_str(), (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..abf39e4b0 100644 --- a/libretroshare/src/util/rsdebug.h +++ b/libretroshare/src/util/rsdebug.h @@ -30,34 +30,47 @@ #ifndef RS_LOG_DEBUG_H #define RS_LOG_DEBUG_H - - -#define RSL_NONE -1 -#define RSL_ALERT 1 -#define RSL_ERROR 3 -#define RSL_WARNING 5 -#define RSL_DEBUG_ALERT 6 -#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 specific log lvl + logLvl lvl; + // module name (displayed in log) + const std::string name; + }; +} + int setDebugCrashMode(const char *cfile); -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); +void rslog(const RsLog::logLvl lvl, RsLog::logInfo *info, const std::string &msg); /* * retaining old #DEFINES and functions for backward compatibility. */ +#define RSL_NONE RsLog::None +#define RSL_ALERT RsLog::Alert +#define RSL_ERROR RsLog::Error +#define RSL_WARNING RsLog::Warning +#define RSL_DEBUG_ALERT RsLog::Debug_Alert +#define RSL_DEBUG_BASIC RsLog::Debug_Basic +#define RSL_DEBUG_ALL RsLog::Debug_Basic + //int pqioutput(unsigned int lvl, int zone, std::string msg); #define pqioutput rslog