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