GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 74.9% 125 / 0 / 167
Functions: 74.4% 32 / 0 / 43
Branches: 76.1% 70 / 0 / 92

src/utilities/core/LogSink.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "LogSink.hpp"
7 #include "LogSink_Impl.hpp"
8
9 #include "Logger.hpp"
10 #include "LogMessage.hpp"
11
12 #include <boost/log/support/regex.hpp>
13 #include <boost/log/expressions.hpp>
14 #include <boost/phoenix/bind.hpp>
15 #include <boost/log/support/date_time.hpp>
16
17 namespace sinks = boost::log::sinks;
18 namespace keywords = boost::log::keywords;
19 namespace expr = boost::log::expressions;
20
21 namespace openstudio {
22
23 namespace detail {
24
25 // Custom severity level formatting function
26 3475 std::string formatter_severity_upcase(boost::log::value_ref<LogLevel> const& logLevel_) {
27 static constexpr std::array<std::string_view, 6> logLevelStrs = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"};
28 3475 LogLevel logLevel = Trace;
29
1/2
✓ Branch 0 taken 3475 times.
✗ Branch 1 not taken.
3475 if (logLevel_) {
30 // if (auto logLevel_ = rec[expr::attr<LogLevel>("Severity")]) {
31 3475 logLevel = logLevel_.get();
32 }
33
34
1/1
✓ Branch 2 taken 3475 times.
10425 return std::string{logLevelStrs[static_cast<size_t>(logLevel) - static_cast<size_t>(LogLevel::Trace)]};
35 }
36
37 13900 LogSink_Impl::LogSink_Impl()
38
3/5
✓ Branch 1 taken 13900 times.
✓ Branch 4 taken 13900 times.
✓ Branch 7 taken 13900 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
13900 : m_sink{boost::shared_ptr<LogSinkBackend>(new LogSinkBackend())},
39 // set formatting, seems like you have to call this after the stream is added
40 // DLM@20110701: would like to format Severity as string but can't figure out how to do it
41 // because you can't overload operator<< for an enum type
42 // this seems to suggest this should work: http://www.edm2.com/0405/enumeration.html
43
3/3
✓ Branch 5 taken 13900 times.
✓ Branch 8 taken 13900 times.
✓ Branch 11 taken 13900 times.
69500 m_formatter{expr::stream << "[" << expr::attr<LogChannel>("Channel") << "] <" << expr::attr<LogLevel>("Severity") << "> " << expr::smessage} {}
44
45 22 void LogSink_Impl::setFormatter(const boost::log::formatter& fmter) {
46
1/1
✓ Branch 1 taken 22 times.
22 std::unique_lock l{m_mutex};
47
1/1
✓ Branch 1 taken 22 times.
22 m_formatter = fmter;
48
1/1
✓ Branch 2 taken 22 times.
22 m_sink->set_formatter(m_formatter);
49 22 }
50
51 22 void LogSink_Impl::useWorkflowGemFormatter(bool use, bool include_channel) {
52
1/1
✓ Branch 1 taken 22 times.
22 std::unique_lock l{m_mutex};
53
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (use) {
54
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
22 Logger::instance().addTimeStampToLogger();
55
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (include_channel) {
56
1/1
✓ Branch 1 taken 22 times.
44 m_formatter = expr::stream << "[" << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S.%f") << " "
57
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
66 << boost::phoenix::bind(&formatter_severity_upcase, expr::attr<LogLevel>("Severity")) << "] " << "["
58
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
66 << expr::attr<LogChannel>("Channel") << "] " << expr::smessage;
59 } else {
60 ✗ m_formatter = expr::stream << "[" << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S.%f") << " "
61 ✗ << boost::phoenix::bind(&formatter_severity_upcase, expr::attr<LogLevel>("Severity")) << "] " << expr::smessage;
62 }
63
64 } else {
65 ✗ m_formatter = expr::stream << "[" << expr::attr<LogChannel>("Channel") << "] <" << expr::attr<LogLevel>("Severity") << "> " << expr::smessage;
66 }
67
1/1
✓ Branch 2 taken 22 times.
22 m_sink->set_formatter(m_formatter);
68 22 }
69
70 3 bool LogSink_Impl::isEnabled() const {
71
1/1
✓ Branch 3 taken 3 times.
3 return Logger::instance().findSink(m_sink);
72 }
73
74 5148 void LogSink_Impl::enable() {
75
1/1
✓ Branch 3 taken 5148 times.
5148 Logger::instance().addSink(m_sink);
76 5148 }
77
78 8397 void LogSink_Impl::disable() {
79
1/1
✓ Branch 3 taken 8397 times.
8397 Logger::instance().removeSink(m_sink);
80 8397 }
81
82 277 boost::optional<LogLevel> LogSink_Impl::logLevel() const {
83
1/1
✓ Branch 1 taken 277 times.
277 std::shared_lock l{m_mutex};
84
85 277 return m_logLevel;
86 277 }
87
88 13922 void LogSink_Impl::setLogLevel(LogLevel logLevel) {
89
1/1
✓ Branch 1 taken 13922 times.
13922 std::unique_lock l{m_mutex};
90
91
1/1
✓ Branch 1 taken 13922 times.
13922 m_logLevel = logLevel;
92
93
1/1
✓ Branch 1 taken 13922 times.
13922 this->updateFilter(l);
94 13922 }
95
96 ✗ void LogSink_Impl::resetLogLevel() {
97 ✗ std::unique_lock l{m_mutex};
98
99 ✗ m_logLevel.reset();
100
101 ✗ this->updateFilter(l);
102 ✗ }
103
104 10 boost::optional<boost::regex> LogSink_Impl::channelRegex() const {
105
1/1
✓ Branch 1 taken 10 times.
10 std::shared_lock l{m_mutex};
106
107
1/1
✓ Branch 1 taken 10 times.
20 return m_channelRegex;
108 10 }
109
110 1810 void LogSink_Impl::setChannelRegex(const boost::regex& channelRegex) {
111
1/1
✓ Branch 1 taken 1810 times.
1810 std::unique_lock l{m_mutex};
112
113
1/1
✓ Branch 1 taken 1810 times.
1810 m_channelRegex = channelRegex;
114
115
1/1
✓ Branch 1 taken 1810 times.
1810 this->updateFilter(l);
116 1810 }
117
118 2 void LogSink_Impl::setChannelRegex(const std::string& channelRegex) {
119
1/1
✓ Branch 1 taken 2 times.
2 boost::regex br(channelRegex);
120
1/1
✓ Branch 1 taken 2 times.
2 setChannelRegex(br);
121 2 }
122
123 ✗ void LogSink_Impl::resetChannelRegex() {
124 ✗ std::unique_lock l{m_mutex};
125
126 ✗ m_channelRegex.reset();
127
128 ✗ this->updateFilter(l);
129 ✗ }
130
131 ✗ bool LogSink_Impl::autoFlush() const {
132 ✗ std::shared_lock l{m_mutex};
133
134 ✗ return m_autoFlush;
135 ✗ }
136
137 13900 void LogSink_Impl::setAutoFlush(bool autoFlush) {
138
1/1
✓ Branch 1 taken 13900 times.
13900 std::unique_lock l{m_mutex};
139
140 13900 m_autoFlush = autoFlush;
141
142
2/2
✓ Branch 2 taken 13900 times.
✓ Branch 6 taken 13900 times.
13900 m_sink->locked_backend()->auto_flush(autoFlush);
143 13900 }
144
145 ✗ std::thread::id LogSink_Impl::threadId() const {
146 ✗ std::shared_lock l{m_mutex};
147
148 ✗ return m_threadId;
149 ✗ }
150
151 2379 void LogSink_Impl::setThreadId(const std::thread::id threadId) {
152
1/1
✓ Branch 1 taken 2379 times.
2379 std::unique_lock l{m_mutex};
153
154 2379 m_threadId = threadId;
155
156
1/1
✓ Branch 1 taken 2379 times.
2379 this->updateFilter(l);
157 2379 }
158
159 ✗ void LogSink_Impl::resetThreadId() {
160 ✗ std::unique_lock l{m_mutex};
161
162 ✗ m_threadId = std::thread::id{}; // default represents no thread
163
164 ✗ this->updateFilter(l);
165 ✗ }
166
167 13900 void LogSink_Impl::setStream(boost::shared_ptr<std::ostream> os) {
168
1/1
✓ Branch 1 taken 13900 times.
13900 std::unique_lock l{m_mutex};
169
170
2/2
✓ Branch 2 taken 13900 times.
✓ Branch 6 taken 13900 times.
13900 m_sink->locked_backend()->add_stream(os);
171
172
1/1
✓ Branch 2 taken 13900 times.
13900 m_sink->set_formatter(m_formatter);
173
174 //m_sink->locked_backend()->set_formatter(fmt::stream
175 // << "[" << fmt::attr< LogChannel >("Channel")
176 // << "] <" << fmt::attr< LogLevel >("Severity")
177 // << "> @" << fmt::attr< std::thread::id >("ThreadId") << ", " << fmt::message());
178
179
1/1
✓ Branch 1 taken 13900 times.
13900 this->updateFilter(l);
180
181 // avoid deadlock
182
1/1
✓ Branch 1 taken 13900 times.
13900 l.unlock();
183
184
1/1
✓ Branch 1 taken 13900 times.
13900 this->setAutoFlush(true);
185 13900 }
186
187 4379 boost::shared_ptr<LogSinkBackend> LogSink_Impl::sink() const {
188
1/1
✓ Branch 1 taken 4379 times.
4379 std::shared_lock l{m_mutex};
189
190 8758 return m_sink;
191 4379 }
192
193 32011 void LogSink_Impl::updateFilter(const std::unique_lock<std::shared_mutex>& /*l*/) {
194
1/1
✓ Branch 2 taken 32011 times.
32011 m_sink->reset_filter();
195
196 32011 LogLevel filterLogLevel = Trace;
197
2/2
✓ Branch 1 taken 18110 times.
✓ Branch 2 taken 13901 times.
64022 if (m_logLevel) {
198
1/1
✓ Branch 1 taken 18110 times.
18110 filterLogLevel = *m_logLevel;
199 }
200
201
1/1
✓ Branch 1 taken 32011 times.
32011 boost::regex filterChannelRegex(".*");
202
2/2
✓ Branch 1 taken 4000 times.
✓ Branch 2 taken 28011 times.
64022 if (m_channelRegex) {
203
2/2
✓ Branch 1 taken 4000 times.
✓ Branch 4 taken 4000 times.
4000 filterChannelRegex = *m_channelRegex;
204 }
205
206
2/2
✓ Branch 2 taken 3151 times.
✓ Branch 3 taken 28860 times.
32011 if (m_threadId != std::thread::id{}) {
207
3/3
✓ Branch 2 taken 3151 times.
✓ Branch 5 taken 3151 times.
✓ Branch 8 taken 3151 times.
9453 m_sink->set_filter(expr::attr<LogLevel>("Severity") >= filterLogLevel && expr::attr<std::thread::id>("ThreadId") == m_threadId
208
1/1
✓ Branch 1 taken 3151 times.
6302 && expr::matches(expr::attr<LogChannel>("Channel"), filterChannelRegex));
209 } else {
210
3/3
✓ Branch 2 taken 28860 times.
✓ Branch 5 taken 28860 times.
✓ Branch 8 taken 28860 times.
86580 m_sink->set_filter(expr::attr<LogLevel>("Severity") >= filterLogLevel && expr::matches(expr::attr<LogChannel>("Channel"), filterChannelRegex));
211 }
212 32011 }
213
214 } // namespace detail
215
216
2/4
✓ Branch 2 taken 8758 times.
✓ Branch 5 taken 8758 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
8758 LogSink::LogSink() : m_impl(new detail::LogSink_Impl()) {}
217
218 5142 LogSink::LogSink(const boost::shared_ptr<detail::LogSink_Impl>& impl) : m_impl(impl) {}
219
220 3 bool LogSink::isEnabled() const {
221 3 return m_impl->isEnabled();
222 }
223
224 6 void LogSink::enable() {
225 6 m_impl->enable();
226 6 }
227
228 7230 void LogSink::disable() {
229 7230 m_impl->disable();
230 7230 }
231
232 277 boost::optional<LogLevel> LogSink::logLevel() const {
233 277 return m_impl->logLevel();
234 }
235
236 13922 void LogSink::setLogLevel(LogLevel logLevel) {
237 13922 m_impl->setLogLevel(logLevel);
238 13922 }
239
240 ✗ void LogSink::resetLogLevel() {
241 ✗ m_impl->resetLogLevel();
242 ✗ }
243
244 10 boost::optional<boost::regex> LogSink::channelRegex() const {
245 10 return m_impl->channelRegex();
246 }
247
248 1808 void LogSink::setChannelRegex(const boost::regex& channelRegex) {
249 1808 m_impl->setChannelRegex(channelRegex);
250 1808 }
251
252 2 void LogSink::setChannelRegex(const std::string& channelRegex) {
253 2 m_impl->setChannelRegex(channelRegex);
254 2 }
255
256 ✗ void LogSink::resetChannelRegex() {
257 ✗ m_impl->resetChannelRegex();
258 ✗ }
259
260 ✗ bool LogSink::autoFlush() const {
261 ✗ return m_impl->autoFlush();
262 }
263
264 ✗ void LogSink::setAutoFlush(bool autoFlush) {
265 ✗ m_impl->setAutoFlush(autoFlush);
266 ✗ }
267
268 ✗ std::thread::id LogSink::threadId() const {
269 ✗ return m_impl->threadId();
270 }
271
272 2379 void LogSink::setThreadId(const std::thread::id threadId) {
273 2379 m_impl->setThreadId(threadId);
274 2379 }
275
276 ✗ void LogSink::resetThreadId() {
277 ✗ m_impl->resetThreadId();
278 ✗ }
279
280 8758 void LogSink::setStream(boost::shared_ptr<std::ostream> os) {
281
1/1
✓ Branch 3 taken 8758 times.
8758 m_impl->setStream(os);
282 8758 }
283
284 4379 boost::shared_ptr<LogSinkBackend> LogSink::sink() const {
285 4379 return m_impl->sink();
286 }
287
288 22 void LogSink::useWorkflowGemFormatter(bool use, bool include_channel) {
289 22 m_impl->useWorkflowGemFormatter(use, include_channel);
290 22 }
291
292 22 void LogSink::setFormatter(const boost::log::formatter& fmter) {
293 22 m_impl->setFormatter(fmter);
294 22 }
295
296 } // namespace openstudio
297