正在显示
1 个修改的文件
包含
202 行增加
和
0 行删除
src/core/dmplogger.cpp
0 → 100644
1 | +/************************************************************************** | |
2 | +* file: dmplogger.cpp | |
3 | + | |
4 | +* Author: wanzhongping | |
5 | +* Date: 2021-03-03 10:29:10 | |
6 | +* Email: zhongpingw@chinadci.com | |
7 | +* copyright: 广州城市信息研究所有限公司 | |
8 | +***************************************************************************/ | |
9 | + | |
10 | +#include "dmplogger.h" | |
11 | +#include <iostream> | |
12 | +#include <fstream> | |
13 | +#include <boost/core/ref.hpp> | |
14 | +#include <boost/bind/bind.hpp> | |
15 | +#include <boost/log/common.hpp> | |
16 | +#include <boost/log/expressions.hpp> | |
17 | +#include <boost/log/attributes/timer.hpp> | |
18 | +#include <boost/log/attributes/named_scope.hpp> | |
19 | +#include <boost/log/sources/logger.hpp> | |
20 | +#include <boost/log/support/date_time.hpp> | |
21 | +#include <boost/log/utility/setup/file.hpp> | |
22 | +#include <boost/log/utility/setup/common_attributes.hpp> | |
23 | +#include <boost/log/utility/setup/from_stream.hpp> | |
24 | +#include <boost/log/utility/setup/filter_parser.hpp> | |
25 | +#include <boost/log/utility/setup/formatter_parser.hpp> | |
26 | +#include <boost/format.hpp> | |
27 | + | |
28 | +namespace logging = boost::log; | |
29 | +namespace src = boost::log::sources; | |
30 | +namespace expr = boost::log::expressions; | |
31 | +namespace sinks = boost::log::sinks; | |
32 | +namespace keywords = boost::log::keywords; | |
33 | +namespace attrs = boost::log::attributes; | |
34 | + | |
35 | +static src::severity_logger< SeverityLevel > slg; | |
36 | +static std::string sConfigName = "log.config"; | |
37 | + | |
38 | +//! Formatting operator for severity levels | |
39 | +std::ostream& operator<< (std::ostream& strm, SeverityLevel level) | |
40 | +{ | |
41 | + static const char* const str[] = | |
42 | + { | |
43 | + "debug", | |
44 | + "info", | |
45 | + "warning", | |
46 | + "error", | |
47 | + "critical" | |
48 | + }; | |
49 | + if (static_cast< std::size_t >(level) < (sizeof(str) / sizeof(*str))) | |
50 | + strm << str[level]; | |
51 | + else | |
52 | + strm << static_cast< int >(level); | |
53 | + return strm; | |
54 | +} | |
55 | + | |
56 | +//! Parsing operator for severity levels | |
57 | +std::istream& operator>> (std::istream& strm, SeverityLevel& level) | |
58 | +{ | |
59 | + if (strm.good()) | |
60 | + { | |
61 | + std::string str; | |
62 | + strm >> str; | |
63 | + if (str == "debug") | |
64 | + level = debug_level; | |
65 | + else if (str == "info") | |
66 | + level = info_level; | |
67 | + else if (str == "warning") | |
68 | + level = warn_level; | |
69 | + else if (str == "error") | |
70 | + level = error_level; | |
71 | + else if (str == "critical") | |
72 | + level = critical_level; | |
73 | + else | |
74 | + strm.setstate(std::ios_base::failbit); | |
75 | + } | |
76 | + | |
77 | + return strm; | |
78 | +} | |
79 | + | |
80 | +//! Our custom formatter for the scope list | |
81 | +struct scope_list_formatter | |
82 | +{ | |
83 | + typedef void result_type; | |
84 | + typedef attrs::named_scope::value_type scope_stack; | |
85 | + | |
86 | + explicit scope_list_formatter(logging::attribute_name const& name) : | |
87 | + name_(name) | |
88 | + { | |
89 | + } | |
90 | + void operator()(logging::record_view const& rec, logging::formatting_ostream& strm) const | |
91 | + { | |
92 | + // We need to acquire the attribute value from the log record | |
93 | + logging::visit< scope_stack > | |
94 | + ( | |
95 | + name_, | |
96 | + rec.attribute_values(), | |
97 | + boost::bind(&scope_list_formatter::format, boost::placeholders::_1, boost::ref(strm)) | |
98 | + ); | |
99 | + } | |
100 | + | |
101 | +private: | |
102 | + //! This is where our custom formatting takes place | |
103 | + static void format(scope_stack const& scopes, logging::formatting_ostream& strm) | |
104 | + { | |
105 | + scope_stack::const_iterator it = scopes.begin(), end = scopes.end(); | |
106 | + for (; it != end; ++it) | |
107 | + { | |
108 | + strm << it->scope_name << " (" << it->file_name << ":" << it->line << ")"; | |
109 | + } | |
110 | + } | |
111 | + | |
112 | +private: | |
113 | + logging::attribute_name name_; | |
114 | +}; | |
115 | + | |
116 | +class my_scopes_formatter_factory : public logging::formatter_factory< char > | |
117 | +{ | |
118 | +public: | |
119 | + /*! | |
120 | + * This function creates a formatter for the MyScopes attribute. | |
121 | + * It effectively associates the attribute with the scope_list_formatter class | |
122 | + */ | |
123 | + formatter_type create_formatter(logging::attribute_name const& attr_name, args_map const& args) | |
124 | + { | |
125 | + return formatter_type(scope_list_formatter(attr_name)); | |
126 | + } | |
127 | +}; | |
128 | + | |
129 | + | |
130 | + | |
131 | +DmpLogger::DmpLogger() | |
132 | +{ | |
133 | + Init(); | |
134 | +} | |
135 | + | |
136 | +DmpLogger::~DmpLogger() | |
137 | +{ | |
138 | +} | |
139 | + | |
140 | +void DmpLogger::Init() | |
141 | +{ | |
142 | + // First thing - register the custom formatter for MyScopes | |
143 | + logging::register_formatter_factory("Scope", boost::make_shared< my_scopes_formatter_factory >()); | |
144 | + | |
145 | + // Also register filter and formatter factories for our custom severity level enum. Since our operator<< and operator>> implement | |
146 | + // all required behavior, simple factories provided by Boost.Log will do. | |
147 | + logging::register_simple_filter_factory< SeverityLevel >("Severity"); | |
148 | + logging::register_simple_formatter_factory< SeverityLevel, char >("Severity"); | |
149 | + | |
150 | + | |
151 | + // Open the setting file | |
152 | + std::ifstream settings(sConfigName); | |
153 | + boost::format fmt = boost::format("Could not open %s file!") % sConfigName; | |
154 | + | |
155 | + if (!settings.is_open()) | |
156 | + // throw std::runtime_error("Could not open log.config file!"); | |
157 | + throw std::runtime_error(fmt.str()); | |
158 | + | |
159 | + logging::init_from_stream(settings); | |
160 | + | |
161 | + // Also let's add some commonly used attributes, like timestamp and record counter. | |
162 | + logging::add_common_attributes(); | |
163 | + logging::core::get()->add_thread_attribute("Scope", attrs::named_scope()); | |
164 | + | |
165 | + slg.add_attribute("Uptime", attrs::timer()); | |
166 | +} | |
167 | + | |
168 | +void DmpLogger::Debug(const std::string &msg) | |
169 | +{ | |
170 | + BOOST_LOG_SEV(slg, debug_level) << msg; | |
171 | +} | |
172 | + | |
173 | +void DmpLogger::Info(const std::string &msg) | |
174 | +{ | |
175 | + BOOST_LOG_SEV(slg, info_level) << msg; | |
176 | +} | |
177 | + | |
178 | +void DmpLogger::Warn(const std::string &msg) | |
179 | +{ | |
180 | + BOOST_LOG_SEV(slg, warn_level) << msg; | |
181 | +} | |
182 | + | |
183 | +void DmpLogger::Error(const std::string &msg) | |
184 | +{ | |
185 | + BOOST_LOG_SEV(slg, error_level) << msg; | |
186 | +} | |
187 | + | |
188 | +void DmpLogger::Critical(const std::string &msg) | |
189 | +{ | |
190 | + BOOST_LOG_SEV(slg, critical_level) << msg; | |
191 | +} | |
192 | + | |
193 | +void DmpLogger::SetLevel(SeverityLevel level) | |
194 | +{ | |
195 | + logging::core::get()->set_filter(expr::attr< SeverityLevel >("Severity") >= level); | |
196 | +} | |
197 | + | |
198 | +DmpLogger *DmpLogger::Instance() | |
199 | +{ | |
200 | + static DmpLogger logger; | |
201 | + return &logger; | |
202 | +} | |
\ No newline at end of file | ... | ... |
请
注册
或
登录
后发表评论