• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // basic_logger.hpp
3 // ~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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_BASIC_LOGGER_HPP
12 #define SERVICES_BASIC_LOGGER_HPP
13 
14 #include <boost/asio.hpp>
15 #include <boost/noncopyable.hpp>
16 #include <string>
17 
18 namespace services {
19 
20 /// Class to provide simple logging functionality. Use the services::logger
21 /// typedef.
22 template <typename Service>
23 class basic_logger
24   : private boost::noncopyable
25 {
26 public:
27   /// The type of the service that will be used to provide timer operations.
28   typedef Service service_type;
29 
30   /// The native implementation type of the timer.
31   typedef typename service_type::impl_type impl_type;
32 
33   /// Constructor.
34   /**
35    * This constructor creates a logger.
36    *
37    * @param context The execution context used to locate the logger service.
38    *
39    * @param identifier An identifier for this logger.
40    */
basic_logger(boost::asio::execution_context & context,const std::string & identifier)41   explicit basic_logger(boost::asio::execution_context& context,
42       const std::string& identifier)
43     : service_(boost::asio::use_service<Service>(context)),
44       impl_(service_.null())
45   {
46     service_.create(impl_, identifier);
47   }
48 
49   /// Destructor.
~basic_logger()50   ~basic_logger()
51   {
52     service_.destroy(impl_);
53   }
54 
55   /// Set the output file for all logger instances.
use_file(const std::string & file)56   void use_file(const std::string& file)
57   {
58     service_.use_file(impl_, file);
59   }
60 
61   /// Log a message.
log(const std::string & message)62   void log(const std::string& message)
63   {
64     service_.log(impl_, message);
65   }
66 
67 private:
68   /// The backend service implementation.
69   service_type& service_;
70 
71   /// The underlying native implementation.
72   impl_type impl_;
73 };
74 
75 } // namespace services
76 
77 #endif // SERVICES_BASIC_LOGGER_HPP
78