GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 1 / 0 / 1
Functions: 100.0% 1 / 0 / 1
Branches: -% 0 / 0 / 0

src/utilities/core/Logger.hpp
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 #ifndef UTILITIES_CORE_LOGGER_HPP
7 #define UTILITIES_CORE_LOGGER_HPP
8
9 #include "../UtilitiesAPI.hpp"
10
11 #include "Compare.hpp"
12 #include "LogMessage.hpp"
13 #include "LogSink.hpp"
14 #include "Exception.hpp"
15
16 #include <boost/shared_ptr.hpp>
17
18 #include <map>
19 #include <memory>
20 #include <set>
21 #include <shared_mutex>
22 #include <sstream>
23
24 /// defines method logChannel() to get a logger for a class
25 #define REGISTER_LOGGER(__logChannel__) \
26 static openstudio::LogChannel logChannel() { \
27 return __logChannel__; \
28 }
29
30 /// log a message from within a registered class
31 #define LOG(__level__, __message__) LOG_FREE(__level__, logChannel(), __message__);
32
33 /// log a message from within a registered class and throw an exception
34 #define LOG_AND_THROW(__message__) LOG_FREE_AND_THROW(logChannel(), __message__);
35
36 /// log a message from outside a registered class
37 #define LOG_FREE(__level__, __channel__, __message__) \
38 { \
39 std::stringstream _ss1; \
40 _ss1 << __message__; \
41 openstudio::logFree(__level__, __channel__, _ss1.str()); \
42 }
43
44 /// log a message from outside a registered class and throw an exception
45 #define LOG_FREE_AND_THROW(__channel__, __message__) \
46 { \
47 LOG_FREE(Fatal, __channel__, __message__); \
48 std::stringstream _ss2; \
49 _ss2 << __FILE__ << "@" << __LINE__ << " : " << __message__; \
50 throw openstudio::Exception(_ss2.str()); \
51 }
52
53 namespace openstudio {
54
55 class OSWorkflow;
56 class LogSink;
57 namespace detail {
58 class LogSink_Impl;
59 } // namespace detail
60
61 /// convenience function for SWIG, prefer macros in C++
62 UTILITIES_API void logFree(LogLevel level, const std::string& channel, const std::string& message);
63
64 class UTILITIES_API Logger
65 {
66 public:
67 static Logger& instance();
68
69 Logger(const Logger& other) = delete;
70 Logger(Logger&& other) = delete;
71 Logger& operator=(const Logger&) = delete;
72 Logger& operator=(Logger&&) = delete;
73
74 /// get logger for standard out
75 LogSink standardOutLogger() const;
76
77 /// get logger for standard error
78 LogSink standardErrLogger() const;
79
80 /// get a logger from a LogChannel enumeration, if logChannel does not
81 /// exist a new logger will be set up at the default level
82 LoggerType& loggerFromChannel(const LogChannel& logChannel);
83
84 protected:
85 friend class detail::LogSink_Impl;
86 friend class openstudio::OSWorkflow;
87
88 /// is the sink found in the logging core
89 bool findSink(boost::shared_ptr<LogSinkBackend> sink);
90
91 /// adds a sink to the logging core, equivalent to logSink.enable()
92 void addSink(boost::shared_ptr<LogSinkBackend> sink);
93
94 /// removes a sink to the logging core, equivalent to logSink.disable()
95 void removeSink(boost::shared_ptr<LogSinkBackend> sink);
96
97 // cppcheck-suppress functionConst
98 void addTimeStampToLogger();
99
100 private:
101 Logger();
102 4379 ~Logger() = default;
103
104 mutable std::shared_mutex m_mutex;
105
106 /// standard out logger
107 LogSink m_standardOutLogger;
108
109 /// standard err logger
110 LogSink m_standardErrLogger;
111
112 /// map of std::string to logger
113 using LoggerMapType = std::map<std::string, LoggerType, openstudio::IstringCompare>;
114 LoggerMapType m_loggerMap;
115
116 /// current sinks, kept here so don't destruct when LogSink wrapper goes out of scope
117 using SinkSetType = std::set<boost::shared_ptr<LogSinkBackend>>;
118 SinkSetType m_sinks;
119 };
120
121 } // namespace openstudio
122
123 #endif // UTILITIES_CORE_LOGGER_HPP
124