diff --git a/src/baseloop.cpp b/src/baseloop.cpp index 7bb2bed3..27d184de 100644 --- a/src/baseloop.cpp +++ b/src/baseloop.cpp @@ -78,7 +78,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) case get: if (cmd.size() < 3) { - result << "format: get class cmd (sub)"; + result << "format: [get class cmd (sub)]"; break; } @@ -120,7 +120,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) case set: if (cmd.size() != 4) { - result << "format: set class cmd value"; + result << "format: [set class cmd value]"; break; } @@ -173,7 +173,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) case cyc: if (cmd.size() < 3) { - result << "format: cyc class cmd (sub)"; + result << "format: [cyc class cmd (sub)]"; break; } @@ -201,7 +201,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) case dump: if (cmd.size() < 2) { - result << "format: dump on|off"; + result << "format: [dump state] (on|off)"; break; } @@ -210,27 +210,35 @@ std::string BaseLoop::decodeMessage(const std::string& data) result << "done"; break; - case log: + case logarea: if (cmd.size() < 2) { - result << "format: log error|event|trace|debug"; + result << "format: [logarea area,area,..] (bas|net|bus|cyc|all)"; break; } - if (cmd[1] == "error") L.getSink(0)->setLevel(error); - if (cmd[1] == "event") L.getSink(0)->setLevel(event); - if (cmd[1] == "trace") L.getSink(0)->setLevel(trace); - if (cmd[1] == "debug") L.getSink(0)->setLevel(debug); + L.getSink(0)->setAreas(calcArea(cmd[1])); + result << "done"; + break; + + case loglevel: + if (cmd.size() < 2) { + result << "format: [loglevel level] (error|event|trace|debug)"; + break; + } + + L.getSink(0)->setLevel(calcLevel(cmd[1])); result << "done"; break; case help: - result << " 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 - << " dump - change dump state [dump on|off]" << std::endl - << " log - change log level [log error|event|trace|debug]" << std::endl - << " quit - close connection" << std::endl - << " help - print this page"; + result << " 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 + << " dump - change dump state [dump state] (on|off)" << std::endl + << " logarea - change log area [logarea area,area,..] (bas|net|bus|cyc|all)" << std::endl + << " loglevel - change log level [loglevel level] (error|event|trace|debug)" << std::endl + << " quit - close connection" << std::endl + << " help - print this page"; break; default: diff --git a/src/baseloop.h b/src/baseloop.h index 1ecb7951..fdbaa914 100644 --- a/src/baseloop.h +++ b/src/baseloop.h @@ -65,12 +65,13 @@ private: WQueue m_queue; enum ClientCommand { - get, // get ebus data - set, // set ebus value - cyc, // fetch cycle data - dump, // change dump state - log, // change log level - help, // print commands + get, // get ebus data + set, // set ebus value + cyc, // fetch cycle data + dump, // change dump state + logarea, // change log area + loglevel, // change log level + help, // print commands notfound }; @@ -81,7 +82,8 @@ private: if (strcasecmp(item.c_str(), "set") == 0) return set; if (strcasecmp(item.c_str(), "cyc") == 0) return cyc; if (strcasecmp(item.c_str(), "dump") == 0) return dump; - if (strcasecmp(item.c_str(), "log") == 0) return log; + if (strcasecmp(item.c_str(), "logarea") == 0) return logarea; + if (strcasecmp(item.c_str(), "loglevel") == 0) return loglevel; if (strcasecmp(item.c_str(), "help") == 0) return help; return notfound; diff --git a/src/main.cpp b/src/main.cpp index b3d0e48f..d778b23b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,11 +49,11 @@ CYCData* cycdata; void define_args() { A.addItem("p_address", Appl::Param("FF"), "a", "address", - "\tebus device address (default: FF)", + "\tebus device address (FF)", Appl::type_string, Appl::opt_mandatory); A.addItem("p_device", Appl::Param("/dev/ttyUSB0"), "d", "device", - "\tebus device (serial or network) (default: /dev/ttyUSB0)", + "\tebus device (serial or network) (/dev/ttyUSB0)", Appl::type_string, Appl::opt_mandatory); A.addItem("p_nodevicecheck", Appl::Param(false), "n", "nodevicecheck", @@ -61,11 +61,11 @@ void define_args() Appl::type_bool, Appl::opt_none); A.addItem("p_retries", Appl::Param(2), "r", "retries", - "\tnumber retries send ebus command (default: 2)\n", + "\tnumber retries send ebus command (2)\n", Appl::type_int, Appl::opt_mandatory); A.addItem("p_ebusconfdir", Appl::Param("/etc/ebus-daemon"), "e", "ebusconfdir", - "directory for ebus configuration (default: /etc/ebus-daemon)\n", + "directory for ebus configuration (/etc/ebus-daemon)\n", Appl::type_string, Appl::opt_mandatory); A.addItem("p_foreground", Appl::Param(false), "f", "foreground", @@ -73,7 +73,7 @@ void define_args() Appl::type_bool, Appl::opt_none); A.addItem("p_port", Appl::Param(8888), "p", "port", - "\tlisten port (default: 8888)", + "\tlisten port (8888)", Appl::type_int, Appl::opt_mandatory); A.addItem("p_localhost", Appl::Param(false), "", "localhost", @@ -81,27 +81,27 @@ void define_args() Appl::type_bool, Appl::opt_none); A.addItem("p_logfile", Appl::Param("/var/log/ebus-daemon.log"), "l", "logfile", - "\tlog file name (default: /var/log/ebus-daemon.log)", + "\tlog file name (/var/log/ebus-daemon.log)", Appl::type_string, Appl::opt_mandatory); - A.addItem("p_logarea", Appl::Param(all), "", "logarea", - "\tlogging area (bas=1, net=2, bus=4, cyc=8, default: all=15)", - Appl::type_int, Appl::opt_mandatory); + A.addItem("p_logarea", Appl::Param("all"), "", "logarea", + "\tlogging area - bas|net|bus|cyc|all (all)", + Appl::type_string, Appl::opt_mandatory); - A.addItem("p_loglevel", Appl::Param(trace), "", "loglevel", - "\tlogging level (error=0, event=1, default: trace=2, debug=3)\n", - Appl::type_int, Appl::opt_mandatory); + A.addItem("p_loglevel", Appl::Param("trace"), "", "loglevel", + "\tlogging level - error|event|trace|debug (event)\n", + Appl::type_string, Appl::opt_mandatory); A.addItem("p_dump", Appl::Param(false), "D", "dump", "\tenable dump", Appl::type_bool, Appl::opt_none); A.addItem("p_dumpfile", Appl::Param("/tmp/ebus_dump.bin"), "", "dumpfile", - "\tdump file name (default: /tmp/ebus_dump.bin)", + "\tdump file name (/tmp/ebus_dump.bin)", Appl::type_string, Appl::opt_mandatory); A.addItem("p_dumpsize", Appl::Param(100), "", "dumpsize", - "\tmax size for dump file in kB (default: 100)\n", + "\tmax size for dump file in kB (100)\n", Appl::type_long, Appl::opt_mandatory); A.addItem("p_settings", Appl::Param(false), "", "settings", @@ -196,13 +196,13 @@ int main(int argc, char* argv[]) // make me Daemon if (A.getParam("p_foreground") == true) { - L += new LogConsole(A.getParam("p_logarea"), - static_cast(A.getParam("p_loglevel")), + L += new LogConsole(calcArea(A.getParam("p_logarea")), + calcLevel(A.getParam("p_loglevel")), "logConsole"); } else { D.run("/var/run/ebus-daemon.pid"); - L += new LogFile(A.getParam("p_logarea"), - static_cast(A.getParam("p_loglevel")), + L += new LogFile(calcArea(A.getParam("p_logarea")), + calcLevel(A.getParam("p_loglevel")), "logFile", A.getParam("p_logfile")); } diff --git a/utils/logger.cpp b/utils/logger.cpp index e0ed83db..02299ca5 100644 --- a/utils/logger.cpp +++ b/utils/logger.cpp @@ -19,6 +19,7 @@ #include "logger.h" #include +#include #include #include #include @@ -27,10 +28,45 @@ #include #include -static const char* AreaNames[Size_of_Area] = { "bas", "net", "bus" , "cyc" }; +static const char* AreaNames[Size_of_Area] = { "bas", "net", "bus", "cyc" }; static const char* LevelNames[Size_of_Level] = { "error", "event", "trace", "debug" }; -LogMessage::LogMessage(const Area area, const Level level, const std::string text, const Status status) +int calcArea(const std::string area) +{ + int m_area = 0; + + // prepare data + std::string token; + std::istringstream stream(area); + 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++) { + if (strcasecmp("all", it->c_str()) == 0) + return (pow(2, (int)Size_of_Area) - 1); + + if (strcasecmp(AreaNames[i], it->c_str()) == 0) + m_area += pow(2, i); + } + + return m_area; +} + +int calcLevel(const std::string level) +{ + int m_level = event; + for (int i = 0; i < Size_of_Level; i++) + if (strcasecmp(LevelNames[i], level.c_str()) == 0) + return i; + + return m_level; +} + + +LogMessage::LogMessage(const int area, const int level, const std::string text, const Status status) : m_area(area), m_level(level), m_text(text), m_status(status) { char time[24]; @@ -145,7 +181,7 @@ LogInstance& LogInstance::operator-= (const LogSink* sink) return (*this); } -void LogInstance::log(const Area area, const Level level, const std::string& data, ...) +void LogInstance::log(const int area, const int level, const std::string& data, ...) { if (m_running == true) { char* tmp; diff --git a/utils/logger.h b/utils/logger.h index c92cf49d..73593f4c 100644 --- a/utils/logger.h +++ b/utils/logger.h @@ -31,14 +31,18 @@ enum Area { bas=1, net=2, bus=4, cyc=8, all=15, Size_of_Area=4 }; enum Level { error=0, event, trace, debug, Size_of_Level }; +int calcArea(const std::string area); +int calcLevel(const std::string level); + + class LogMessage { - + public: enum Status { Run, End }; - - LogMessage(const Area area, const Level level, const std::string text, const Status status); - + + LogMessage(const int area, const int level, const std::string text, const Status status); + ~LogMessage() {} LogMessage(const LogMessage& src) @@ -48,16 +52,16 @@ public: void operator= (const LogMessage& src) { m_area = src.m_area; m_level = src.m_level; m_text = src.m_text; m_status = src.m_status; m_time = src.m_time; } - - Area getArea() const { return (m_area); } - Level getLevel() const { return(m_level); } + + int getArea() const { return (m_area); } + int getLevel() const { return(m_level); } std::string getText() const { return (m_text.c_str()); } Status getStatus() const { return (m_status); } std::string getTime() const { return (m_time.c_str()); } private: - Area m_area; - Level m_level; + int m_area; + int m_level; std::string m_text; Status m_status; std::string m_time; @@ -68,23 +72,23 @@ enum Type { Console, Logfile }; class LogSink : public Thread { - + public: - LogSink(const unsigned char areas, const Level level, const Type type, const char* name) + LogSink(const int areas, const int level, const Type type, const char* name) : m_areas(areas), m_level(level), m_type(type), m_name(name) {} virtual ~LogSink() {} - + void addMessage(const LogMessage& message); void* run(); - unsigned char getAreas() const { return (m_areas); } - void setAreas(const unsigned char areas) { m_areas = areas; } - - Level getLevel() const { return (m_level); } - void setLevel(const Level level) { m_level = level; } - + int getAreas() const { return (m_areas); } + void setAreas(const int& areas) { m_areas = areas; } + + int getLevel() const { return (m_level); } + void setLevel(const int& level) { m_level = level; } + Type getType() const { return (m_type); } const char* getName() const { return (m_name.c_str()); } @@ -92,20 +96,20 @@ protected: WQueue m_queue; private: - unsigned char m_areas; - Level m_level; + int m_areas; + int m_level; Type m_type; std::string m_name; - + virtual void write(const LogMessage& message) const = 0; }; class LogConsole : public LogSink { - + public: - LogConsole(const unsigned char areas, const Level level, const char* name) + LogConsole(const int areas, const int level, const char* name) : LogSink(areas, level, Console, name), m_instance(++m_numInstance) { this->start(name); } @@ -116,31 +120,31 @@ private: static int m_numInstance; void write(const LogMessage& message) const; - + }; class LogFile : public LogSink { - + public: - LogFile(const unsigned char areas, const Level level, const char* name, const char* filename) + LogFile(const int areas, const int level, const char* name, const char* filename) : LogSink(areas, level, Logfile, name), m_filename(filename), m_instance(++m_numInstance) { this->start(name); } ~LogFile() {} - + private: std::string m_filename; const int m_instance; static int m_numInstance; void write(const LogMessage& message) const; - + }; class LogInstance : public Thread { - + public: static LogInstance& Instance(); @@ -149,7 +153,7 @@ public: LogInstance& operator+= (LogSink* sink); LogInstance& operator-= (const LogSink* sink); - void log(const Area area, const Level level, const std::string& text, ...); + void log(const int area, const int level, const std::string& text, ...); int getNumberOfSinks() const { return(m_sinks.size()); } LogSink* getSink(const int Index) const { return(m_sinks[Index]); } @@ -158,11 +162,11 @@ public: void stop(); -private: +private: LogInstance() {} LogInstance(const LogInstance&); LogInstance& operator= (const LogInstance&); - + typedef std::vector sink_t; typedef std::vector::iterator sinkCI_t;