async_mqtt 9.0.1
Loading...
Searching...
No Matches
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_LOG_HPP)
8#define ASYNC_MQTT_UTIL_LOG_HPP
9
10#include <cstddef>
11#include <ostream>
12#include <string>
13
14#if defined(ASYNC_MQTT_USE_LOG)
15
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>
30
31#endif // defined(ASYNC_MQTT_USE_LOG)
32
37namespace async_mqtt {
38
39struct channel : std::string {
40 using std::string::string;
41};
42
53enum class severity_level {
54 trace,
55 debug,
56 info,
57 warning,
58 error,
59 fatal
60};
61
62inline std::ostream& operator<<(std::ostream& o, severity_level sev) {
63 constexpr char const* const str[] {
64 "trace",
65 "debug",
66 "info",
67 "warning",
68 "error",
69 "fatal"
70 };
71 o << str[static_cast<std::size_t>(sev)];
72 return o;
73}
74
75namespace detail {
76
77struct null_log {
78 template <typename... Params>
79 explicit constexpr null_log(Params&&...) {}
80};
81
82template <typename T>
83inline constexpr null_log const& operator<<(null_log const& o, T const&) { return o; }
84
85} // namespace detail
86
87#if defined(ASYNC_MQTT_USE_LOG)
88
89// template arguments are defined in async_mqtt
90// filter and formatter can distinguish mqtt_cpp's channel and severity by their types
91using global_logger_type = boost::log::sources::severity_channel_logger<severity_level, channel>;
92inline global_logger_type& logger() {
93 thread_local global_logger_type l;
94 return l;
95}
96
97// Normal attributes
98BOOST_LOG_ATTRIBUTE_KEYWORD(file, "MqttFile", std::string)
99BOOST_LOG_ATTRIBUTE_KEYWORD(line, "MqttLine", unsigned int)
100BOOST_LOG_ATTRIBUTE_KEYWORD(function, "MqttFunction", std::string)
101BOOST_LOG_ATTRIBUTE_KEYWORD(address, "MqttAddress", void const*)
102
103
104// Take any filterable parameters (FP)
105#define ASYNC_MQTT_LOG_FP(chan, sev) \
106 BOOST_LOG_STREAM_CHANNEL_SEV(async_mqtt::logger(), async_mqtt::channel(chan), sev) \
107 << boost::log::add_value(async_mqtt::file, __FILE__) \
108 << boost::log::add_value(async_mqtt::line, __LINE__) \
109 << boost::log::add_value(async_mqtt::function, BOOST_CURRENT_FUNCTION)
110
111#define ASYNC_MQTT_GET_LOG_SEV_NUM(lv) BOOST_PP_CAT(ASYNC_MQTT_, lv)
112
113// Use can set preprocessor macro ASYNC_MQTT_LOG_SEV.
114// For example, -DASYNC_MQTT_LOG_SEV=info, greater or equal to info log is generated at
115// compiling time.
116
117#if !defined(ASYNC_MQTT_LOG_SEV)
118#define ASYNC_MQTT_LOG_SEV trace
119#endif // !defined(ASYNC_MQTT_LOG_SEV)
120
121#define ASYNC_MQTT_trace 0
122#define ASYNC_MQTT_debug 1
123#define ASYNC_MQTT_info 2
124#define ASYNC_MQTT_warning 3
125#define ASYNC_MQTT_error 4
126#define ASYNC_MQTT_fatal 5
127
128// User can define custom ASYNC_MQTT_LOG implementation
129// By default ASYNC_MQTT_LOG_FP is used
130
131
132#if !defined(ASYNC_MQTT_LOG)
133
134#define ASYNC_MQTT_LOG(chan, sev) \
135 BOOST_PP_IF( \
136 BOOST_PP_GREATER_EQUAL(ASYNC_MQTT_GET_LOG_SEV_NUM(sev), ASYNC_MQTT_GET_LOG_SEV_NUM(ASYNC_MQTT_LOG_SEV)), \
137 ASYNC_MQTT_LOG_FP(chan, async_mqtt::severity_level::sev), \
138 async_mqtt::detail::null_log(chan, async_mqtt::severity_level::sev) \
139 )
140
141#endif // !defined(ASYNC_MQTT_LOG)
142
143#if !defined(ASYNC_MQTT_ADD_VALUE)
144
145#define ASYNC_MQTT_ADD_VALUE(name, val) boost::log::add_value((async_mqtt::name), (val))
146
147#endif // !defined(ASYNC_MQTT_ADD_VALUE)
148
149#else // defined(ASYNC_MQTT_USE_LOG)
150
151#if !defined(ASYNC_MQTT_LOG)
152
153#define ASYNC_MQTT_LOG(chan, sev) async_mqtt::detail::null_log(chan, async_mqtt::severity_level::sev)
154
155#endif // !defined(ASYNC_MQTT_LOG)
156
157#if !defined(ASYNC_MQTT_ADD_VALUE)
158
159#define ASYNC_MQTT_ADD_VALUE(name, val) val
160
161#endif // !defined(ASYNC_MQTT_ADD_VALUE)
162
163#endif // defined(ASYNC_MQTT_USE_LOG)
164
165} // namespace async_mqtt
166
167#endif // ASYNC_MQTT_UTIL_LOG_HPP
severity_level
Definition log.hpp:53
@ trace
trace level for detaied behavior and reporting issue
@ warning
warning level such as timeout
@ debug
debug level not used in async_mqtt, so far
@ info
info level api call is output
@ error
error level error report such as connection is failed
@ fatal
fatal level it is logic error of async_mqtt
std::ostream & operator<<(std::ostream &o, mqtt_error v)
output to the stream