avoid expensive math operations (logn)

This commit is contained in:
john30
2014-12-14 12:35:56 +01:00
parent e507901d8f
commit f2eb31ea1f
4 changed files with 50 additions and 55 deletions
+1 -1
View File
@@ -450,7 +450,7 @@ string BaseLoop::decodeMessage(const string& data)
} }
if (strcasecmp(args[argPos].c_str(), "AREAS") == 0) { 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"; result << "done";
break; break;
} }
+2 -2
View File
@@ -159,12 +159,12 @@ int main(int argc, char* argv[])
// make me daemon // make me daemon
if (A.getOptVal<bool>("foreground") == true) { if (A.getOptVal<bool>("foreground") == true) {
L += new LogConsole(calcAreas(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");
} else { } else {
D.run("/var/run/ebusd.pid"); D.run("/var/run/ebusd.pid");
L += new LogFile(calcAreas(A.getOptVal<const char*>("logareas")), L += new LogFile(calcAreaMask(A.getOptVal<const char*>("logareas")),
calcLevel(A.getOptVal<const char*>("loglevel")), calcLevel(A.getOptVal<const char*>("loglevel")),
"logfile", A.getOptVal<const char*>("logfile")); "logfile", A.getOptVal<const char*>("logfile"));
} }
+15 -19
View File
@@ -23,7 +23,6 @@
#include <fstream> #include <fstream>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <cmath>
#include <ctime> #include <ctime>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
@@ -36,12 +35,9 @@ static const char* AreaNames[Size_of_Areas] = { "bas", "net", "bus", "upd" };
/** static char array with logging level names */ /** static char array with logging level names */
static const char* LevelNames[Size_of_Level] = { "error", "event", "trace", "debug" }; static const char* LevelNames[Size_of_Level] = { "error", "event", "trace", "debug" };
/** inline function of log2 */ int calcAreaMask(const string areas)
inline double Log2(double n) { return log(n) / log(2); }
int calcAreas(const string areas)
{ {
int m_areas = 0; int mask = 0;
// prepare data // prepare data
string token; string token;
@@ -54,13 +50,13 @@ int calcAreas(const string areas)
for (vector<string>::iterator it = cmd.begin() ; it != cmd.end(); ++it) for (vector<string>::iterator it = cmd.begin() ; it != cmd.end(); ++it)
for (int i = 0; i < Size_of_Areas; i++) { for (int i = 0; i < Size_of_Areas; i++) {
if (strcasecmp("ALL", it->c_str()) == 0) 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) 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) int calcLevel(const string level)
@@ -104,15 +100,15 @@ void LogSink::run()
{ {
while (1) { while (1) {
LogMessage* message = m_logQueue.remove(); 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; delete message;
while (m_logQueue.size() == true) {
LogMessage* message = m_logQueue.remove();
write(*message);
delete message;
}
return;
} }
return;
}
write(*message); write(*message);
delete message; delete message;
@@ -124,7 +120,7 @@ void LogSink::run()
void LogConsole::write(const LogMessage& message) const void LogConsole::write(const LogMessage& message) const
{ {
cout << message.getTime() << " [" cout << message.getTime() << " ["
<< AreaNames[(int)Log2(message.getArea())] << " " << AreaNames[(int)message.getArea()] << " "
<< LevelNames[message.getLevel()] << "] " << LevelNames[message.getLevel()] << "] "
<< message.getText() << endl; << message.getText() << endl;
} }
@@ -137,7 +133,7 @@ void LogFile::write(const LogMessage& message) const
if (file.is_open() == true) { if (file.is_open() == true) {
file << message.getTime() << " [" file << message.getTime() << " ["
<< AreaNames[(int)Log2(message.getArea())] << " " << AreaNames[(int)message.getArea()] << " "
<< LevelNames[message.getLevel()] << "] " << LevelNames[message.getLevel()] << "] "
<< message.getText() << endl; << message.getText() << endl;
file.close(); file.close();
@@ -214,7 +210,7 @@ void Logger::run()
for (; iter != m_sinks.end(); ++iter) { for (; iter != m_sinks.end(); ++iter) {
if (*iter != 0) { if (*iter != 0) {
if ((((*iter)->getAreas() & message->getArea()) != 0 if ((((*iter)->getAreaMask() & (1 << message->getArea())) != 0
&& (*iter)->getLevel() >= message->getLevel()) && (*iter)->getLevel() >= message->getLevel())
&& message->isRunning() == true) { && message->isRunning() == true) {
(*iter)->addMessage(*message); (*iter)->addMessage(*message);
+32 -33
View File
@@ -34,11 +34,10 @@ using namespace std;
/** available types for all subsystems */ /** available types for all subsystems */
enum AreasType { enum AreasType {
bas=1, /*!< basis */ bas=0, /*!< basis */
net=2, /*!< network */ net, /*!< network */
bus=4, /*!< ebus */ bus, /*!< ebus */
upd=8, /*!< updates found while listening to the bus */ upd, /*!< updates found while listening to the bus */
all=15, /*!< type for all subsystems */
Size_of_Areas=4 /*!< number of possible areas */ Size_of_Areas=4 /*!< number of possible areas */
}; };
@@ -51,8 +50,8 @@ enum LevelType {
Size_of_Level /*!< number of possible levels */ Size_of_Level /*!< number of possible levels */
}; };
/** global function to get calculate logging areas */ /** global function to get the mask of logging areas */
int calcAreas(const string areas); int calcAreaMask(const string areas);
/** global function to get calculate logging level */ /** global function to get calculate logging level */
int calcLevel(const string level); int calcLevel(const string level);
@@ -77,19 +76,19 @@ public:
* @brief get the logging area. * @brief get the logging area.
* @return the logging area. * @return the logging area.
*/ */
int getArea() const { return (m_area); } int getArea() const { return m_area; }
/** /**
* @brief get the logging level. * @brief get the logging level.
* @return the logging level. * @return the logging level.
*/ */
int getLevel() const { return(m_level); } int getLevel() const { return m_level; }
/** /**
* @brief get the logging text. * @brief get the logging text.
* @return 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. * @brief status of logging subsystem.
@@ -101,22 +100,22 @@ public:
* @brief get the logging timestamp. * @brief get the logging timestamp.
* @return the logging timestamp. * @return the logging timestamp.
*/ */
string getTime() const { return (m_time.c_str()); } string getTime() const { return m_time.c_str(); }
private: private:
/** the logging area */ /** the logging area. */
int m_area; int m_area;
/** the logging level */ /** the logging level. */
int m_level; int m_level;
/** the logging message */ /** the logging message. */
string m_text; string m_text;
/** true if this instance is running */ /** true if this instance is running. */
bool m_running; bool m_running;
/** the logging timestamp */ /** the logging timestamp. */
string m_time; string m_time;
}; };
@@ -130,10 +129,10 @@ class LogSink : public Thread
public: public:
/** /**
* @brief creates a virtual logging sink. * @brief creates a virtual logging sink.
* @param areas the logging areas. * @param areas the logging area mask.
* @param level the logging level. * @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. * @brief adds the logging message to internal message queue.
@@ -147,16 +146,16 @@ public:
void run(); void run();
/** /**
* @brief get the logging areas. * @brief get the logging area mask.
* @return the logging areas. * @return the logging area mask.
*/ */
int getAreas() const { return (m_areas); } int getAreaMask() const { return m_areaMask; }
/** /**
* @brief set the logging areas. * @brief set the logging area mask.
* @param areas the logging areas. * @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. * @brief get the logging level.
@@ -175,10 +174,10 @@ protected:
WQueue<LogMessage*> m_logQueue; WQueue<LogMessage*> m_logQueue;
private: private:
/** the logging areas */ /** the logging area mask. */
int m_areas; int m_areaMask;
/** the logging level */ /** the logging level. */
int m_level; int m_level;
/** /**
@@ -198,12 +197,12 @@ class LogConsole : public LogSink
public: public:
/** /**
* @brief creates a console logging sink. * @brief creates a console logging sink.
* @param areas the logging areas. * @param areas the logging area mask.
* @param level the logging level. * @param level the logging level.
* @param name the thread name for logging sink. * @param name the thread name for logging sink.
*/ */
LogConsole(const int areas, const int level, const char* name) LogConsole(const int areaMask, const int level, const char* name)
: LogSink(areas, level) { this->start(name); } : LogSink(areaMask, level) { this->start(name); }
private: private:
/** /**
@@ -223,13 +222,13 @@ class LogFile : public LogSink
public: public:
/** /**
* @brief creates a log file logging sink. * @brief creates a log file logging sink.
* @param areas the logging areas. * @param areas the logging area mask.
* @param level the logging level. * @param level the logging level.
* @param name the thread name for logging sink. * @param name the thread name for logging sink.
* @param file the log file. * @param file the log file.
*/ */
LogFile(const int areas, const int level, const char* name, const char* file) LogFile(const int areaMask, const int level, const char* name, const char* file)
: LogSink(areas, level), m_file(file) { this->start(name); } : LogSink(areaMask, level), m_file(file) { this->start(name); }
private: private:
/** the logging file */ /** the logging file */