From 6e2f29d6c21045b88b1f2cfdb303e2f7bcb53cf6 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Fri, 3 Oct 2014 12:35:15 +0200 Subject: [PATCH] class BaseLoop: config option added. (config reload implemented) --- ChangeLog | 9 ++++-- lib/logger.cpp | 16 +++++----- lib/logger.h | 4 +-- src/baseloop.cpp | 77 +++++++++++++++++++++++++----------------------- src/baseloop.h | 6 ++-- src/cycdata.cpp | 4 +++ src/cycdata.h | 5 ++-- src/main.cpp | 8 ++--- 8 files changed, 71 insertions(+), 58 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9ea24953..99b0b980 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,12 +1,17 @@ ebusd - ChangeLog ----------------- +2014-10-03 Roland Jax +* class Logger: minor name changes. +* class CYCData: delCommands fixed. +* class BaseLoop: config option added. (config reload implemented) + 2014-10-02 Roland Jax * class Network splitted into classes Network and Connection. -* class Message separated from class Baseloop. +* class Message separated from class BaseLoop. * global Objects (network, commands, ebusloop, cycdata) relocated into new global Object baseloop. -* daemon log option reworked (logarea -> logareas; loglevel -> log level). +* class BaseLoop: log option reworked (logarea -> logareas; loglevel -> log level). 2014-06-26 Roland Jax * ebusdump merged into ebusctl. diff --git a/lib/logger.cpp b/lib/logger.cpp index 139994cb..155a7edc 100644 --- a/lib/logger.cpp +++ b/lib/logger.cpp @@ -28,31 +28,31 @@ #include #include -static const char* AreaNames[Size_of_Area] = { "bas", "net", "bus", "cyc" }; +static const char* AreaNames[Size_of_Areas] = { "bas", "net", "bus", "cyc" }; static const char* LevelNames[Size_of_Level] = { "error", "event", "trace", "debug" }; -int calcArea(const std::string area) +int calcAreas(const std::string areas) { - int m_area = 0; + int m_areas = 0; // prepare data std::string token; - std::istringstream stream(area); + std::istringstream stream(areas); std::vector cmd; while (std::getline(stream, token, ',') != 0) cmd.push_back(token); for (std::vector::iterator it = cmd.begin() ; it != cmd.end(); ++it) - for (int i = 0; i < Size_of_Area; i++) { + for (int i = 0; i < Size_of_Areas; i++) { if (strcasecmp("all", it->c_str()) == 0) - return (pow(2, (int)Size_of_Area) - 1); + return (pow(2, (int)Size_of_Areas) - 1); if (strcasecmp(AreaNames[i], it->c_str()) == 0) - m_area += pow(2, i); + m_areas += pow(2, i); } - return m_area; + return m_areas; } int calcLevel(const std::string level) diff --git a/lib/logger.h b/lib/logger.h index 4bb543ae..91dd5485 100644 --- a/lib/logger.h +++ b/lib/logger.h @@ -28,10 +28,10 @@ #include #include -enum Area { bas=1, net=2, bus=4, cyc=8, all=15, Size_of_Area=4 }; +enum Areas { bas=1, net=2, bus=4, cyc=8, all=15, Size_of_Areas=4 }; enum Level { error=0, event, trace, debug, Size_of_Level }; -int calcArea(const std::string area); +int calcAreas(const std::string areas); int calcLevel(const std::string level); diff --git a/src/baseloop.cpp b/src/baseloop.cpp index 2fa032f1..6d7cf104 100644 --- a/src/baseloop.cpp +++ b/src/baseloop.cpp @@ -302,9 +302,9 @@ std::string BaseLoop::decodeMessage(const std::string& data) break; } - // ToDo: check for possible areas + // ToDo: check for possible areas and level if (strcasecmp(cmd[1].c_str(), "areas") == 0) { - L.getSink(0)->setAreas(calcArea(cmd[2])); + L.getSink(0)->setAreas(calcAreas(cmd[2])); result << "done"; break; } @@ -315,53 +315,56 @@ std::string BaseLoop::decodeMessage(const std::string& data) break; } + result << "usage: 'log areas area,area,..' (areas: bas|net|bus|cyc|all)" << std::endl + << " 'log level level' (level: error|event|trace|debug)"; + break; - //~ case cfgreload: + case config: + if (cmd.size() != 2) { + result << "usage: 'config list'" << std::endl + << " 'config reload'"; + break; + } - // free CYCData - //~ if (m_cycdata != NULL) { - //~ m_cycdata->stop(); - //~ delete m_cycdata; - //~ } + // ToDo: ... + if (strcasecmp(cmd[1].c_str(), "list") == 0) { + result << "done"; + break; + } - // free EBusLoop - //~ if (m_ebusloop != NULL) { - //~ m_ebusloop->stop(); - //~ m_ebusloop->join(); - //~ delete m_ebusloop; - //~ } + if (strcasecmp(cmd[1].c_str(), "reload") == 0) { - // free Commands DB - //~ if (m_commands != NULL) - //~ delete m_commands; + m_cycdata->delCommands(); + delete m_commands; - // create Commands DB - //~ m_commands = ConfigCommands(A.getParam("p_ebusconfdir"), CSV).getCommands(); - //~ L.log(bas, debug, "ebus configuration dir: %s", A.getParam("p_ebusconfdir")); - //~ L.log(bas, event, "commands DB with %d entries created", m_commands->size()); + // create Commands DB + m_commands = ConfigCommands(A.getParam("p_ebusconfdir"), CSV).getCommands(); + L.log(bas, debug, "ebus configuration dir: %s", A.getParam("p_ebusconfdir")); + L.log(bas, event, "commands DB with %d entries created", m_commands->size()); - // create EBusLoop - //~ m_ebusloop = new EBusLoop(); - //~ m_ebusloop->start("ebusloop"); + // add commands to cycDB + m_cycdata->addCommands(m_commands); - // create CYCData - //~ m_cycdata = new CYCData(m_ebusloop, m_commands); - //~ m_cycdata->start("cycdata"); + result << "done"; + break; + } - //~ result << "done"; - //~ break; + result << "usage: 'config list'" << std::endl + << " 'config reload'"; + break; case help: result << "commands:" << std::endl - << " get - fetch ebus data 'get class cmd (sub)'" << std::endl - << " set - set ebus values 'set class cmd value'" << std::endl - << " cyc - fetch cycle data 'cyc class cmd (sub)'" << std::endl - << " hex - send given hex value 'hex type value' (value: ZZPBSBNNDx)" << std::endl << std::endl - << " dump - change dump state 'dump state' (state: on|off)" << std::endl << std::endl - << " log areas - change log areas 'log areas area,area,..' (areas: bas|net|bus|cyc|all)" << std::endl - << " log level - change log level 'log level level' (level: error|event|trace|debug)" << std::endl << std::endl - //~ << " cfgreload - reload ebus configuration" << std::endl << std::endl + << " get - fetch ebus data 'get class cmd (sub)'" << std::endl + << " set - set ebus values 'set class cmd value'" << std::endl + << " cyc - fetch cycle data 'cyc class cmd (sub)'" << std::endl + << " hex - send given hex value 'hex type value' (value: ZZPBSBNNDx)" << std::endl << std::endl + << " dump - change dump state 'dump state' (state: on|off)" << std::endl << std::endl + << " log - change log areas 'log areas area,area,..' (areas: bas|net|bus|cyc|all)" << std::endl + << " - change log level 'log level level' (level: error|event|trace|debug)" << std::endl << std::endl + << " config - list ebus configuration 'config list'" << std::endl + << " - reload ebus configuration 'config reload'" << std::endl << std::endl << " stop - stop daemon" << std::endl << " quit - close connection" << std::endl << std::endl << " help - print this page"; diff --git a/src/baseloop.h b/src/baseloop.h index 8d66741e..4b40bfec 100644 --- a/src/baseloop.h +++ b/src/baseloop.h @@ -53,8 +53,8 @@ private: cyc, // fetch cycle data hex, // send hex value dump, // change dump state - log, // change loggins settings - //~ cfgreload, // reload ebus configuration + log, // logger settings + config, // ebus configuration help, // print commands notfound }; @@ -67,7 +67,7 @@ private: if (strcasecmp(item.c_str(), "hex") == 0) return hex; if (strcasecmp(item.c_str(), "dump") == 0) return dump; if (strcasecmp(item.c_str(), "log") == 0) return log; - //~ if (strcasecmp(item.c_str(), "cfgreload") == 0) return cfgreload; + if (strcasecmp(item.c_str(), "config") == 0) return config; if (strcasecmp(item.c_str(), "help") == 0) return help; return notfound; diff --git a/src/cycdata.cpp b/src/cycdata.cpp index 2befaf91..8ba74214 100644 --- a/src/cycdata.cpp +++ b/src/cycdata.cpp @@ -122,6 +122,8 @@ void CYCData::storeData(int index, std::string data) void CYCData::addCommands(Commands* commands) { + m_commands = commands; + for (size_t index = 0; index < commands->size(); index++) { if (strcasecmp((*m_commands)[index][0].c_str(),"cyc") == 0) { Command* cmd = new Command(index, (*m_commands)[index]); @@ -136,5 +138,7 @@ void CYCData::delCommands() { for (mapCI_t iter = m_cycDB.begin(); iter != m_cycDB.end(); ++iter) delete iter->second; + + m_cycDB.clear(); } diff --git a/src/cycdata.h b/src/cycdata.h index 876dcc7c..19c8ff64 100644 --- a/src/cycdata.h +++ b/src/cycdata.h @@ -22,6 +22,7 @@ #include "libebus.h" #include "ebusloop.h" +#include using namespace libebus; @@ -35,8 +36,8 @@ class CYCData : public Thread public: CYCData(EBusLoop* ebusloop, Commands* commands) - : m_ebusloop(ebusloop), m_commands(commands), m_stop(false) - { addCommands(m_commands); } + : m_ebusloop(ebusloop), m_stop(false) + { addCommands(commands); } ~CYCData() { delCommands(); } void* run(); diff --git a/src/main.cpp b/src/main.cpp index 526db68e..7598016a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,8 +76,8 @@ void define_args() "\tlog file name (/var/log/ebusd.log)", Appl::type_string, Appl::opt_mandatory); - A.addItem("p_logarea", Appl::Param("all"), "", "logarea", - "\tlog area - bas|net|bus|cyc|all (all)", + A.addItem("p_logareas", Appl::Param("all"), "", "logareas", + "\tlog areas - bas|net|bus|cyc|all (all)", Appl::type_string, Appl::opt_mandatory); A.addItem("p_loglevel", Appl::Param("trace"), "", "loglevel", @@ -170,12 +170,12 @@ int main(int argc, char* argv[]) // make me Daemon if (A.getParam("p_foreground") == true) { - L += new LogConsole(calcArea(A.getParam("p_logarea")), + L += new LogConsole(calcAreas(A.getParam("p_logareas")), calcLevel(A.getParam("p_loglevel")), "logconsole"); } else { D.run("/var/run/ebusd.pid"); - L += new LogFile(calcArea(A.getParam("p_logarea")), + L += new LogFile(calcAreas(A.getParam("p_logareas")), calcLevel(A.getParam("p_loglevel")), "logfile", A.getParam("p_logfile")); }