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