From f68e0d676377955f533d70433c6d86779dfa5c34 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 28 Dec 2014 10:40:44 +0100 Subject: [PATCH] only support a single LogSink in Logger, added hasSink() and setAreaMask() and setLevel() to Logger --- src/ebusd/baseloop.cpp | 4 +-- src/lib/utils/logger.cpp | 65 +++++++++++++++++++++++----------------- src/lib/utils/logger.h | 57 +++++++++++++++++++---------------- 3 files changed, 71 insertions(+), 55 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 8da7f7c2..2a9584c7 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -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; } diff --git a/src/lib/utils/logger.cpp b/src/lib/utils/logger.cpp index 807671fb..47878986 100644 --- a/src/lib/utils/logger.cpp +++ b/src/lib/utils/logger.cpp @@ -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; } diff --git a/src/lib/utils/logger.h b/src/lib/utils/logger.h index 813ffa78..a4c5e709 100644 --- a/src/lib/utils/logger.h +++ b/src/lib/utils/logger.h @@ -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 sink_t; - - /** typedefs for a vector of type LogSink* iterator */ - typedef vector::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 m_logQueue; };