mqtt_cpp
setup_log.hpp
Go to the documentation of this file.
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(MQTT_SETUP_LOG_HPP)
8 #define MQTT_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 <mqtt/namespace.hpp>
18 #include <mqtt/log.hpp>
19 #include <mqtt/move.hpp>
20 
21 #include <boost/filesystem.hpp>
22 #include <boost/date_time/posix_time/posix_time_io.hpp>
23 
24 namespace MQTT_NS {
25 
26 #if defined(MQTT_USE_LOG)
27 
34 inline
35 void setup_log(std::map<std::string, severity_level> threshold) {
36  // https://www.boost.org/doc/libs/1_73_0/libs/log/doc/html/log/tutorial/advanced_filtering.html
37 
38  auto fmt =
39  [](boost::log::record_view const& rec, boost::log::formatting_ostream& strm) {
40  // Timestamp custom formatting example
41  if (auto v = boost::log::extract<boost::posix_time::ptime>("TimeStamp", rec)) {
42  strm.imbue(
43  std::locale(
44  strm.getloc(),
45  // https://www.boost.org/doc/html/date_time/date_time_io.html#date_time.format_flags
46  new boost::posix_time::time_facet("%H:%M:%s") // ownership is moved here
47  )
48  );
49  strm << v.get() << " ";
50  }
51  // ThreadID example
52  if (auto v = boost::log::extract<boost::log::thread_id>("ThreadID", rec)) {
53  strm << "T:" << v.get() << " ";
54  }
55  // Adjust severity length example
56  if (auto v = boost::log::extract<severity_level>("Severity", rec)) {
57  strm << "S:" << std::setw(7) << std::left << v.get() << " ";
58  }
59  if (auto v = boost::log::extract<channel>("Channel", rec)) {
60  strm << "C:" << std::setw(5) << std::left << v.get() << " ";
61  }
62  // Shorten file path example
63  if (auto v = boost::log::extract<std::string>("MqttFile", rec)) {
64  strm << boost::filesystem::path(v.get()).filename().string() << ":";
65  }
66  if (auto v = boost::log::extract<unsigned int>("MqttLine", rec)) {
67  strm << v.get() << " ";
68  }
69  if (auto v = boost::log::extract<void const*>("MqttAddress", rec)) {
70  strm << "A:" << v.get() << " ";
71  }
72 
73 #if 0 // function is ofthen noisy
74  if (auto v = boost::log::extract<std::string>("MqttFunction", rec)) {
75  strm << v << ":";
76  }
77 #endif
78  strm << rec[boost::log::expressions::smessage];
79  };
80 
81  // https://www.boost.org/doc/libs/1_73_0/libs/log/doc/html/log/tutorial/sinks.html
82  boost::shared_ptr<std::ostream> stream(&std::clog, boost::null_deleter());
83 
84  using text_sink = boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>;
85  auto sink = boost::make_shared<text_sink>();
86  sink->locked_backend()->add_stream(stream);
87  sink->set_formatter(fmt);
88 
89  auto fil =
90  [threshold = force_move(threshold)]
91  (boost::log::attribute_value_set const& avs) {
92  {
93  // For mqtt
94  auto chan = boost::log::extract<channel>("Channel", avs);
95  auto sev = boost::log::extract<severity_level>("Severity", avs);
96  if (chan && sev) {
97  auto it = threshold.find(chan.get());
98  if (it == threshold.end()) return false;
99  return sev.get() >= it->second;
100  }
101  }
102  return true;
103  };
104 
105  boost::log::core::get()->set_filter(fil);
106  boost::log::core::get()->add_sink(sink);
107 
108  boost::log::add_common_attributes();
109 }
110 
117 inline
119  setup_log(
120  {
121  { "mqtt_api", threshold },
122  { "mqtt_cb", threshold },
123  { "mqtt_impl", threshold },
124  { "mqtt_broker", threshold },
125  }
126  );
127 }
128 
129 #else // defined(MQTT_USE_LOG)
130 
131 template <typename... Params>
132 void setup_log(Params&&...) {}
133 
134 #endif // defined(MQTT_USE_LOG)
135 
136 } // namespace MQTT_NS
137 
138 #endif // MQTT_SETUP_LOG_HPP
Definition: any.hpp:27
void setup_log(Params &&...)
Definition: setup_log.hpp:132
constexpr std::remove_reference_t< T > && force_move(T &&t)
Definition: move.hpp:20
severity_level
Definition: log.hpp:37