#ifndef DATAQUEUE_LOG_HH #define DATAQUEUE_LOG_HH #include #include #include #include /** * \author cpellegrino */ namespace Log { enum LogLevel {cDebug, cWarning, cError, cNotice, cStatus}; class Logger { std::ostringstream m_stream; LogLevel m_level; public: Logger(LogLevel level) : m_level(level) {} std::ostringstream& stream() { return m_stream; } ~Logger(); }; class Counter { typedef std::map< std::string, unsigned int> container_t; container_t m_counters; mutable boost::mutex m_mutex; Counter() {} Counter(Counter const&); Counter& operator =(Counter const&); public: static Counter& get() { static Counter c; return c; } void add(std::string const& tag) { boost::mutex::scoped_lock lock(m_mutex); container_t::iterator const it = m_counters.find(tag); if (it != m_counters.end()) { ++it->second; } else { m_counters.insert(std::make_pair(tag, 1)); } } void reset() { boost::mutex::scoped_lock lock(m_mutex); m_counters.clear(); } void reset(std::string const& tag) { boost::mutex::scoped_lock lock(m_mutex); container_t::iterator const it = m_counters.find(tag); if (it != m_counters.end()) { it->second = 0; } } friend std::ostream& operator <<(std::ostream& stream, Counter const& c) { boost::mutex::scoped_lock lock(c.m_mutex); for ( container_t::const_iterator it = c.m_counters.begin(), et = c.m_counters.end(); it != et; ++it ) { stream << it->first << ": " << it->second << ", "; } return stream; } }; } // ns Log #define LOG_DEBUG Log::Logger(Log::cDebug ).stream() #define LOG_WARNING Log::Logger(Log::cWarning).stream() #define LOG_ERROR Log::Logger(Log::cError ).stream() #define LOG_NOTICE Log::Logger(Log::cNotice ).stream() #define LOG_STATUS Log::Logger(Log::cDebug ).stream() #endif // DATAQUEUE_LOG_HH