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: #include "logger.hpp" ian@0: ian@0: namespace logger { ian@0: ian@0: Logger::Logger() ian@0: : output_stream_(std::cout), ian@0: quit_thread_(false) { ian@0: logging_thread_.reset(new boost::thread(&Logger::WorkerThread, this)); ian@0: } ian@0: ian@0: Logger::~Logger() { ian@0: quit_thread_ = true; ian@0: queue_condition_.notify_one(); ian@0: logging_thread_->join(); ian@0: } ian@0: ian@0: void Logger::LogMessage(const std::string& message) { ian@0: boost::mutex::scoped_lock lock(queue_mutex_); ian@0: log_queue_.push(message); ian@0: queue_condition_.notify_one(); ian@0: } ian@0: ian@0: void Logger::WorkerThread() { ian@0: while (!quit_thread_) { ian@0: boost::mutex::scoped_lock lock(queue_mutex_); ian@0: while (log_queue_.empty() && !quit_thread_) { ian@0: queue_condition_.wait(lock); ian@0: } ian@0: while (!log_queue_.empty()) { ian@0: output_stream_ << log_queue_.front(); ian@0: log_queue_.pop(); ian@0: } ian@0: output_stream_ << std::flush; ian@0: queue_condition_.notify_one(); ian@0: } ian@0: } ian@0: ian@0: void Logger::Flush() { ian@0: // wait for the worker thread to empty the work queue ian@0: boost::mutex::scoped_lock lock(queue_mutex_); ian@0: while (!log_queue_.empty()) { ian@0: queue_condition_.wait(lock); ian@0: } ian@0: } ian@0: ian@0: } // logger namespace