Realistic 3D camera system
3D camera system components
logger_service.hpp
Go to the documentation of this file.
1 //
2 // logger_service.hpp
3 // ~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef SERVICES_LOGGER_SERVICE_HPP
12 #define SERVICES_LOGGER_SERVICE_HPP
13 
14 #include <asio.hpp>
15 #include <boost/bind.hpp>
16 #include <boost/date_time/posix_time/posix_time.hpp>
17 #include <boost/noncopyable.hpp>
18 #include <boost/scoped_ptr.hpp>
19 #include <fstream>
20 #include <sstream>
21 #include <string>
22 
23 namespace services {
24 
28 {
29 public:
32 
34  struct logger_impl
35  {
36  explicit logger_impl(const std::string& ident) : identifier(ident) {}
37  std::string identifier;
38  };
39 
42 
45  : asio::io_service::service(io_service),
46  work_io_service_(),
47  work_(new asio::io_service::work(work_io_service_)),
48  work_thread_(new asio::thread(
49  boost::bind(&asio::io_service::run, &work_io_service_)))
50  {
51  }
52 
55  {
58  work_.reset();
59  if (work_thread_)
60  work_thread_->join();
61  }
62 
65  {
66  }
67 
69  impl_type null() const
70  {
71  return 0;
72  }
73 
75  void create(impl_type& impl, const std::string& identifier)
76  {
77  impl = new logger_impl(identifier);
78  }
79 
81  void destroy(impl_type& impl)
82  {
83  delete impl;
84  impl = null();
85  }
86 
91  void use_file(impl_type& /*impl*/, const std::string& file)
92  {
93  // Pass the work of opening the file to the background thread.
94  work_io_service_.post(boost::bind(
95  &logger_service::use_file_impl, this, file));
96  }
97 
99  void log(impl_type& impl, const std::string& message)
100  {
101  // Format the text to be logged.
102  std::ostringstream os;
103  os << impl->identifier << ": " << message;
104 
105  // Pass the work of opening the file to the background thread.
106  work_io_service_.post(boost::bind(
107  &logger_service::log_impl, this, os.str()));
108  }
109 
110 private:
113  void use_file_impl(const std::string& file)
114  {
115  ofstream_.close();
116  ofstream_.clear();
117  ofstream_.open(file.c_str());
118  }
119 
122  void log_impl(const std::string& text)
123  {
124  ofstream_ << text << std::endl;
125  }
126 
128  asio::io_service work_io_service_;
129 
133  boost::scoped_ptr<asio::io_service::work> work_;
134 
136  boost::scoped_ptr<asio::thread> work_thread_;
137 
139  std::ofstream ofstream_;
140 };
141 
142 } // namespace services
143 
144 #endif // SERVICES_LOGGER_SERVICE_HPP
void use_file(impl_type &, const std::string &file)
void log(impl_type &impl, const std::string &message)
Log a message.
Class used to uniquely identify a service.
Definition: io_service.hpp:665
Provides core I/O functionality.
Definition: io_service.hpp:184
ASIO_DECL service(asio::io_service &owner)
Constructor.
Definition: io_service.ipp:127
impl_type null() const
Return a null logger implementation.
Service implementation for the logger.
The backend implementation of a logger.
static asio::io_service::id id
The unique service identifier.
null_thread thread
Definition: thread.hpp:40
logger_impl * impl_type
The type for an implementation of the logger.
int bind(socket_type s, const socket_addr_type *addr, std::size_t addrlen, asio::error_code &ec)
Definition: socket_ops.ipp:278
void shutdown_service()
Destroy all user-defined handler objects owned by the service.
~logger_service()
Destructor shuts down the private io_service.
void create(impl_type &impl, const std::string &identifier)
Create a new logger implementation.
Base class for all io_service services.
Definition: io_service.hpp:674
void destroy(impl_type &impl)
Destroy a logger implementation.
logger_service(asio::io_service &io_service)
Constructor creates a thread to run a private io_service.
logger_impl(const std::string &ident)