ian@0: // Copyright 2011, Ian Hobson. ian@0: // ian@0: // This file is part of gpsynth. ian@0: // ian@0: // gpsynth is free software: you can redistribute it and/or modify ian@0: // it under the terms of the GNU General Public License as published by ian@0: // the Free Software Foundation, either version 3 of the License, or ian@0: // (at your option) any later version. ian@0: // ian@0: // gpsynth is distributed in the hope that it will be useful, ian@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of ian@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ian@0: // GNU General Public License for more details. ian@0: // ian@0: // You should have received a copy of the GNU General Public License ian@0: // along with gpsynth in the file COPYING. ian@0: // If not, see http://www.gnu.org/licenses/. ian@0: ian@0: // Simple threadsafe logger class ian@0: ian@0: #pragma once ian@0: ian@0: #include "boost/shared_ptr.hpp" ian@0: #include "boost/thread.hpp" ian@0: ian@0: #include ian@0: #include ian@0: #include ian@0: ian@0: namespace logger { ian@0: ian@0: class Logger { ian@0: std::ostream& output_stream_; ian@0: std::queue log_queue_; ian@0: boost::mutex queue_mutex_; ian@0: boost::shared_ptr logging_thread_; ian@0: boost::condition_variable queue_condition_; ian@0: bool quit_thread_; ian@0: ian@0: public: ian@0: Logger(); ian@0: ~Logger(); ian@0: void LogMessage(const std::string& message); ian@0: void Flush(); ian@0: ian@0: static Logger& Singleton() { ian@0: static Logger singleton; ian@0: return singleton; ian@0: } ian@0: ian@0: private: ian@0: void WorkerThread(); ian@0: }; ian@0: ian@0: inline void Log(const std::string& message) { ian@0: Logger::Singleton().LogMessage(message); ian@0: } ian@0: ian@0: inline void Flush() { ian@0: Logger::Singleton().Flush(); ian@0: } ian@0: ian@0: } // logger namespace