/* * LogManager.h * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along * with this program. If not, see . */ #ifndef MAD_CORE_LOGMANAGER_H_ #define MAD_CORE_LOGMANAGER_H_ #include "export.h" #include "Configurable.h" #include "Exception.h" #include "Logger.h" #include "String.h" #include #include #include #include namespace Mad { namespace Core { class Application; class ThreadManager; class MAD_CORE_EXPORT LogManager : public Configurable { private: friend class Application; friend class ThreadManager; typedef Logger::MessageCategory MessageCategory; typedef Logger::MessageLevel MessageLevel; struct Message { MessageCategory category; MessageLevel level; boost::posix_time::ptime timestamp; Core::String message; Core::String source; }; class ConsoleLogger : public Logger { private: // For long messages, writing to cerr is not atomic // -> lock cerr to prevent messages mixing up boost::mutex cerrMutex; protected: virtual void logMessage(MessageCategory category, MessageLevel, boost::posix_time::ptime timestamp, const Core::String &message, const Core::String &source); public: ConsoleLogger() {} void logMessageDirect(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const Core::String &message, const Core::String &source); }; Application *application; boost::shared_ptr consoleLogger; std::set > loggers; bool configured, running; boost::mutex queueMutex; boost::condition_variable queueCond; boost::mutex loggerMutex; std::queue messageQueue; void loggerThread(); void stopLoggerThread() { boost::lock_guard lock(queueMutex); running = false; queueCond.notify_one(); } LogManager(Application *application0); ~LogManager(); protected: virtual void configure(); public: static MessageLevel parseLevel(const String &str) throw (Exception); void log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const Core::String &message, const Core::String &source = Core::String()); void registerLogger(boost::shared_ptr logger) { boost::lock_guard lock(loggerMutex); loggers.insert(logger); } void unregisterLogger(boost::shared_ptr logger) { boost::lock_guard lock(loggerMutex); loggers.erase(logger); } }; } } #endif /* MAD_CORE_LOGMANAGER_H_ */