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) {
L.getSink(0)->setAreaMask(calcAreaMask(args[argPos + 1]));
L.setAreaMask(calcAreaMask(args[argPos + 1]));
result << "done";
break;
}
if (strcasecmp(args[argPos].c_str(), "LEVEL") == 0) {
L.getSink(0)->setLevel(calcLevel(args[argPos + 1]));
L.setLevel(calcLevel(args[argPos + 1]));
result << "done";
break;
}
+37 -28
View File
@@ -152,34 +152,50 @@ Logger& Logger::Instance()
Logger::~Logger()
{
while (m_sinks.empty() == false)
*this -= *(m_sinks.begin());
if (m_sink != NULL) {
delete m_sink;
m_sink = NULL;
}
}
Logger& Logger::operator+=(LogSink* sink)
{
sinkCI_t itEnd = m_sinks.end();
sinkCI_t it = find(m_sinks.begin(), itEnd, sink);
if (it == itEnd)
m_sinks.push_back(sink);
return (*this);
if (m_sink != NULL)
delete m_sink;
m_sink = sink;
return *this;
}
Logger& Logger::operator-=(const LogSink* sink)
{
sinkCI_t itEnd = m_sinks.end();
sinkCI_t it = find(m_sinks.begin(), itEnd, sink);
if (sink != NULL && sink == m_sink) {
delete m_sink;
m_sink = NULL;
}
return *this;
}
if (it == itEnd)
return (*this);
void Logger::setAreaMask(const int& areaMask)
{
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);
return (*this);
bool Logger::hasSink(const int area, const int level)
{
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, ...)
@@ -207,19 +223,12 @@ void Logger::log(const int area, const int level, const string& data, ...)
void Logger::handleMessage(LogMessage* message) {
if (message == NULL)
return;
sinkCI_t iter = m_sinks.begin();
for (; iter != m_sinks.end(); ++iter) {
if (*iter != 0) {
if ((((*iter)->getAreaMask() & (1 << message->getArea())) != 0
&& (*iter)->getLevel() >= message->getLevel())) {
(*iter)->addMessage(*message);
}
if (m_sink != NULL) {
if (((m_sink->getAreaMask() & (1 << message->getArea())) != 0
&& m_sink->getLevel() >= message->getLevel())) {
m_sink->addMessage(*message);
}
}
delete message;
}
+32 -25
View File
@@ -280,19 +280,39 @@ public:
virtual ~Logger();
/**
* @brief adds a logging sink and returns the reference to logger.
* @param sink the used logging sink.
* @return the reference to logger.
* @brief Add a @a LogSink.
* @param sink the used @a LogSink.
* @return the reference to this @a Logger.
*/
Logger& operator+=(LogSink* sink);
/**
* @brief removes a logging sink and returns the reference to logger.
* @param sink the used logging sink.
* @return the reference to logger.
* @brief Remove a @a LogSink and delete it.
* @param sink the used @a LogSink.
* @return the reference to this @a Logger.
*/
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.
* @param area the logging area of the message.
@@ -302,13 +322,6 @@ public:
*/
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
virtual bool start(const char* name);
@@ -322,12 +335,12 @@ protected:
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.
*/
Logger(const Logger&);
@@ -345,19 +358,13 @@ private:
*/
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. */
bool m_direct;
/** vector of available logging sinks */
sink_t m_sinks;
/** the used @a LogSink, or NULL. */
LogSink* m_sink;
/** queue for logging messages */
/** queue for logging messages. */
WQueue<LogMessage*> m_logQueue;
};