only support a single LogSink in Logger, added hasSink() and setAreaMask() and setLevel() to Logger

This commit is contained in:
john30
2014-12-28 10:40:44 +01:00
parent 5fcfb27d29
commit f68e0d6763
3 changed files with 71 additions and 55 deletions
+2 -2
View File
@@ -460,13 +460,13 @@ 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)->setAreaMask(calcAreaMask(args[argPos + 1])); L.setAreaMask(calcAreaMask(args[argPos + 1]));
result << "done"; result << "done";
break; break;
} }
if (strcasecmp(args[argPos].c_str(), "LEVEL") == 0) { if (strcasecmp(args[argPos].c_str(), "LEVEL") == 0) {
L.getSink(0)->setLevel(calcLevel(args[argPos + 1])); L.setLevel(calcLevel(args[argPos + 1]));
result << "done"; result << "done";
break; break;
} }
+37 -28
View File
@@ -152,34 +152,50 @@ Logger& Logger::Instance()
Logger::~Logger() Logger::~Logger()
{ {
while (m_sinks.empty() == false) if (m_sink != NULL) {
*this -= *(m_sinks.begin()); delete m_sink;
m_sink = NULL;
}
} }
Logger& Logger::operator+=(LogSink* sink) Logger& Logger::operator+=(LogSink* sink)
{ {
sinkCI_t itEnd = m_sinks.end(); if (m_sink != NULL)
sinkCI_t it = find(m_sinks.begin(), itEnd, sink); delete m_sink;
m_sink = sink;
if (it == itEnd) return *this;
m_sinks.push_back(sink);
return (*this);
} }
Logger& Logger::operator-=(const LogSink* sink) Logger& Logger::operator-=(const LogSink* sink)
{ {
sinkCI_t itEnd = m_sinks.end(); if (sink != NULL && sink == m_sink) {
sinkCI_t it = find(m_sinks.begin(), itEnd, sink); delete m_sink;
m_sink = NULL;
}
return *this;
}
if (it == itEnd) void Logger::setAreaMask(const int& areaMask)
return (*this); {
if (m_sink != NULL)
m_sink->setAreaMask(areaMask);
}
m_sinks.erase(it); void Logger::setLevel(const int& level)
{
if (m_sink != NULL)
m_sink->setLevel(level);
}
delete (sink); bool Logger::hasSink(const int area, const int level)
{
return (*this); if (m_sink != NULL) {
if (((m_sink->getAreaMask() & (1 << area)) != 0
&& m_sink->getLevel() >= level)) {
return true;
}
}
return false;
} }
void Logger::log(const int area, const int level, const string& data, ...) void Logger::log(const int area, const int level, const string& data, ...)
@@ -207,19 +223,12 @@ void Logger::log(const int area, const int level, const string& data, ...)
void Logger::handleMessage(LogMessage* message) { void Logger::handleMessage(LogMessage* message) {
if (message == NULL) if (message == NULL)
return; return;
if (m_sink != NULL) {
sinkCI_t iter = m_sinks.begin(); if (((m_sink->getAreaMask() & (1 << message->getArea())) != 0
&& m_sink->getLevel() >= message->getLevel())) {
for (; iter != m_sinks.end(); ++iter) { m_sink->addMessage(*message);
if (*iter != 0) {
if ((((*iter)->getAreaMask() & (1 << message->getArea())) != 0
&& (*iter)->getLevel() >= message->getLevel())) {
(*iter)->addMessage(*message);
}
} }
} }
delete message; delete message;
} }
+32 -25
View File
@@ -280,19 +280,39 @@ public:
virtual ~Logger(); virtual ~Logger();
/** /**
* @brief adds a logging sink and returns the reference to logger. * @brief Add a @a LogSink.
* @param sink the used logging sink. * @param sink the used @a LogSink.
* @return the reference to logger. * @return the reference to this @a Logger.
*/ */
Logger& operator+=(LogSink* sink); Logger& operator+=(LogSink* sink);
/** /**
* @brief removes a logging sink and returns the reference to logger. * @brief Remove a @a LogSink and delete it.
* @param sink the used logging sink. * @param sink the used @a LogSink.
* @return the reference to logger. * @return the reference to this @a Logger.
*/ */
Logger& operator-=(const LogSink* sink); Logger& operator-=(const LogSink* sink);
/**
* @brief Set the logging area mask on all @a LogSink instances.
* @param areaMask the logging area mask.
*/
void setAreaMask(const int& areaMask);
/**
* @brief Set the logging level on all @a LogSink instances.
* @param level the logging level.
*/
void setLevel(const int& level);
/**
* @brief Return whether a @a LogSink is available that will produce output for the specified area and level.
* @param area the logging area of the message.
* @param level the logging level of the message.
* @return whether a @a LogSink is available that will produce output for the specified area and level.
*/
bool hasSink(const int area, const int level);
/** /**
* @brief creates a logging message and add them to internal message queue. * @brief creates a logging message and add them to internal message queue.
* @param area the logging area of the message. * @param area the logging area of the message.
@@ -302,13 +322,6 @@ public:
*/ */
void log(const int area, const int level, const string& text, ...); void log(const int area, const int level, const string& text, ...);
/**
* @brief returns the sink at the specified index.
* @param index the index of the sink to return.
* @return the sink at the specified index.
*/
LogSink* getSink(const int index) const { return(m_sinks[index]); }
//@copydoc //@copydoc
virtual bool start(const char* name); virtual bool start(const char* name);
@@ -322,12 +335,12 @@ protected:
private: private:
/** /**
* @brief private construtor. * @brief private constructor.
*/ */
Logger() : m_direct(true) {} Logger() : m_direct(true), m_sink(NULL) {}
/** /**
* @brief private copy construtor. * @brief private copy constructor.
* @param reference to an instance. * @param reference to an instance.
*/ */
Logger(const Logger&); Logger(const Logger&);
@@ -345,19 +358,13 @@ private:
*/ */
void handleMessage(LogMessage* message); void handleMessage(LogMessage* message);
/** typedefs for a vector of type LogSink* */
typedef vector<LogSink*> sink_t;
/** typedefs for a vector of type LogSink* iterator */
typedef vector<LogSink*>::iterator sinkCI_t;
/** true to directly log to all sinks, false to buffer via @a m_logQueue. */ /** true to directly log to all sinks, false to buffer via @a m_logQueue. */
bool m_direct; bool m_direct;
/** vector of available logging sinks */ /** the used @a LogSink, or NULL. */
sink_t m_sinks; LogSink* m_sink;
/** queue for logging messages */ /** queue for logging messages. */
WQueue<LogMessage*> m_logQueue; WQueue<LogMessage*> m_logQueue;
}; };