GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 64 / 0 / 64
Functions: 100.0% 10 / 0 / 10
Branches: 95.5% 64 / 0 / 67

src/utilities/core/Logger.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 "Logger.hpp"
7
8 #include <boost/log/common.hpp>
9 #include <boost/log/attributes/function.hpp>
10 #include <boost/log/attributes/clock.hpp>
11
12 #include <boost/core/null_deleter.hpp>
13
14 #include <utility>
15
16 namespace openstudio {
17
18 /// convenience function for SWIG, prefer macros in C++
19 1874395 void logFree(LogLevel level, const std::string& channel, const std::string& message) {
20
11/11
✓ Branch 1 taken 1874395 times.
✓ Branch 4 taken 1874395 times.
✓ Branch 7 taken 1874395 times.
✓ Branch 10 taken 1874395 times.
✓ Branch 13 taken 408215 times.
✓ Branch 16 taken 408215 times.
✓ Branch 19 taken 408215 times.
✓ Branch 23 taken 408215 times.
✓ Branch 26 taken 408215 times.
✓ Branch 29 taken 408215 times.
✓ Branch 30 taken 1874395 times.
2282610 BOOST_LOG_SEV(openstudio::Logger::instance().loggerFromChannel(channel), level) << message;
21 1874395 }
22
23 // Meyers' singleton
24 2299857 Logger& Logger::instance() {
25
4/7
✓ Branch 0 taken 4331 times.
✓ Branch 1 taken 2295526 times.
✓ Branch 3 taken 4331 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4331 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
2299857 static Logger instance;
26 2299857 return instance;
27 }
28
29
1/1
✓ Branch 3 taken 4331 times.
4331 Logger::Logger() {
30 // Make current thread id attribute available to logging
31
4/4
✓ Branch 1 taken 4331 times.
✓ Branch 5 taken 4331 times.
✓ Branch 8 taken 4331 times.
✓ Branch 11 taken 4331 times.
4331 boost::log::core::get()->add_global_attribute("ThreadId", boost::log::attributes::make_function(&std::this_thread::get_id));
32
33 // We have to provide an null deleter to avoid destroying the global stream
34
1/1
✓ Branch 1 taken 4331 times.
4331 boost::shared_ptr<std::ostream> stdOut(&std::cout, boost::null_deleter());
35
1/1
✓ Branch 2 taken 4331 times.
4331 m_standardOutLogger.setStream(stdOut);
36
1/1
✓ Branch 1 taken 4331 times.
4331 m_standardOutLogger.setLogLevel(Warn);
37
2/2
✓ Branch 1 taken 4331 times.
✓ Branch 4 taken 4331 times.
4331 this->addSink(m_standardOutLogger.sink());
38
39 // We have to provide an empty deleter to avoid destroying the global stream
40
1/1
✓ Branch 1 taken 4331 times.
4331 boost::shared_ptr<std::ostream> stdErr(&std::cerr, boost::null_deleter());
41
1/1
✓ Branch 2 taken 4331 times.
4331 m_standardErrLogger.setStream(stdErr);
42
1/1
✓ Branch 1 taken 4331 times.
4331 m_standardErrLogger.setLogLevel(Warn);
43 //this->addSink(m_standardErrLogger.sink());
44 4331 }
45
46 3814 LogSink Logger::standardOutLogger() const {
47
1/1
✓ Branch 1 taken 3814 times.
3814 std::shared_lock l{m_mutex};
48
49 7628 return m_standardOutLogger;
50 3814 }
51
52 6 LogSink Logger::standardErrLogger() const {
53
1/1
✓ Branch 1 taken 6 times.
6 std::shared_lock l{m_mutex};
54
55 12 return m_standardErrLogger;
56 6 }
57
58 2282610 LoggerType& Logger::loggerFromChannel(const LogChannel& logChannel) {
59
1/1
✓ Branch 1 taken 2282610 times.
2282610 std::shared_lock l{m_mutex};
60
61
1/1
✓ Branch 1 taken 2282610 times.
2282610 auto it = m_loggerMap.find(logChannel);
62
2/2
✓ Branch 2 taken 7654 times.
✓ Branch 3 taken 2274956 times.
2282610 if (it == m_loggerMap.end()) {
63 // Drop the read lock and grab a write lock - we need to add the new file to the map
64 // this will reduce contention when multiple threads trying to log at once.
65
1/1
✓ Branch 1 taken 7654 times.
7654 l.unlock();
66
1/1
✓ Branch 1 taken 7654 times.
7654 std::unique_lock l2{m_mutex};
67
68 //LoggerType newLogger(boost::log::keywords::channel = logChannel, boost::log::keywords::severity = Debug);
69 // No need for try_emplace, this we checked it wasn't in the map
70
3/3
✓ Branch 1 taken 7654 times.
✓ Branch 4 taken 7654 times.
✓ Branch 7 taken 7654 times.
7654 auto [it2, inserted] = m_loggerMap.emplace(logChannel, LoggerType(boost::log::keywords::channel = logChannel));
71
72 7654 return it2->second;
73 7654 }
74
75 2274956 return it->second;
76 2282610 }
77
78 3 bool Logger::findSink(boost::shared_ptr<LogSinkBackend> sink) {
79
1/1
✓ Branch 1 taken 3 times.
3 std::unique_lock l{m_mutex};
80
81
1/1
✓ Branch 1 taken 3 times.
3 auto it = m_sinks.find(sink);
82
83 6 return (it != m_sinks.end());
84 3 }
85
86 9418 void Logger::addSink(boost::shared_ptr<LogSinkBackend> sink) {
87
1/1
✓ Branch 1 taken 9418 times.
9418 std::shared_lock l{m_mutex};
88
89
1/1
✓ Branch 1 taken 9418 times.
9418 auto it = m_sinks.find(sink);
90
2/2
✓ Branch 2 taken 9416 times.
✓ Branch 3 taken 2 times.
9418 if (it == m_sinks.end()) {
91
92 // Drop the read lock and grab a write lock - we need to add the new file to the map
93 // this will reduce contention when multiple threads trying to log at once.
94
1/1
✓ Branch 1 taken 9416 times.
9416 l.unlock();
95
1/1
✓ Branch 1 taken 9416 times.
9416 std::unique_lock l2{m_mutex};
96
97
1/1
✓ Branch 1 taken 9416 times.
9416 m_sinks.insert(sink);
98
99 // Register the sink in the logging core
100
2/2
✓ Branch 1 taken 9416 times.
✓ Branch 6 taken 9416 times.
9416 boost::log::core::get()->add_sink(sink);
101 9416 }
102 9418 }
103
104 8289 void Logger::removeSink(boost::shared_ptr<LogSinkBackend> sink) {
105
1/1
✓ Branch 1 taken 8289 times.
8289 std::shared_lock l{m_mutex};
106
107
1/1
✓ Branch 1 taken 8289 times.
8289 auto it = m_sinks.find(sink);
108
2/2
✓ Branch 2 taken 8283 times.
✓ Branch 3 taken 6 times.
8289 if (it != m_sinks.end()) {
109
110 // Drop the read lock and grab a write lock - we need to add the new file to the map
111 // this will reduce contention when multiple threads trying to log at once.
112
1/1
✓ Branch 1 taken 8283 times.
8283 l.unlock();
113
1/1
✓ Branch 1 taken 8283 times.
8283 std::unique_lock l2{m_mutex};
114
115
1/1
✓ Branch 1 taken 8283 times.
8283 m_sinks.erase(it);
116
117 // Register the sink in the logging core
118
2/2
✓ Branch 1 taken 8283 times.
✓ Branch 6 taken 8283 times.
8283 boost::log::core::get()->remove_sink(sink);
119 8283 }
120 8289 }
121
122 44 void Logger::addTimeStampToLogger() {
123
1/1
✓ Branch 1 taken 44 times.
44 std::unique_lock l{m_mutex};
124
125 // Add a TimeStamp attribute, same as boost::log::add_common_attributes() would do
126
4/4
✓ Branch 1 taken 44 times.
✓ Branch 5 taken 44 times.
✓ Branch 8 taken 44 times.
✓ Branch 11 taken 44 times.
44 [[maybe_unused]] auto [it, inserted] = boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::local_clock{});
127 // if (!use) {
128 // boost::log::core::get()->remove_global_attribute(it);
129 // }
130 44 }
131
132 } // namespace openstudio
133