diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 2de262db..8cf6a896 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -21,7 +21,6 @@ #include "logger.h" #include "appl.h" #include "data.h" -#include #include using namespace std; @@ -104,69 +103,13 @@ BaseLoop::~BaseLoop() delete m_templates; } +extern result_t loadConfigFiles(DataFieldTemplates* templates, MessageMap* messages, bool verbose=false); + result_t BaseLoop::loadMessages() { - string path = A.getOptVal("configpath"); - L.log(bas, trace, "path to ebus configuration files: %s", path.c_str()); - m_messages->clear(); - m_templates->clear(); - result_t result = m_templates->readFromFile(path+"/_templates.csv"); - if (result == RESULT_OK) - L.log(bas, trace, "read templates"); - else - L.log(bas, error, "error reading templates: %s", getResultCode(result)); - - result = readConfigFiles(path, ".csv"); - if (result == RESULT_OK) { - L.log(bas, trace, "read config files"); - - L.log(bas, event, "message DB: %d ", m_messages->size()); - L.log(bas, event, "updates DB: %d ", m_messages->size(true)); - L.log(bas, event, "polling DB: %d ", m_messages->sizePoll()); - } else - L.log(bas, error, "error reading config files: %s", getResultCode(result)); - - return result; + return loadConfigFiles(m_templates, m_messages); } -result_t BaseLoop::readConfigFiles(const string path, const string extension) -{ - DIR* dir = opendir(path.c_str()); - - if (dir == NULL) - return RESULT_ERR_NOTFOUND; - - dirent* d = readdir(dir); - - while (d != NULL) { - if (d->d_type == DT_DIR) { - string fn = d->d_name; - - if (fn != "." && fn != "..") { - const string p = path + "/" + d->d_name; - result_t result = readConfigFiles(p, extension); - if (result != RESULT_OK) - return result; - } - } else if (d->d_type == DT_REG || d->d_type == DT_LNK) { - string fn = d->d_name; - - if (fn.find(extension, (fn.length() - extension.length())) != string::npos - && fn != "_templates" + extension) { - const string p = path + "/" + d->d_name; - result_t result = m_messages->readFromFile(p, m_templates); - if (result != RESULT_OK) - return result; - } - } - - d = readdir(dir); - } - closedir(dir); - - return RESULT_OK; -}; - void BaseLoop::start() { for (;;) { diff --git a/src/ebusd/baseloop.h b/src/ebusd/baseloop.h index ba3da3cd..a84f8b38 100644 --- a/src/ebusd/baseloop.h +++ b/src/ebusd/baseloop.h @@ -64,14 +64,6 @@ public: */ result_t loadMessages(); - /** - * @brief Read the configuration files from the specified path. - * @param path the path from which to read the files. - * @param extension the filename extension of the files to read. - * @return the result code. - */ - result_t readConfigFiles(const string path, const string extension); - /** * @brief start baseloop instance. */ diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index 8a72f18e..323b37f7 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -27,6 +27,8 @@ #include "baseloop.h" #include #include +#include + using namespace std; @@ -34,7 +36,7 @@ Appl& A = Appl::Instance(); Daemon& D = Daemon::Instance(); Logger& L = Logger::Instance(); -BaseLoop* baseloop; +BaseLoop* baseloop = NULL; void define_args() { @@ -113,7 +115,8 @@ void define_args() void shutdown() { // stop threads - delete baseloop; + if (baseloop != NULL) + delete baseloop; // reset all signal handlers to default signal(SIGHUP, SIG_DFL); @@ -152,6 +155,86 @@ void signal_handler(int sig) } } +/** + * @brief Read the configuration files from the specified path. + * @param path the path from which to read the files. + * @param extension the filename extension of the files to read. + * @param logFunc the function to call for logging, or @a NULL to be silent. + * @param templates the available @a DataFieldTemplates. + * @param messages the @a MessageMap to load the messages into. + * @param verbose whether to verbosely log problems. + * @return the result code. + */ +static result_t readConfigFiles(const string path, const string extension, DataFieldTemplates* templates, MessageMap* messages, bool verbose) +{ + DIR* dir = opendir(path.c_str()); + + if (dir == NULL) + return RESULT_ERR_NOTFOUND; + + dirent* d = readdir(dir); + + while (d != NULL) { + if (d->d_type == DT_DIR) { + string fn = d->d_name; + + if (fn != "." && fn != "..") { + const string p = path + "/" + d->d_name; + result_t result = readConfigFiles(p, extension, templates, messages, verbose); + if (result != RESULT_OK) + return result; + } + } else if (d->d_type == DT_REG || d->d_type == DT_LNK) { + string fn = d->d_name; + + if (fn.find(extension, (fn.length() - extension.length())) != string::npos + && fn != "_templates" + extension) { + const string p = path + "/" + d->d_name; + result_t result = messages->readFromFile(p, templates, verbose); + if (result != RESULT_OK) + return result; + } + } + + d = readdir(dir); + } + closedir(dir); + + return RESULT_OK; +}; + +/** + * @brief Load the message definitions from the configuration files. + * @param templates the @a DataFieldTemplates to load the templates into. + * @param messages the @a MessageMap to load the messages into. + * @param verbose whether to verbosely log problems. + * @return the result code. + */ +result_t loadConfigFiles(DataFieldTemplates* templates, MessageMap* messages, bool verbose=false) { + string path = A.getOptVal("configpath"); + L.log(bas, trace, "path to ebus configuration files: %s", path.c_str()); + messages->clear(); + templates->clear(); + result_t result = templates->readFromFile(path+"/_templates.csv", NULL, verbose); + if (result == RESULT_OK) + L.log(bas, trace, "read templates"); + else + L.log(bas, error, "error reading templates: %s", getResultCode(result)); + + result = readConfigFiles(path, ".csv", templates, messages, verbose); + if (result == RESULT_OK) + L.log(bas, trace, "read config files"); + else + L.log(bas, error, "error reading config files: %s", getResultCode(result)); + + L.log(bas, event, "message DB: %d ", messages->size()); + L.log(bas, event, "updates DB: %d ", messages->size(true)); + L.log(bas, event, "polling DB: %d ", messages->sizePoll()); + + return result; +} + + int main(int argc, char* argv[]) { // define arguments and application variables @@ -160,8 +243,26 @@ int main(int argc, char* argv[]) // parse arguments A.parseArgs(argc, argv); + if (A.getOptVal("checkconfig") == true) { + L += new LogConsole(calcAreaMask(A.getOptVal("logareas")), + calcLevel(A.getOptVal("loglevel")), + "logconsole"); + + DataFieldTemplates templates; + MessageMap messages; + + loadConfigFiles(&templates, &messages, true); + + messages.clear(); + templates.clear(); + + shutdown(); + + return 0; + } + // make me daemon or checkconfig is true - if (A.getOptVal("foreground") == true || A.getOptVal("checkconfig") == true) { + if (A.getOptVal("foreground") == true) { L += new LogConsole(calcAreaMask(A.getOptVal("logareas")), calcLevel(A.getOptVal("loglevel")), "logconsole"); @@ -185,8 +286,7 @@ int main(int argc, char* argv[]) // create baseloop baseloop = new BaseLoop(); - if (A.getOptVal("checkconfig") == false) - baseloop->start(); + baseloop->start(); // shutdown shutdown();