annotate src/logger.hpp @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
rev   line source
ian@0 1 // Copyright 2011, Ian Hobson.
ian@0 2 //
ian@0 3 // This file is part of gpsynth.
ian@0 4 //
ian@0 5 // gpsynth is free software: you can redistribute it and/or modify
ian@0 6 // it under the terms of the GNU General Public License as published by
ian@0 7 // the Free Software Foundation, either version 3 of the License, or
ian@0 8 // (at your option) any later version.
ian@0 9 //
ian@0 10 // gpsynth is distributed in the hope that it will be useful,
ian@0 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
ian@0 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ian@0 13 // GNU General Public License for more details.
ian@0 14 //
ian@0 15 // You should have received a copy of the GNU General Public License
ian@0 16 // along with gpsynth in the file COPYING.
ian@0 17 // If not, see http://www.gnu.org/licenses/.
ian@0 18
ian@0 19 // Simple threadsafe logger class
ian@0 20
ian@0 21 #pragma once
ian@0 22
ian@0 23 #include "boost/shared_ptr.hpp"
ian@0 24 #include "boost/thread.hpp"
ian@0 25
ian@0 26 #include <iostream>
ian@0 27 #include <queue>
ian@0 28 #include <string>
ian@0 29
ian@0 30 namespace logger {
ian@0 31
ian@0 32 class Logger {
ian@0 33 std::ostream& output_stream_;
ian@0 34 std::queue<std::string> log_queue_;
ian@0 35 boost::mutex queue_mutex_;
ian@0 36 boost::shared_ptr<boost::thread> logging_thread_;
ian@0 37 boost::condition_variable queue_condition_;
ian@0 38 bool quit_thread_;
ian@0 39
ian@0 40 public:
ian@0 41 Logger();
ian@0 42 ~Logger();
ian@0 43 void LogMessage(const std::string& message);
ian@0 44 void Flush();
ian@0 45
ian@0 46 static Logger& Singleton() {
ian@0 47 static Logger singleton;
ian@0 48 return singleton;
ian@0 49 }
ian@0 50
ian@0 51 private:
ian@0 52 void WorkerThread();
ian@0 53 };
ian@0 54
ian@0 55 inline void Log(const std::string& message) {
ian@0 56 Logger::Singleton().LogMessage(message);
ian@0 57 }
ian@0 58
ian@0 59 inline void Flush() {
ian@0 60 Logger::Singleton().Flush();
ian@0 61 }
ian@0 62
ian@0 63 } // logger namespace