[PATCH] osmo-trx[master]: Logger: Stop using Log.File and Log.Level from config

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Tue Jan 9 14:42:14 UTC 2018


Review at  https://gerrit.osmocom.org/5702

Logger: Stop using Log.File and Log.Level from config

This is a required step towards getting rid of ConfigurationTable class
and libsqlite dependency.

As a side effect, support for different log levels for different files
is dropped, but it's not something really being used and we will end up
dropping current logging system in favour of osmocom's one in the future
anyway.

Change-Id: I51cb12d1ab7e103e78190ac71a70fb5bb1d9ff51
---
M CommonLibs/Logger.cpp
M CommonLibs/Logger.h
2 files changed, 17 insertions(+), 93 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/02/5702/1

diff --git a/CommonLibs/Logger.cpp b/CommonLibs/Logger.cpp
index d246584..1c462e7 100644
--- a/CommonLibs/Logger.cpp
+++ b/CommonLibs/Logger.cpp
@@ -50,7 +50,8 @@
 
 // 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 @@
 	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;
@@ -138,62 +123,6 @@
 {
 	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<uint64_t,int>  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<uint64_t,int>::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<uint64_t,int>(key,level));
-	sLogCacheLock.unlock();
-	return level;
-}
-
-
-
-
 
 // copies the alarm list and returns it. list supposed to be small.
 list<string> gGetLoggerAlarms()
@@ -268,26 +197,21 @@
 
 
 
-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 68c5a9b..099d300 100644
--- a/CommonLibs/Logger.h
+++ b/CommonLibs/Logger.h
@@ -40,11 +40,13 @@
 #include <map>
 #include <string>
 
+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 @@
 /**@ 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)));
 //@}

-- 
To view, visit https://gerrit.osmocom.org/5702
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I51cb12d1ab7e103e78190ac71a70fb5bb1d9ff51
Gerrit-PatchSet: 1
Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list