Mercurial > hg > gpsynth
comparison src/logger.cpp @ 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 #include "logger.hpp" | |
20 | |
21 namespace logger { | |
22 | |
23 Logger::Logger() | |
24 : output_stream_(std::cout), | |
25 quit_thread_(false) { | |
26 logging_thread_.reset(new boost::thread(&Logger::WorkerThread, this)); | |
27 } | |
28 | |
29 Logger::~Logger() { | |
30 quit_thread_ = true; | |
31 queue_condition_.notify_one(); | |
32 logging_thread_->join(); | |
33 } | |
34 | |
35 void Logger::LogMessage(const std::string& message) { | |
36 boost::mutex::scoped_lock lock(queue_mutex_); | |
37 log_queue_.push(message); | |
38 queue_condition_.notify_one(); | |
39 } | |
40 | |
41 void Logger::WorkerThread() { | |
42 while (!quit_thread_) { | |
43 boost::mutex::scoped_lock lock(queue_mutex_); | |
44 while (log_queue_.empty() && !quit_thread_) { | |
45 queue_condition_.wait(lock); | |
46 } | |
47 while (!log_queue_.empty()) { | |
48 output_stream_ << log_queue_.front(); | |
49 log_queue_.pop(); | |
50 } | |
51 output_stream_ << std::flush; | |
52 queue_condition_.notify_one(); | |
53 } | |
54 } | |
55 | |
56 void Logger::Flush() { | |
57 // wait for the worker thread to empty the work queue | |
58 boost::mutex::scoped_lock lock(queue_mutex_); | |
59 while (!log_queue_.empty()) { | |
60 queue_condition_.wait(lock); | |
61 } | |
62 } | |
63 | |
64 } // logger namespace |