moved config file reading to ebusd.cpp, enhanced config checking mode
This commit is contained in:
+3
-60
@@ -21,7 +21,6 @@
|
|||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "appl.h"
|
#include "appl.h"
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
#include <dirent.h>
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -104,69 +103,13 @@ BaseLoop::~BaseLoop()
|
|||||||
delete m_templates;
|
delete m_templates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern result_t loadConfigFiles(DataFieldTemplates* templates, MessageMap* messages, bool verbose=false);
|
||||||
|
|
||||||
result_t BaseLoop::loadMessages()
|
result_t BaseLoop::loadMessages()
|
||||||
{
|
{
|
||||||
string path = A.getOptVal<const char*>("configpath");
|
return loadConfigFiles(m_templates, m_messages);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
void BaseLoop::start()
|
||||||
{
|
{
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|||||||
@@ -64,14 +64,6 @@ public:
|
|||||||
*/
|
*/
|
||||||
result_t loadMessages();
|
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.
|
* @brief start baseloop instance.
|
||||||
*/
|
*/
|
||||||
|
|||||||
+105
-5
@@ -27,6 +27,8 @@
|
|||||||
#include "baseloop.h"
|
#include "baseloop.h"
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <dirent.h>
|
||||||
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -34,7 +36,7 @@ Appl& A = Appl::Instance();
|
|||||||
Daemon& D = Daemon::Instance();
|
Daemon& D = Daemon::Instance();
|
||||||
Logger& L = Logger::Instance();
|
Logger& L = Logger::Instance();
|
||||||
|
|
||||||
BaseLoop* baseloop;
|
BaseLoop* baseloop = NULL;
|
||||||
|
|
||||||
void define_args()
|
void define_args()
|
||||||
{
|
{
|
||||||
@@ -113,7 +115,8 @@ void define_args()
|
|||||||
void shutdown()
|
void shutdown()
|
||||||
{
|
{
|
||||||
// stop threads
|
// stop threads
|
||||||
delete baseloop;
|
if (baseloop != NULL)
|
||||||
|
delete baseloop;
|
||||||
|
|
||||||
// reset all signal handlers to default
|
// reset all signal handlers to default
|
||||||
signal(SIGHUP, SIG_DFL);
|
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<const char*>("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[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
// define arguments and application variables
|
// define arguments and application variables
|
||||||
@@ -160,8 +243,26 @@ int main(int argc, char* argv[])
|
|||||||
// parse arguments
|
// parse arguments
|
||||||
A.parseArgs(argc, argv);
|
A.parseArgs(argc, argv);
|
||||||
|
|
||||||
|
if (A.getOptVal<bool>("checkconfig") == true) {
|
||||||
|
L += new LogConsole(calcAreaMask(A.getOptVal<const char*>("logareas")),
|
||||||
|
calcLevel(A.getOptVal<const char*>("loglevel")),
|
||||||
|
"logconsole");
|
||||||
|
|
||||||
|
DataFieldTemplates templates;
|
||||||
|
MessageMap messages;
|
||||||
|
|
||||||
|
loadConfigFiles(&templates, &messages, true);
|
||||||
|
|
||||||
|
messages.clear();
|
||||||
|
templates.clear();
|
||||||
|
|
||||||
|
shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// make me daemon or checkconfig is true
|
// make me daemon or checkconfig is true
|
||||||
if (A.getOptVal<bool>("foreground") == true || A.getOptVal<bool>("checkconfig") == true) {
|
if (A.getOptVal<bool>("foreground") == true) {
|
||||||
L += new LogConsole(calcAreaMask(A.getOptVal<const char*>("logareas")),
|
L += new LogConsole(calcAreaMask(A.getOptVal<const char*>("logareas")),
|
||||||
calcLevel(A.getOptVal<const char*>("loglevel")),
|
calcLevel(A.getOptVal<const char*>("loglevel")),
|
||||||
"logconsole");
|
"logconsole");
|
||||||
@@ -185,8 +286,7 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
// create baseloop
|
// create baseloop
|
||||||
baseloop = new BaseLoop();
|
baseloop = new BaseLoop();
|
||||||
if (A.getOptVal<bool>("checkconfig") == false)
|
baseloop->start();
|
||||||
baseloop->start();
|
|
||||||
|
|
||||||
// shutdown
|
// shutdown
|
||||||
shutdown();
|
shutdown();
|
||||||
|
|||||||
Reference in New Issue
Block a user