7#if !defined(ASYNC_MQTT_SETUP_LOG_HPP)
8#define ASYNC_MQTT_SETUP_LOG_HPP
17#include <async_mqtt/log.hpp>
19#if defined(ASYNC_MQTT_USE_LOG)
21#include <async_mqtt/util/move.hpp>
23#include <boost/filesystem.hpp>
24#include <boost/date_time/posix_time/posix_time_io.hpp>
30#if defined(ASYNC_MQTT_USE_LOG)
32static constexpr char const* log_color_table[] {
48void setup_log(std::map<std::string, severity_level> threshold) {
52 [](boost::log::record_view
const& rec, boost::log::formatting_ostream& strm) {
54 if (
auto v = boost::log::extract<boost::posix_time::ptime>(
"TimeStamp", rec)) {
59 new boost::posix_time::time_facet(
"%H:%M:%s")
62 strm << v.
get() <<
" ";
65 if (
auto v = boost::log::extract<boost::log::thread_id>(
"ThreadID", rec)) {
66 strm <<
"T:" << v.
get() <<
" ";
69 if (
auto v = boost::log::extract<severity_level>(
"Severity", rec)) {
70 strm << log_color_table[static_cast<std::size_t>(v.get())];
71 strm <<
"S:" << std::setw(7) << std::left << v.
get() <<
" ";
73 if (
auto v = boost::log::extract<channel>(
"Channel", rec)) {
74 strm <<
"C:" << std::setw(5) << std::left << v.
get() <<
" ";
77 if (
auto v = boost::log::extract<std::string>(
"MqttFile", rec)) {
78 strm << boost::filesystem::path(v.get()).filename().string() <<
":";
80 if (
auto v = boost::log::extract<unsigned int>(
"MqttLine", rec)) {
81 strm << v.
get() <<
" ";
83 if (
auto v = boost::log::extract<void const*>(
"MqttAddress", rec)) {
84 strm <<
"A:" << v.
get() <<
" ";
88 if (
auto v = boost::log::extract<std::string>(
"MqttFunction", rec)) {
92 strm << rec[boost::log::expressions::smessage];
97 boost::shared_ptr<std::ostream> stream(&std::clog, boost::null_deleter());
99 using text_sink = boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>;
100 auto sink = boost::make_shared<text_sink>();
101 sink->locked_backend()->add_stream(stream);
102 sink->set_formatter(fmt);
105 [threshold = force_move(threshold)]
106 (boost::log::attribute_value_set
const& avs) {
109 auto chan = boost::log::extract<channel>(
"Channel", avs);
110 auto sev = boost::log::extract<severity_level>(
"Severity", avs);
112 auto it = threshold.find(chan.get());
113 if (it == threshold.end())
return false;
114 return sev.get() >= it->second;
120 boost::log::core::get()->set_filter(fil);
121 boost::log::core::get()->add_sink(sink);
123 boost::log::add_common_attributes();
133void setup_log(severity_level threshold = severity_level::warning) {
136 {
"mqtt_api", threshold },
137 {
"mqtt_cb", threshold },
138 {
"mqtt_impl", threshold },
139 {
"mqtt_broker", threshold },
140 {
"mqtt_test", threshold },
147template <
typename... Params>
148void setup_log(Params&&...) {}
decltype(auto) get()
Get by type. If not match, then throw std::bad_variant_access exception.
Definition packet_variant.hpp:113