-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathlog.hpp
131 lines (99 loc) · 3.7 KB
/
log.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright Takatoshi Kondo 2020
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if !defined(MQTT_LOG_HPP)
#define MQTT_LOG_HPP
#include <tuple>
#include <boost/log/core.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/attributes/scoped_attribute.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/expressions/keyword.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/manipulators/add_value.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/preprocessor/if.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/comparison/greater_equal.hpp>
namespace MQTT_NS {
namespace log = boost::log;
struct channel : std::string {
using std::string::string;
};
enum class severity_level {
trace,
debug,
info,
warning,
error,
fatal
};
inline std::ostream& operator<<(std::ostream& o, severity_level sev) {
constexpr char const* const str[] {
"trace",
"debug",
"info",
"warning",
"error",
"fatal"
};
o << str[static_cast<std::size_t>(sev)];
return o;
}
// template arguments are defined in MQTT_NS
// filter and formatter can distinguish mqtt_cpp's channel and severity by their types
using global_logger_t = log::sources::severity_channel_logger_mt<severity_level, channel>;
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(logger, global_logger_t);
// Normal attributes
BOOST_LOG_ATTRIBUTE_KEYWORD(file, "MqttFile", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(line, "MqttLine", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(function, "MqttFunction", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(address, "MqttAddress", void const*)
// Take any filterable parameters (FP)
#define MQTT_LOG_FP(chan, sev) \
BOOST_LOG_STREAM_CHANNEL_SEV(logger::get(), channel(chan), sev) \
<< log::add_value(file, __FILE__) \
<< log::add_value(line, __LINE__) \
<< log::add_value(function, BOOST_CURRENT_FUNCTION)
namespace detail {
struct null_log {
template <typename... Params>
constexpr null_log(Params&&...) {}
};
template <typename T>
inline constexpr null_log const& operator<<(null_log const& o, T const&) { return o; }
} // namespace detail
#if defined(MQTT_DISABLE_LOG)
#define MQTT_LOG(chan, sev) detail::null_log(chan, severity_level::sev)
#else // defined(MQTT_DISABLE_LOG)
#define MQTT_GET_LOG_SEV_NUM(lv) BOOST_PP_CAT(MQTT_, lv)
// Use can set preprocessor macro MQTT_LOG_SEV.
// For example, -DMQTT_LOG_SEV=info, greater or equal to info log is generated at
// compiling time.
#if !defined(MQTT_LOG_SEV)
#define MQTT_LOG_SEV trace
#endif // !defined(MQTT_LOG_SEV)
#define MQTT_trace 0
#define MQTT_debug 1
#define MQTT_info 2
#define MQTT_warning 3
#define MQTT_error 4
#define MQTT_fatal 5
// User can define custom MQTT_LOG implementation
// By default MQTT_LOG_FP is used
#if !defined(MQTT_LOG)
#define MQTT_LOG(chan, sev) \
BOOST_PP_IF( \
BOOST_PP_GREATER_EQUAL(MQTT_GET_LOG_SEV_NUM(sev), MQTT_GET_LOG_SEV_NUM(MQTT_LOG_SEV)), \
MQTT_LOG_FP(chan, severity_level::sev), \
MQTT_NS::detail::null_log(chan, severity_level::sev) \
)
#endif // !defined(MQTT_LOG)
#endif // defined(MQTT_DISABLE_LOG)
} // namespace MQTT_NS
#endif // MQTT_LOG_HPP