logger: logarea and loglevel reimplemented; daemon socket option logarea added.

This commit is contained in:
Roland Jax
2014-05-17 13:48:32 +02:00
parent 0821a7d31b
commit 0ed9795d99
5 changed files with 127 additions and 77 deletions
+39 -3
View File
@@ -19,6 +19,7 @@
#include "logger.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <cstring>
@@ -27,10 +28,45 @@
#include <sys/time.h>
#include <unistd.h>
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<std::string> cmd;
while (std::getline(stream, token, ',') != 0)
cmd.push_back(token);
for (std::vector<std::string>::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;
+36 -32
View File
@@ -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<LogMessage*> 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<LogSink*> sink_t;
typedef std::vector<LogSink*>::iterator sinkCI_t;