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 | 13744 | LogSink_Impl::LogSink_Impl() | |
| 38 |
3/5✓ Branch 1 taken 13744 times.
✓ Branch 4 taken 13744 times.
✓ Branch 7 taken 13744 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
13744 | : 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 13744 times.
✓ Branch 8 taken 13744 times.
✓ Branch 11 taken 13744 times.
|
68720 | 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 | 5087 | void LogSink_Impl::enable() { | |
| 75 |
1/1✓ Branch 3 taken 5087 times.
|
5087 | Logger::instance().addSink(m_sink); |
| 76 | 5087 | } | |
| 77 | |||
| 78 | 8289 | void LogSink_Impl::disable() { | |
| 79 |
1/1✓ Branch 3 taken 8289 times.
|
8289 | Logger::instance().removeSink(m_sink); |
| 80 | 8289 | } | |
| 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 | 13766 | void LogSink_Impl::setLogLevel(LogLevel logLevel) { | |
| 89 |
1/1✓ Branch 1 taken 13766 times.
|
13766 | std::unique_lock l{m_mutex}; |
| 90 | |||
| 91 |
1/1✓ Branch 1 taken 13766 times.
|
13766 | m_logLevel = logLevel; |
| 92 | |||
| 93 |
1/1✓ Branch 1 taken 13766 times.
|
13766 | this->updateFilter(l); |
| 94 | 13766 | } | |
| 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 | 1799 | void LogSink_Impl::setChannelRegex(const boost::regex& channelRegex) { | |
| 111 |
1/1✓ Branch 1 taken 1799 times.
|
1799 | std::unique_lock l{m_mutex}; |
| 112 | |||
| 113 |
1/1✓ Branch 1 taken 1799 times.
|
1799 | m_channelRegex = channelRegex; |
| 114 | |||
| 115 |
1/1✓ Branch 1 taken 1799 times.
|
1799 | this->updateFilter(l); |
| 116 | 1799 | } | |
| 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 | 13744 | void LogSink_Impl::setAutoFlush(bool autoFlush) { | |
| 138 |
1/1✓ Branch 1 taken 13744 times.
|
13744 | std::unique_lock l{m_mutex}; |
| 139 | |||
| 140 | 13744 | m_autoFlush = autoFlush; | |
| 141 | |||
| 142 |
2/2✓ Branch 2 taken 13744 times.
✓ Branch 6 taken 13744 times.
|
13744 | m_sink->locked_backend()->auto_flush(autoFlush); |
| 143 | 13744 | } | |
| 144 | |||
| 145 | ✗ | std::thread::id LogSink_Impl::threadId() const { | |
| 146 | ✗ | std::shared_lock l{m_mutex}; | |
| 147 | |||
| 148 | ✗ | return m_threadId; | |
| 149 | ✗ | } | |
| 150 | |||
| 151 | 2354 | void LogSink_Impl::setThreadId(const std::thread::id threadId) { | |
| 152 |
1/1✓ Branch 1 taken 2354 times.
|
2354 | std::unique_lock l{m_mutex}; |
| 153 | |||
| 154 | 2354 | m_threadId = threadId; | |
| 155 | |||
| 156 |
1/1✓ Branch 1 taken 2354 times.
|
2354 | this->updateFilter(l); |
| 157 | 2354 | } | |
| 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 | 13744 | void LogSink_Impl::setStream(boost::shared_ptr<std::ostream> os) { | |
| 168 |
1/1✓ Branch 1 taken 13744 times.
|
13744 | std::unique_lock l{m_mutex}; |
| 169 | |||
| 170 |
2/2✓ Branch 2 taken 13744 times.
✓ Branch 6 taken 13744 times.
|
13744 | m_sink->locked_backend()->add_stream(os); |
| 171 | |||
| 172 |
1/1✓ Branch 2 taken 13744 times.
|
13744 | 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 13744 times.
|
13744 | this->updateFilter(l); |
| 180 | |||
| 181 | // avoid deadlock | ||
| 182 |
1/1✓ Branch 1 taken 13744 times.
|
13744 | l.unlock(); |
| 183 | |||
| 184 |
1/1✓ Branch 1 taken 13744 times.
|
13744 | this->setAutoFlush(true); |
| 185 | 13744 | } | |
| 186 | |||
| 187 | 4331 | boost::shared_ptr<LogSinkBackend> LogSink_Impl::sink() const { | |
| 188 |
1/1✓ Branch 1 taken 4331 times.
|
4331 | std::shared_lock l{m_mutex}; |
| 189 | |||
| 190 | 8662 | return m_sink; | |
| 191 | 4331 | } | |
| 192 | |||
| 193 | 31663 | void LogSink_Impl::updateFilter(const std::unique_lock<std::shared_mutex>& /*l*/) { | |
| 194 |
1/1✓ Branch 2 taken 31663 times.
|
31663 | m_sink->reset_filter(); |
| 195 | |||
| 196 | 31663 | LogLevel filterLogLevel = Trace; | |
| 197 |
2/2✓ Branch 1 taken 17918 times.
✓ Branch 2 taken 13745 times.
|
63326 | if (m_logLevel) { |
| 198 |
1/1✓ Branch 1 taken 17918 times.
|
17918 | filterLogLevel = *m_logLevel; |
| 199 | } | ||
| 200 | |||
| 201 |
1/1✓ Branch 1 taken 31663 times.
|
31663 | boost::regex filterChannelRegex(".*"); |
| 202 |
2/2✓ Branch 1 taken 3964 times.
✓ Branch 2 taken 27699 times.
|
63326 | if (m_channelRegex) { |
| 203 |
2/2✓ Branch 1 taken 3964 times.
✓ Branch 4 taken 3964 times.
|
3964 | filterChannelRegex = *m_channelRegex; |
| 204 | } | ||
| 205 | |||
| 206 |
2/2✓ Branch 2 taken 3126 times.
✓ Branch 3 taken 28537 times.
|
31663 | if (m_threadId != std::thread::id{}) { |
| 207 |
3/3✓ Branch 2 taken 3126 times.
✓ Branch 5 taken 3126 times.
✓ Branch 8 taken 3126 times.
|
9378 | m_sink->set_filter(expr::attr<LogLevel>("Severity") >= filterLogLevel && expr::attr<std::thread::id>("ThreadId") == m_threadId |
| 208 |
1/1✓ Branch 1 taken 3126 times.
|
6252 | && expr::matches(expr::attr<LogChannel>("Channel"), filterChannelRegex)); |
| 209 | } else { | ||
| 210 |
3/3✓ Branch 2 taken 28537 times.
✓ Branch 5 taken 28537 times.
✓ Branch 8 taken 28537 times.
|
85611 | m_sink->set_filter(expr::attr<LogLevel>("Severity") >= filterLogLevel && expr::matches(expr::attr<LogChannel>("Channel"), filterChannelRegex)); |
| 211 | } | ||
| 212 | 31663 | } | |
| 213 | |||
| 214 | } // namespace detail | ||
| 215 | |||
| 216 |
2/4✓ Branch 2 taken 8662 times.
✓ Branch 5 taken 8662 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
8662 | LogSink::LogSink() : m_impl(new detail::LogSink_Impl()) {} |
| 217 | |||
| 218 | 5082 | 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 | 5 | void LogSink::enable() { | |
| 225 | 5 | m_impl->enable(); | |
| 226 | 5 | } | |
| 227 | |||
| 228 | 7134 | void LogSink::disable() { | |
| 229 | 7134 | m_impl->disable(); | |
| 230 | 7134 | } | |
| 231 | |||
| 232 | 277 | boost::optional<LogLevel> LogSink::logLevel() const { | |
| 233 | 277 | return m_impl->logLevel(); | |
| 234 | } | ||
| 235 | |||
| 236 | 13766 | void LogSink::setLogLevel(LogLevel logLevel) { | |
| 237 | 13766 | m_impl->setLogLevel(logLevel); | |
| 238 | 13766 | } | |
| 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 | 1797 | void LogSink::setChannelRegex(const boost::regex& channelRegex) { | |
| 249 | 1797 | m_impl->setChannelRegex(channelRegex); | |
| 250 | 1797 | } | |
| 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 | 2354 | void LogSink::setThreadId(const std::thread::id threadId) { | |
| 273 | 2354 | m_impl->setThreadId(threadId); | |
| 274 | 2354 | } | |
| 275 | |||
| 276 | ✗ | void LogSink::resetThreadId() { | |
| 277 | ✗ | m_impl->resetThreadId(); | |
| 278 | ✗ | } | |
| 279 | |||
| 280 | 8662 | void LogSink::setStream(boost::shared_ptr<std::ostream> os) { | |
| 281 |
1/1✓ Branch 3 taken 8662 times.
|
8662 | m_impl->setStream(os); |
| 282 | 8662 | } | |
| 283 | |||
| 284 | 4331 | boost::shared_ptr<LogSinkBackend> LogSink::sink() const { | |
| 285 | 4331 | 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 |