7#if !defined(ASYNC_MQTT_LOG_HPP)
8#define ASYNC_MQTT_LOG_HPP
14#if defined(ASYNC_MQTT_USE_LOG)
16#include <boost/log/core.hpp>
17#include <boost/log/attributes.hpp>
18#include <boost/log/attributes/scoped_attribute.hpp>
19#include <boost/log/expressions.hpp>
20#include <boost/log/expressions/keyword.hpp>
21#include <boost/log/sources/global_logger_storage.hpp>
22#include <boost/log/sources/severity_channel_logger.hpp>
23#include <boost/log/trivial.hpp>
24#include <boost/log/utility/manipulators/add_value.hpp>
25#include <boost/log/utility/setup/common_attributes.hpp>
26#include <boost/log/utility/setup/console.hpp>
27#include <boost/preprocessor/if.hpp>
28#include <boost/preprocessor/cat.hpp>
29#include <boost/preprocessor/comparison/greater_equal.hpp>
35struct channel : std::string {
36 using std::string::string;
39enum class severity_level {
48inline std::ostream& operator<<(std::ostream& o, severity_level sev) {
49 constexpr char const*
const str[] {
57 o << str[static_cast<std::size_t>(sev)];
64 template <
typename... Params>
65 constexpr null_log(Params&&...) {}
69inline constexpr null_log
const& operator<<(null_log
const& o, T
const&) {
return o; }
73#if defined(ASYNC_MQTT_USE_LOG)
77using global_logger_t = boost::log::sources::severity_channel_logger<severity_level, channel>;
78inline global_logger_t& logger() {
79 thread_local global_logger_t l;
84BOOST_LOG_ATTRIBUTE_KEYWORD(file,
"MqttFile", std::string)
85BOOST_LOG_ATTRIBUTE_KEYWORD(line,
"MqttLine",
unsigned int)
86BOOST_LOG_ATTRIBUTE_KEYWORD(function,
"MqttFunction", std::string)
87BOOST_LOG_ATTRIBUTE_KEYWORD(address,
"MqttAddress",
void const*)
91#define ASYNC_MQTT_LOG_FP(chan, sev) \
92 BOOST_LOG_STREAM_CHANNEL_SEV(async_mqtt::logger(), async_mqtt::channel(chan), sev) \
93 << boost::log::add_value(async_mqtt::file, __FILE__) \
94 << boost::log::add_value(async_mqtt::line, __LINE__) \
95 << boost::log::add_value(async_mqtt::function, BOOST_CURRENT_FUNCTION)
97#define ASYNC_MQTT_GET_LOG_SEV_NUM(lv) BOOST_PP_CAT(ASYNC_MQTT_, lv)
103#if !defined(ASYNC_MQTT_LOG_SEV)
104#define ASYNC_MQTT_LOG_SEV trace
107#define ASYNC_MQTT_trace 0
108#define ASYNC_MQTT_debug 1
109#define ASYNC_MQTT_info 2
110#define ASYNC_MQTT_warning 3
111#define ASYNC_MQTT_error 4
112#define ASYNC_MQTT_fatal 5
118#if !defined(ASYNC_MQTT_LOG)
120#define ASYNC_MQTT_LOG(chan, sev) \
122 BOOST_PP_GREATER_EQUAL(ASYNC_MQTT_GET_LOG_SEV_NUM(sev), ASYNC_MQTT_GET_LOG_SEV_NUM(ASYNC_MQTT_LOG_SEV)), \
123 ASYNC_MQTT_LOG_FP(chan, async_mqtt::severity_level::sev), \
124 async_mqtt::detail::null_log(chan, async_mqtt::severity_level::sev) \
129#if !defined(ASYNC_MQTT_ADD_VALUE)
131#define ASYNC_MQTT_ADD_VALUE(name, val) boost::log::add_value((async_mqtt::name), (val))
137#if !defined(ASYNC_MQTT_LOG)
139#define ASYNC_MQTT_LOG(chan, sev) async_mqtt::detail::null_log(chan, async_mqtt::severity_level::sev)
143#if !defined(ASYNC_MQTT_ADD_VALUE)
145#define ASYNC_MQTT_ADD_VALUE(name, val) val