Sylvain patch #2, with modifications:

CommonLibs: Avoid direct syslog calls in ConfigurationTable

    We instead introduce a 'log early' facility in Logger.h to accomplish
    the same

    Signed-off-by: Sylvain Munaut <tnt@246tNt.com>

I added similar code to the reporting functions, which did not exist when sylvain made this patch

git-svn-id: http://wush.net/svn/range/software/public/openbts/trunk@4629 19bc5d8c-e614-43d4-8b26-e1612bc8e597
This commit is contained in:
kurtis.heimerl 2012-12-16 06:08:18 +00:00
parent db70eb4c6e
commit 00913d74d9
5 changed files with 25 additions and 17 deletions

View File

@ -27,10 +27,11 @@
#include "Configuration.h" #include "Configuration.h"
#include "Logger.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string.h> #include <string.h>
#include <syslog.h>
using namespace std; using namespace std;
@ -56,10 +57,9 @@ float ConfigurationRecord::floatNumber() const
} }
ConfigurationTable::ConfigurationTable(const char* filename, const char *wCmdName, int wFacility) ConfigurationTable::ConfigurationTable(const char* filename, const char *wCmdName)
:mFacility(wFacility)
{ {
syslog(LOG_INFO | mFacility, "opening configuration table from path %s", filename); gLogEarly(LOG_INFO, "opening configuration table from path %s", filename);
// Connect to the database. // Connect to the database.
int rc = sqlite3_open(filename,&mDB); int rc = sqlite3_open(filename,&mDB);
// (pat) When I used malloc here, sqlite3 sporadically crashes. // (pat) When I used malloc here, sqlite3 sporadically crashes.
@ -69,14 +69,14 @@ ConfigurationTable::ConfigurationTable(const char* filename, const char *wCmdNam
strcat(gCmdName,":"); strcat(gCmdName,":");
} }
if (rc) { if (rc) {
syslog(LOG_EMERG | mFacility, "cannot open configuration database at %s, error message: %s", filename, sqlite3_errmsg(mDB)); gLogEarly(LOG_EMERG, "cannot open configuration database at %s, error message: %s", filename, sqlite3_errmsg(mDB));
sqlite3_close(mDB); sqlite3_close(mDB);
mDB = NULL; mDB = NULL;
return; return;
} }
// Create the table, if needed. // Create the table, if needed.
if (!sqlite3_command(mDB,createConfigTable)) { if (!sqlite3_command(mDB,createConfigTable)) {
syslog(LOG_EMERG | mFacility, "cannot create configuration table in database at %s, error message: %s", filename, sqlite3_errmsg(mDB)); gLogEarly(LOG_EMERG, "cannot create configuration table in database at %s, error message: %s", filename, sqlite3_errmsg(mDB));
} }
} }
@ -175,7 +175,7 @@ string ConfigurationTable::getStr(const string& key)
return lookup(key).value(); return lookup(key).value();
} catch (ConfigurationTableKeyNotFound) { } catch (ConfigurationTableKeyNotFound) {
// Raise an alert and re-throw the exception. // Raise an alert and re-throw the exception.
syslog(LOG_ALERT | mFacility, "configuration parameter %s has no defined value", key.c_str()); gLogEarly(LOG_ALERT, "configuration parameter %s has no defined value", key.c_str());
throw ConfigurationTableKeyNotFound(key); throw ConfigurationTableKeyNotFound(key);
} }
} }
@ -186,7 +186,7 @@ string ConfigurationTable::getStr(const string& key, const char* defaultValue)
ScopedLock lock(mLock); ScopedLock lock(mLock);
return lookup(key).value(); return lookup(key).value();
} catch (ConfigurationTableKeyNotFound) { } catch (ConfigurationTableKeyNotFound) {
syslog(LOG_NOTICE | mFacility, "deinfing missing parameter %s with value %s", key.c_str(),defaultValue); gLogEarly(LOG_NOTICE, "deinfing missing parameter %s with value %s", key.c_str(),defaultValue);
set(key,defaultValue); set(key,defaultValue);
return string(defaultValue); return string(defaultValue);
} }
@ -211,7 +211,7 @@ long ConfigurationTable::getNum(const string& key)
return lookup(key).number(); return lookup(key).number();
} catch (ConfigurationTableKeyNotFound) { } catch (ConfigurationTableKeyNotFound) {
// Raise an alert and re-throw the exception. // Raise an alert and re-throw the exception.
syslog(LOG_ALERT | mFacility, "configuration parameter %s has no defined value", key.c_str()); gLogEarly(LOG_ALERT, "configuration parameter %s has no defined value", key.c_str());
throw ConfigurationTableKeyNotFound(key); throw ConfigurationTableKeyNotFound(key);
} }
} }
@ -223,7 +223,7 @@ long ConfigurationTable::getNum(const string& key, long defaultValue)
ScopedLock lock(mLock); ScopedLock lock(mLock);
return lookup(key).number(); return lookup(key).number();
} catch (ConfigurationTableKeyNotFound) { } catch (ConfigurationTableKeyNotFound) {
syslog(LOG_NOTICE | mFacility, "deinfing missing parameter %s with value %ld", key.c_str(),defaultValue); gLogEarly(LOG_NOTICE, "deinfing missing parameter %s with value %ld", key.c_str(),defaultValue);
set(key,defaultValue); set(key,defaultValue);
return defaultValue; return defaultValue;
} }
@ -247,7 +247,7 @@ std::vector<string> ConfigurationTable::getVectorOfStrings(const string& key)
line = strdup(rec.value().c_str()); line = strdup(rec.value().c_str());
} catch (ConfigurationTableKeyNotFound) { } catch (ConfigurationTableKeyNotFound) {
// Raise an alert and re-throw the exception. // Raise an alert and re-throw the exception.
syslog(LOG_ALERT | mFacility, "configuration parameter %s has no defined value", key.c_str()); gLogEarly(LOG_ALERT, "configuration parameter %s has no defined value", key.c_str());
throw ConfigurationTableKeyNotFound(key); throw ConfigurationTableKeyNotFound(key);
} }
@ -289,7 +289,7 @@ std::vector<unsigned> ConfigurationTable::getVector(const string& key)
line = strdup(rec.value().c_str()); line = strdup(rec.value().c_str());
} catch (ConfigurationTableKeyNotFound) { } catch (ConfigurationTableKeyNotFound) {
// Raise an alert and re-throw the exception. // Raise an alert and re-throw the exception.
syslog(LOG_ALERT | mFacility, "configuration parameter %s has no defined value", key.c_str()); gLogEarly(LOG_ALERT, "configuration parameter %s has no defined value", key.c_str());
throw ConfigurationTableKeyNotFound(key); throw ConfigurationTableKeyNotFound(key);
} }

View File

@ -33,7 +33,6 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <syslog.h>
#include <map> #include <map>
#include <vector> #include <vector>
@ -181,12 +180,11 @@ class ConfigurationTable {
sqlite3* mDB; ///< database connection sqlite3* mDB; ///< database connection
ConfigurationMap mCache; ///< cache of recently access configuration values ConfigurationMap mCache; ///< cache of recently access configuration values
mutable Mutex mLock; ///< control for multithreaded access to the cache mutable Mutex mLock; ///< control for multithreaded access to the cache
int mFacility;
public: public:
ConfigurationTable(const char* filename = ":memory:", const char *wCmdName = 0, int wFacility = LOG_USER); ConfigurationTable(const char* filename = ":memory:", const char *wCmdName = 0);
/** Return true if the key is used in the table. */ /** Return true if the key is used in the table. */
bool defines(const std::string& key); bool defines(const std::string& key);

View File

@ -32,7 +32,7 @@
using namespace std; using namespace std;
ConfigurationTable gConfig("exampleconfig.db","test",LOG_LOCAL7); ConfigurationTable gConfig("exampleconfig.db","test");
void purgeConfig(void*,int,char const*, char const*, sqlite3_int64) void purgeConfig(void*,int,char const*, char const*, sqlite3_int64)
{ {

View File

@ -28,6 +28,7 @@
#include <cstdio> #include <cstdio>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <stdarg.h>
#include "Configuration.h" #include "Configuration.h"
#include "Logger.h" #include "Logger.h"
@ -200,6 +201,13 @@ void gLogInit(const char* name, const char* level, int facility)
} }
void gLogEarly(int level, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vsyslog(level | LOG_USER, fmt, args);
va_end(args);
}
// vim: ts=4 sw=4 // vim: ts=4 sw=4

View File

@ -102,6 +102,8 @@ std::list<std::string> gGetLoggerAlarms(); ///< Get a copy of the recent alarm
void gLogInit(const char* name, const char* level=NULL, int facility=LOG_USER); void gLogInit(const char* name, const char* level=NULL, int facility=LOG_USER);
/** Get the logging level associated with a given file. */ /** Get the logging level associated with a given file. */
int gGetLoggingLevel(const char *filename=NULL); int gGetLoggingLevel(const char *filename=NULL);
/** Allow early logging when still in constructors */
void gLogEarly(int level, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
//@} //@}