diff --git a/CommonLibs/Logger.cpp b/CommonLibs/Logger.cpp index d246584a..1c462e7a 100644 --- a/CommonLibs/Logger.cpp +++ b/CommonLibs/Logger.cpp @@ -50,7 +50,8 @@ Mutex gLogToLock; // Reference to a global config table, used all over the system. extern ConfigurationTable gConfig; - +// Global log level threshold: +int config_log_level; /**@ The global alarms table. */ //@{ @@ -97,22 +98,6 @@ int levelStringToInt(const string& name) return -1; } -/** Given a string, return the corresponding level name. */ -int lookupLevel(const string& key) -{ - string val = gConfig.getStr(key); - int level = levelStringToInt(val); - - if (level == -1) { - string defaultLevel = gConfig.mSchema["Log.Level"].getDefaultValue(); - level = levelStringToInt(defaultLevel); - _LOG(CRIT) << "undefined logging level (" << key << " = \"" << val << "\") defaulting to \"" << defaultLevel << ".\" Valid levels are: EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO or DEBUG"; - gConfig.set(key, defaultLevel); - } - - return level; -} - static std::string format(const char *fmt, ...) { va_list ap; @@ -139,62 +124,6 @@ std::ostream& operator<<(std::ostream& os, std::ostringstream& ss) return os << ss.str(); } -int getLoggingLevel(const char* filename) -{ - // Default level? - if (!filename) return lookupLevel("Log.Level"); - - // This can afford to be inefficient since it is not called that often. - const string keyName = string("Log.Level.") + string(filename); - if (gConfig.defines(keyName)) return lookupLevel(keyName); - return lookupLevel("Log.Level"); -} - - - -int gGetLoggingLevel(const char* filename) -{ - // This is called a lot and needs to be efficient. - - static Mutex sLogCacheLock; - static map sLogCache; - static unsigned sCacheCount; - static const unsigned sCacheRefreshCount = 1000; - - if (filename==NULL) return gGetLoggingLevel(""); - - HashString hs(filename); - uint64_t key = hs.hash(); - - sLogCacheLock.lock(); - // Time for a cache flush? - if (sCacheCount>sCacheRefreshCount) { - sLogCache.clear(); - sCacheCount=0; - } - // Is it cached already? - map::const_iterator where = sLogCache.find(key); - sCacheCount++; - if (where!=sLogCache.end()) { - int retVal = where->second; - sLogCacheLock.unlock(); - return retVal; - } - // Look it up in the config table and cache it. - // FIXME: Figure out why unlock and lock below fix the config table deadlock. - // (pat) Probably because getLoggingLevel may call LOG recursively via lookupLevel(). - sLogCacheLock.unlock(); - int level = getLoggingLevel(filename); - sLogCacheLock.lock(); - sLogCache.insert(pair(key,level)); - sLogCacheLock.unlock(); - return level; -} - - - - - // copies the alarm list and returns it. list supposed to be small. list gGetLoggerAlarms() { @@ -268,26 +197,21 @@ ostringstream& Log::get() -void gLogInit(const char* name, const char* level, int facility) +void gLogInit(const char* name, const char* level, int facility, char* fn) { // Set the level if one has been specified. - if (level) { - gConfig.set("Log.Level",level); - } + if (level) + config_log_level = levelStringToInt(level); // Both the transceiver and OpenBTS use this same facility, but only OpenBTS/OpenNodeB may use this log file: - string str = gConfig.getStr("Log.File"); - if (gLogToFile==NULL && str.length() && 0==strncmp(gCmdName,"Open",4)) { - const char *fn = str.c_str(); - if (fn && *fn && strlen(fn)>3) { // strlen because a garbage char is getting in sometimes. - gLogToFile = fopen(fn,"w"); // New log file each time we start. - if (gLogToFile) { - time_t now; - time(&now); - fprintf(gLogToFile,"Starting at %s",ctime(&now)); - fflush(gLogToFile); - std::cout << "Logging to file: " << fn << "\n"; - } + if (!gLogToFile && fn) { + gLogToFile = fopen(fn,"w"); // New log file each time we start. + if (gLogToFile) { + time_t now; + time(&now); + fprintf(gLogToFile,"Starting at %s",ctime(&now)); + fflush(gLogToFile); + std::cout << "Logging to file: " << fn << "\n"; } } diff --git a/CommonLibs/Logger.h b/CommonLibs/Logger.h index 68c5a9b4..099d3001 100644 --- a/CommonLibs/Logger.h +++ b/CommonLibs/Logger.h @@ -40,11 +40,13 @@ #include #include +extern int config_log_level; + #define _LOG(level) \ Log(LOG_##level).get() << pthread_self() \ << timestr() << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": " -#define IS_LOG_LEVEL(wLevel) (gGetLoggingLevel(__FILE__)>=LOG_##wLevel) +#define IS_LOG_LEVEL(wLevel) (config_log_level>=LOG_##wLevel) #ifdef NDEBUG #define LOG(wLevel) \ @@ -128,9 +130,7 @@ std::ostream& operator<<(std::ostream& os, std::ostringstream& ss); /**@ Global control and initialization of the logging system. */ //@{ /** Initialize the global logging system. */ -void gLogInit(const char* name, const char* level=NULL, int facility=LOG_USER); -/** Get the logging level associated with a given file. */ -int gGetLoggingLevel(const char *filename=NULL); +void gLogInit(const char* name, const char* level=NULL, int facility=LOG_USER, char* fn=NULL); /** Allow early logging when still in constructors */ void gLogEarly(int level, const char *fmt, ...) __attribute__((format(printf, 2, 3))); //@}