From f2eb31ea1fe3601ea22630622a31ec11530fa5d8 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 14 Dec 2014 12:35:56 +0100 Subject: [PATCH 1/3] avoid expensive math operations (logn) --- src/ebusd/baseloop.cpp | 2 +- src/ebusd/ebusd.cpp | 4 +-- src/lib/utils/logger.cpp | 34 ++++++++++----------- src/lib/utils/logger.h | 65 ++++++++++++++++++++-------------------- 4 files changed, 50 insertions(+), 55 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 2b65b8fd..b42ce2da 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -450,7 +450,7 @@ string BaseLoop::decodeMessage(const string& data) } if (strcasecmp(args[argPos].c_str(), "AREAS") == 0) { - L.getSink(0)->setAreas(calcAreas(args[argPos + 1])); + L.getSink(0)->setAreaMask(calcAreaMask(args[argPos + 1])); result << "done"; break; } diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index 0fed09f6..b299dd40 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -159,12 +159,12 @@ int main(int argc, char* argv[]) // make me daemon if (A.getOptVal("foreground") == true) { - L += new LogConsole(calcAreas(A.getOptVal("logareas")), + L += new LogConsole(calcAreaMask(A.getOptVal("logareas")), calcLevel(A.getOptVal("loglevel")), "logconsole"); } else { D.run("/var/run/ebusd.pid"); - L += new LogFile(calcAreas(A.getOptVal("logareas")), + L += new LogFile(calcAreaMask(A.getOptVal("logareas")), calcLevel(A.getOptVal("loglevel")), "logfile", A.getOptVal("logfile")); } diff --git a/src/lib/utils/logger.cpp b/src/lib/utils/logger.cpp index 9262bcc0..e9972b17 100644 --- a/src/lib/utils/logger.cpp +++ b/src/lib/utils/logger.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -36,12 +35,9 @@ static const char* AreaNames[Size_of_Areas] = { "bas", "net", "bus", "upd" }; /** static char array with logging level names */ static const char* LevelNames[Size_of_Level] = { "error", "event", "trace", "debug" }; -/** inline function of log2 */ -inline double Log2(double n) { return log(n) / log(2); } - -int calcAreas(const string areas) +int calcAreaMask(const string areas) { - int m_areas = 0; + int mask = 0; // prepare data string token; @@ -54,13 +50,13 @@ int calcAreas(const string areas) for (vector::iterator it = cmd.begin() ; it != cmd.end(); ++it) for (int i = 0; i < Size_of_Areas; i++) { if (strcasecmp("ALL", it->c_str()) == 0) - return (pow(2, (int)Size_of_Areas) - 1); + return (1 << (int)Size_of_Areas) - 1; if (strcasecmp(AreaNames[i], it->c_str()) == 0) - m_areas += pow(2, i); + mask |= 1 << i; } - return m_areas; + return mask; } int calcLevel(const string level) @@ -104,15 +100,15 @@ void LogSink::run() { while (1) { LogMessage* message = m_logQueue.remove(); - if (message->isRunning() == false) { + if (message->isRunning() == false) { + delete message; + while (m_logQueue.size() == true) { + LogMessage* message = m_logQueue.remove(); + write(*message); delete message; - while (m_logQueue.size() == true) { - LogMessage* message = m_logQueue.remove(); - write(*message); - delete message; - } - return; } + return; + } write(*message); delete message; @@ -124,7 +120,7 @@ void LogSink::run() void LogConsole::write(const LogMessage& message) const { cout << message.getTime() << " [" - << AreaNames[(int)Log2(message.getArea())] << " " + << AreaNames[(int)message.getArea()] << " " << LevelNames[message.getLevel()] << "] " << message.getText() << endl; } @@ -137,7 +133,7 @@ void LogFile::write(const LogMessage& message) const if (file.is_open() == true) { file << message.getTime() << " [" - << AreaNames[(int)Log2(message.getArea())] << " " + << AreaNames[(int)message.getArea()] << " " << LevelNames[message.getLevel()] << "] " << message.getText() << endl; file.close(); @@ -214,7 +210,7 @@ void Logger::run() for (; iter != m_sinks.end(); ++iter) { if (*iter != 0) { - if ((((*iter)->getAreas() & message->getArea()) != 0 + if ((((*iter)->getAreaMask() & (1 << message->getArea())) != 0 && (*iter)->getLevel() >= message->getLevel()) && message->isRunning() == true) { (*iter)->addMessage(*message); diff --git a/src/lib/utils/logger.h b/src/lib/utils/logger.h index e1d07795..18789e65 100644 --- a/src/lib/utils/logger.h +++ b/src/lib/utils/logger.h @@ -34,11 +34,10 @@ using namespace std; /** available types for all subsystems */ enum AreasType { - bas=1, /*!< basis */ - net=2, /*!< network */ - bus=4, /*!< ebus */ - upd=8, /*!< updates found while listening to the bus */ - all=15, /*!< type for all subsystems */ + bas=0, /*!< basis */ + net, /*!< network */ + bus, /*!< ebus */ + upd, /*!< updates found while listening to the bus */ Size_of_Areas=4 /*!< number of possible areas */ }; @@ -51,8 +50,8 @@ enum LevelType { Size_of_Level /*!< number of possible levels */ }; -/** global function to get calculate logging areas */ -int calcAreas(const string areas); +/** global function to get the mask of logging areas */ +int calcAreaMask(const string areas); /** global function to get calculate logging level */ int calcLevel(const string level); @@ -77,19 +76,19 @@ public: * @brief get the logging area. * @return the logging area. */ - int getArea() const { return (m_area); } + int getArea() const { return m_area; } /** * @brief get the logging level. * @return the logging level. */ - int getLevel() const { return(m_level); } + int getLevel() const { return m_level; } /** * @brief get the logging text. * @return the logging text. */ - string getText() const { return (m_text.c_str()); } + string getText() const { return m_text.c_str(); } /** * @brief status of logging subsystem. @@ -101,22 +100,22 @@ public: * @brief get the logging timestamp. * @return the logging timestamp. */ - string getTime() const { return (m_time.c_str()); } + string getTime() const { return m_time.c_str(); } private: - /** the logging area */ + /** the logging area. */ int m_area; - /** the logging level */ + /** the logging level. */ int m_level; - /** the logging message */ + /** the logging message. */ string m_text; - /** true if this instance is running */ + /** true if this instance is running. */ bool m_running; - /** the logging timestamp */ + /** the logging timestamp. */ string m_time; }; @@ -130,10 +129,10 @@ class LogSink : public Thread public: /** * @brief creates a virtual logging sink. - * @param areas the logging areas. + * @param areas the logging area mask. * @param level the logging level. */ - LogSink(const int areas, const int level) : m_areas(areas), m_level(level) {} + LogSink(const int areaMask, const int level) : m_areaMask(areaMask), m_level(level) {} /** * @brief adds the logging message to internal message queue. @@ -147,16 +146,16 @@ public: void run(); /** - * @brief get the logging areas. - * @return the logging areas. + * @brief get the logging area mask. + * @return the logging area mask. */ - int getAreas() const { return (m_areas); } + int getAreaMask() const { return m_areaMask; } /** - * @brief set the logging areas. - * @param areas the logging areas. + * @brief set the logging area mask. + * @param areas the logging area mask. */ - void setAreas(const int& areas) { m_areas = areas; } + void setAreaMask(const int& areaMask) { m_areaMask = areaMask; } /** * @brief get the logging level. @@ -175,10 +174,10 @@ protected: WQueue m_logQueue; private: - /** the logging areas */ - int m_areas; + /** the logging area mask. */ + int m_areaMask; - /** the logging level */ + /** the logging level. */ int m_level; /** @@ -198,12 +197,12 @@ class LogConsole : public LogSink public: /** * @brief creates a console logging sink. - * @param areas the logging areas. + * @param areas the logging area mask. * @param level the logging level. * @param name the thread name for logging sink. */ - LogConsole(const int areas, const int level, const char* name) - : LogSink(areas, level) { this->start(name); } + LogConsole(const int areaMask, const int level, const char* name) + : LogSink(areaMask, level) { this->start(name); } private: /** @@ -223,13 +222,13 @@ class LogFile : public LogSink public: /** * @brief creates a log file logging sink. - * @param areas the logging areas. + * @param areas the logging area mask. * @param level the logging level. * @param name the thread name for logging sink. * @param file the log file. */ - LogFile(const int areas, const int level, const char* name, const char* file) - : LogSink(areas, level), m_file(file) { this->start(name); } + LogFile(const int areaMask, const int level, const char* name, const char* file) + : LogSink(areaMask, level), m_file(file) { this->start(name); } private: /** the logging file */ From caa0ddc027382d7df2d474e783a954fd2e94841e Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 14 Dec 2014 12:44:57 +0100 Subject: [PATCH 2/3] fix for new empty result code --- src/lib/ebus/message.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/ebus/message.cpp b/src/lib/ebus/message.cpp index 363dd133..48573b7d 100644 --- a/src/lib/ebus/message.cpp +++ b/src/lib/ebus/message.cpp @@ -305,7 +305,7 @@ result_t Message::decode(const PartType partType, SymbolString& data, int startPos = output.str().length(); result_t result = m_data->read(partType, data, offset, output, leadingSeparator, verbose, filterName, separator); time(&m_lastUpdateTime); - if (result != RESULT_OK) { + if (result < RESULT_OK) { m_lastValue.clear(); return result; } From 848a043f8b9c583279ab6d37c103b80650319395 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 14 Dec 2014 12:45:21 +0100 Subject: [PATCH 3/3] force refresh if single field is requested --- src/ebusd/baseloop.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index b42ce2da..a79117c8 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -256,6 +256,8 @@ string BaseLoop::decodeMessage(const string& data) result << "usage: 'read [-v] [-f] [-m seconds] [class] cmd' or 'read [-v] [-f] [-m seconds] class cmd sub'"; break; } + if (args.size() == argPos + 3) + maxAge = 0; // force refresh to filter single field time_t now;