aboutsummaryrefslogtreecommitdiff
path: root/src/utility/logger.h
blob: 0dd88a6299ba602b03e3df351bf586f116483a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef CHANGE_SRC_LOGGER_H_
#define CHANGE_SRC_LOGGER_H_

#include <mutex>

#include <ext/stdio_filebuf.h>

#include <boost/process.hpp>

namespace utility {

class Logger {
	public:
		Logger(const int target_fd):
			buffer_(target_fd, std::ios::out),
			stream_(&this->buffer_) { }

		void forward(boost::process::pistream&);

		template <typename... Arguments>
		void append(Arguments&&... args) {
			std::lock_guard<std::mutex> guard(this->write_mutex_);

			this->append_to_stream(std::forward<Arguments>(args)...);
		}

	private:
		__gnu_cxx::stdio_filebuf<char> buffer_;
		std::ostream                   stream_;
		std::mutex                     write_mutex_;

		template <typename Head>
		inline void append_to_stream(Head&& head) {
			this->stream_ << head << std::endl;
		}

		template <typename Head, typename... Tail>
		inline void append_to_stream(Head&& head, Tail&&... tail) {
			this->stream_ << head;

			this->append_to_stream(std::forward<Tail>(tail)...);
		}

};

}

#endif  // CHANGE_SRC_LOGGER_H_