]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // basic_logger.hpp | |
3 | // ~~~~~~~~~~~~~~~~ | |
4 | // | |
b32b8144 | 5 | // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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 | * | |
b32b8144 | 37 | * @param io_context The io_context object used to locate the logger service. |
7c673cae FG |
38 | * |
39 | * @param identifier An identifier for this logger. | |
40 | */ | |
b32b8144 | 41 | explicit basic_logger(boost::asio::io_context& io_context, |
7c673cae | 42 | const std::string& identifier) |
b32b8144 | 43 | : service_(boost::asio::use_service<Service>(io_context)), |
7c673cae FG |
44 | impl_(service_.null()) |
45 | { | |
46 | service_.create(impl_, identifier); | |
47 | } | |
48 | ||
49 | /// Destructor. | |
50 | ~basic_logger() | |
51 | { | |
52 | service_.destroy(impl_); | |
53 | } | |
54 | ||
b32b8144 FG |
55 | /// Get the io_context associated with the object. |
56 | boost::asio::io_context& get_io_context() | |
7c673cae | 57 | { |
b32b8144 | 58 | return service_.get_io_context(); |
7c673cae FG |
59 | } |
60 | ||
61 | /// Set the output file for all logger instances. | |
62 | void use_file(const std::string& file) | |
63 | { | |
64 | service_.use_file(impl_, file); | |
65 | } | |
66 | ||
67 | /// Log a message. | |
68 | void log(const std::string& message) | |
69 | { | |
70 | service_.log(impl_, message); | |
71 | } | |
72 | ||
73 | private: | |
74 | /// The backend service implementation. | |
75 | service_type& service_; | |
76 | ||
77 | /// The underlying native implementation. | |
78 | impl_type impl_; | |
79 | }; | |
80 | ||
81 | } // namespace services | |
82 | ||
83 | #endif // SERVICES_BASIC_LOGGER_HPP |