Merge pull request #23 from john30/master

avoid expensive math operations (logn)
This commit is contained in:
Roland Jax
2014-12-14 13:15:19 +01:00
5 changed files with 53 additions and 56 deletions
+3 -1
View File
@@ -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;
@@ -450,7 +452,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;
}
+2 -2
View File
@@ -159,12 +159,12 @@ int main(int argc, char* argv[])
// make me daemon
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")),
"logconsole");
} else {
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")),
"logfile", A.getOptVal<const char*>("logfile"));
}
+1 -1
View File
@@ -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;
}
+15 -19
View File
@@ -23,7 +23,6 @@
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <sys/time.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 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<string>::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);
+32 -33
View File
@@ -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<LogMessage*> 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 */