asio 0.3.7 Home | Reference | Tutorial | Examples | Design
Examples

services/logger_service.hpp

Go to the documentation of this file.
00001 #ifndef SERVICES_LOGGER_SERVICE_HPP
00002 #define SERVICES_LOGGER_SERVICE_HPP
00003 
00004 #include <asio.hpp>
00005 #include <boost/bind.hpp>
00006 #include <boost/date_time/posix_time/posix_time.hpp>
00007 #include <boost/noncopyable.hpp>
00008 #include <boost/scoped_ptr.hpp>
00009 #include <fstream>
00010 #include <sstream>
00011 #include <string>
00012 
00013 namespace services {
00014 
00016 class logger_service
00017   : public asio::io_service::service
00018 {
00019 public:
00021   struct logger_impl
00022   {
00023     explicit logger_impl(const std::string& id) : identifier(id) {}
00024     std::string identifier;
00025   };
00026 
00028   typedef logger_impl* impl_type;
00029 
00031   logger_service(asio::io_service& io_service)
00032     : asio::io_service::service(io_service),
00033       work_io_service_(),
00034       work_(new asio::io_service::work(work_io_service_)),
00035       work_thread_(new asio::thread(
00036             boost::bind(&asio::io_service::run, &work_io_service_)))
00037   {
00038   }
00039 
00041   ~logger_service()
00042   {
00045     work_.reset();
00046     if (work_thread_)
00047       work_thread_->join();
00048   }
00049 
00051   void shutdown_service()
00052   {
00053   }
00054 
00056   impl_type null() const
00057   {
00058     return 0;
00059   }
00060 
00062   void create(impl_type& impl, const std::string& identifier)
00063   {
00064     impl = new logger_impl(identifier);
00065   }
00066 
00068   void destroy(impl_type& impl)
00069   {
00070     delete impl;
00071     impl = null();
00072   }
00073 
00078   void use_file(impl_type& impl, const std::string& file)
00079   {
00080     // Pass the work of opening the file to the background thread.
00081     work_io_service_.post(boost::bind(
00082           &logger_service::use_file_impl, this, file));
00083   }
00084 
00086   void log(impl_type& impl, const std::string& message)
00087   {
00088     // Format the text to be logged.
00089     std::ostringstream os;
00090     os << boost::posix_time::microsec_clock::universal_time();
00091     os << " - " << impl->identifier << " - " << message;
00092 
00093     // Pass the work of opening the file to the background thread.
00094     work_io_service_.post(boost::bind(
00095           &logger_service::log_impl, this, os.str()));
00096   }
00097 
00098 private:
00101   void use_file_impl(const std::string& file)
00102   {
00103     ofstream_.close();
00104     ofstream_.clear();
00105     ofstream_.open(file.c_str());
00106   }
00107 
00110   void log_impl(const std::string& text)
00111   {
00112     ofstream_ << text << std::endl;
00113   }
00114 
00116   asio::io_service work_io_service_;
00117 
00121   boost::scoped_ptr<asio::io_service::work> work_;
00122 
00124   boost::scoped_ptr<asio::thread> work_thread_;
00125 
00127   std::ofstream ofstream_;
00128 };
00129 
00130 } // namespace services
00131 
00132 #endif // SERVICES_LOGGER_SERVICE_HPP
asio 0.3.7 Home | Reference | Tutorial | Examples | Design