async_mqtt 9.0.1
Loading...
Searching...
No Matches
setup_log.hpp
1// Copyright Takatoshi Kondo 2020
2//
3// Distributed under the Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#if !defined(ASYNC_MQTT_UTIL_SETUP_LOG_HPP)
8#define ASYNC_MQTT_UTIL_SETUP_LOG_HPP
9
10// This is an example implementation for logging setup.
11// If your code doesn't use Boost.Log then you can use the setup_log() directly.
12// setup_log() provides a typical console logging setup.
13// If you want to use existing Boost.Log related code with mqtt_cpp,
14// then you can write your own logging setup code.
15// setup_log() could be a good reference for your own logging setup code.
16
17#include <async_mqtt/util/log.hpp>
18
19#if defined(ASYNC_MQTT_USE_LOG)
20
21#include <async_mqtt/util/move.hpp>
22
23#include <boost/filesystem.hpp>
24#include <boost/date_time/posix_time/posix_time_io.hpp>
25
26#endif // defined(ASYNC_MQTT_USE_LOG)
27
28
29namespace async_mqtt {
30
31#if defined(ASYNC_MQTT_USE_LOG)
32
33static constexpr char const* log_color_table[] {
34 "\033[0m", // trace
35 "\033[36m", // debug
36 "\033[32m", // info
37 "\033[33m", // warning
38 "\033[35m", // error
39 "\033[31m", // fatal
40};
41
54inline
55void setup_log(std::map<std::string, severity_level> threshold) {
56 // https://www.boost.org/doc/libs/1_73_0/libs/log/doc/html/log/tutorial/advanced_filtering.html
57
58 auto fmt =
59 [](boost::log::record_view const& rec, boost::log::formatting_ostream& strm) {
60 // Timestamp custom formatting example
61 if (auto v = boost::log::extract<boost::posix_time::ptime>("TimeStamp", rec)) {
62 strm.imbue(
63 std::locale(
64 strm.getloc(),
65 // https://www.boost.org/doc/html/date_time/date_time_io.html#date_time.format_flags
66 new boost::posix_time::time_facet("%H:%M:%s") // ownership is moved here
67 )
68 );
69 strm << v.get() << " ";
70 }
71 // ThreadID example
72 if (auto v = boost::log::extract<boost::log::thread_id>("ThreadID", rec)) {
73 strm << "T:" << v.get() << " ";
74 }
75 // Adjust severity length example
76 if (auto v = boost::log::extract<severity_level>("Severity", rec)) {
77 strm << log_color_table[static_cast<std::size_t>(v.get())];
78 strm << "S:" << std::setw(7) << std::left << v.get() << " ";
79 }
80 if (auto v = boost::log::extract<channel>("Channel", rec)) {
81 strm << "C:" << std::setw(5) << std::left << v.get() << " ";
82 }
83 // Shorten file path example
84 if (auto v = boost::log::extract<std::string>("MqttFile", rec)) {
85 strm << boost::filesystem::path(v.get()).filename().string() << ":";
86 }
87 if (auto v = boost::log::extract<unsigned int>("MqttLine", rec)) {
88 strm << v.get() << " ";
89 }
90 if (auto v = boost::log::extract<void const*>("MqttAddress", rec)) {
91 strm << "A:" << v.get() << " ";
92 }
93
94#if 0 // function is ofthen noisy
95 if (auto v = boost::log::extract<std::string>("MqttFunction", rec)) {
96 strm << v << ":";
97 }
98#endif
99 strm << rec[boost::log::expressions::smessage];
100 strm << "\033[0m";
101 };
102
103 // https://www.boost.org/doc/libs/1_73_0/libs/log/doc/html/log/tutorial/sinks.html
104 boost::shared_ptr<std::ostream> stream(&std::clog, boost::null_deleter());
105
106 using text_sink = boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>;
107 auto sink = boost::make_shared<text_sink>();
108 sink->locked_backend()->add_stream(stream);
109 sink->set_formatter(fmt);
110
111 auto fil =
112 [threshold = force_move(threshold)]
113 (boost::log::attribute_value_set const& avs) {
114 {
115 // For mqtt
116 auto chan = boost::log::extract<channel>("Channel", avs);
117 auto sev = boost::log::extract<severity_level>("Severity", avs);
118 if (chan && sev) {
119 auto it = threshold.find(chan.get());
120 if (it == threshold.end()) return false;
121 return sev.get() >= it->second;
122 }
123 }
124 return true;
125 };
126
127 boost::log::core::get()->set_filter(fil);
128 boost::log::core::get()->add_sink(sink);
129
130 boost::log::add_common_attributes();
131}
132
145inline
147 setup_log(
148 {
149 { "mqtt_api", threshold },
150 { "mqtt_cb", threshold },
151 { "mqtt_impl", threshold },
152 { "mqtt_broker", threshold },
153 { "mqtt_test", threshold },
154 }
155 );
156}
157
158#else // defined(ASYNC_MQTT_USE_LOG)
159
160template <typename... Params>
161void setup_log(Params&&...) {}
162
163#endif // defined(ASYNC_MQTT_USE_LOG)
164
165} // namespace async_mqtt
166
167#endif // ASYNC_MQTT_UTIL_SETUP_LOG_HPP
severity_level
Definition log.hpp:53
void setup_log(std::map< std::string, severity_level > threshold)
Setup logging.
Definition setup_log.hpp:55
@ warning
warning level such as timeout