view 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
line wrap: on
line source
//  Copyright 2011, Ian Hobson.
//
//  This file is part of gpsynth.
//
//  gpsynth is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  gpsynth 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 General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with gpsynth in the file COPYING. 
//  If not, see http://www.gnu.org/licenses/.

// Simple threadsafe logger class

#pragma once

#include "boost/shared_ptr.hpp"
#include "boost/thread.hpp"

#include <iostream>
#include <queue>
#include <string>

namespace logger {

class Logger {
  std::ostream& output_stream_;
  std::queue<std::string> log_queue_;
  boost::mutex queue_mutex_;
  boost::shared_ptr<boost::thread> logging_thread_;
  boost::condition_variable queue_condition_;
  bool quit_thread_;
  
public:
  Logger();
  ~Logger();
  void LogMessage(const std::string& message);
  void Flush();
  
  static Logger& Singleton() {
    static Logger singleton;
    return singleton;
  }

private:
  void WorkerThread();
};

inline void Log(const std::string& message) {
  Logger::Singleton().LogMessage(message);
}

inline void Flush() {
  Logger::Singleton().Flush();
}

} // logger namespace