comparison Core/Logging.h @ 4014:27628b0f6ada

merging logging code for plugins and files
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 08 Jun 2020 17:03:25 +0200
parents f0ee3f1db775
children c783f4f29390
comparison
equal deleted inserted replaced
4013:1e9f6d706237 4014:27628b0f6ada
40 40
41 #if !defined(ORTHANC_ENABLE_LOGGING) 41 #if !defined(ORTHANC_ENABLE_LOGGING)
42 # error The macro ORTHANC_ENABLE_LOGGING must be defined 42 # error The macro ORTHANC_ENABLE_LOGGING must be defined
43 #endif 43 #endif
44 44
45 #if !defined(ORTHANC_ENABLE_LOGGING_PLUGIN)
46 # if ORTHANC_ENABLE_LOGGING == 1
47 # error The macro ORTHANC_ENABLE_LOGGING_PLUGIN must be defined
48 # else
49 # define ORTHANC_ENABLE_LOGGING_PLUGIN 0
50 # endif
51 #endif
52
53 #if !defined(ORTHANC_ENABLE_LOGGING_STDIO) 45 #if !defined(ORTHANC_ENABLE_LOGGING_STDIO)
54 # if ORTHANC_ENABLE_LOGGING == 1 46 # if ORTHANC_ENABLE_LOGGING == 1
55 # error The macro ORTHANC_ENABLE_LOGGING_STDIO must be defined 47 # error The macro ORTHANC_ENABLE_LOGGING_STDIO must be defined
56 # else 48 # else
57 # define ORTHANC_ENABLE_LOGGING_STDIO 0 49 # define ORTHANC_ENABLE_LOGGING_STDIO 0
58 # endif 50 # endif
59 #endif 51 #endif
60 52
61 #include <boost/lexical_cast.hpp>
62 53
63 namespace Orthanc 54 namespace Orthanc
64 { 55 {
65 namespace Logging 56 namespace Logging
66 { 57 {
74 65
75 ORTHANC_PUBLIC const char* EnumerationToString(LogLevel level); 66 ORTHANC_PUBLIC const char* EnumerationToString(LogLevel level);
76 67
77 ORTHANC_PUBLIC LogLevel StringToLogLevel(const char* level); 68 ORTHANC_PUBLIC LogLevel StringToLogLevel(const char* level);
78 69
79 #if ORTHANC_ENABLE_LOGGING_PLUGIN == 1
80 // "pluginContext" must be of type "OrthancPluginContext" 70 // "pluginContext" must be of type "OrthancPluginContext"
81 ORTHANC_PUBLIC void Initialize(void* pluginContext); 71 ORTHANC_PUBLIC void InitializePluginContext(void* pluginContext);
82 #else 72
83 ORTHANC_PUBLIC void Initialize(); 73 ORTHANC_PUBLIC void Initialize();
84 #endif
85 74
86 ORTHANC_PUBLIC void Finalize(); 75 ORTHANC_PUBLIC void Finalize();
87 76
88 ORTHANC_PUBLIC void Reset(); 77 ORTHANC_PUBLIC void Reset();
89 78
118 } 107 }
119 } 108 }
120 109
121 110
122 #if ORTHANC_ENABLE_LOGGING != 1 111 #if ORTHANC_ENABLE_LOGGING != 1
123 # define LOG(level) ::Orthanc::Logging::NullStream() 112 #define LOG(level) ::Orthanc::Logging::NullStream()
124 # define VLOG(level) ::Orthanc::Logging::NullStream() 113 #define VLOG(level) ::Orthanc::Logging::NullStream()
125 #else 114
126 # define LOG(level) ::Orthanc::Logging::InternalLogger \ 115 #else /* ORTHANC_ENABLE_LOGGING == 1 */
116
117 #define LOG(level) ::Orthanc::Logging::InternalLogger \
127 (::Orthanc::Logging::LogLevel_ ## level, __FILE__, __LINE__) 118 (::Orthanc::Logging::LogLevel_ ## level, __FILE__, __LINE__)
128 # define VLOG(level) ::Orthanc::Logging::InternalLogger \ 119 #define VLOG(level) ::Orthanc::Logging::InternalLogger \
129 (::Orthanc::Logging::LogLevel_TRACE, __FILE__, __LINE__) 120 (::Orthanc::Logging::LogLevel_TRACE, __FILE__, __LINE__)
130 #endif
131 121
122 #include "Compatibility.h" // For std::unique_ptr<>
132 123
133 #if (ORTHANC_ENABLE_LOGGING == 1 && \ 124 #include <boost/lexical_cast.hpp>
134 (ORTHANC_ENABLE_LOGGING_PLUGIN == 1 || \
135 ORTHANC_ENABLE_LOGGING_STDIO == 1))
136
137 #include <boost/noncopyable.hpp> 125 #include <boost/noncopyable.hpp>
126 #include <boost/thread/mutex.hpp>
127 #include <sstream>
138 128
139 namespace Orthanc 129 namespace Orthanc
140 { 130 {
141 namespace Logging 131 namespace Logging
142 { 132 {
143 class ORTHANC_PUBLIC InternalLogger : public boost::noncopyable 133 class ORTHANC_PUBLIC InternalLogger : public boost::noncopyable
144 { 134 {
145 private: 135 private:
146 LogLevel level_; 136 boost::mutex::scoped_lock lock_;
147 std::stringstream messageStream_; 137 LogLevel level_;
148 138 std::unique_ptr<std::stringstream> pluginStream_;
149 public: 139 std::ostream* stream_;
150 InternalLogger(LogLevel level,
151 const char* file,
152 int line);
153
154 ~InternalLogger();
155
156 template <typename T>
157 std::ostream& operator<< (const T& message)
158 {
159 messageStream_ << message;
160 return messageStream_;
161 }
162 };
163 }
164 }
165
166 #endif
167
168
169
170
171 #if (ORTHANC_ENABLE_LOGGING == 1 && \
172 ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && \
173 ORTHANC_ENABLE_LOGGING_STDIO == 0)
174
175 #include <boost/thread/mutex.hpp>
176
177 namespace Orthanc
178 {
179 namespace Logging
180 {
181 class ORTHANC_PUBLIC InternalLogger
182 {
183 private:
184 boost::mutex::scoped_lock lock_;
185 NullStream null_;
186 std::ostream* stream_;
187 140
188 public: 141 public:
189 InternalLogger(LogLevel level, 142 InternalLogger(LogLevel level,
190 const char* file, 143 const char* file,
191 int line); 144 int line);
209 * long as logging calls are performed. In order to prevent 162 * long as logging calls are performed. In order to prevent
210 * dangling pointer usage, it is mandatory to call 163 * dangling pointer usage, it is mandatory to call
211 * Orthanc::Logging::Reset() before the stream objects are 164 * Orthanc::Logging::Reset() before the stream objects are
212 * destroyed and the references become invalid. 165 * destroyed and the references become invalid.
213 * 166 *
214 * This function must only be used by unit tests. 167 * This function must only be used by unit tests. It is ignored if
168 * InitializePluginContext() was called.
215 **/ 169 **/
216 ORTHANC_PUBLIC void SetErrorWarnInfoLoggingStreams(std::ostream& errorStream, 170 ORTHANC_PUBLIC void SetErrorWarnInfoLoggingStreams(std::ostream& errorStream,
217 std::ostream& warningStream, 171 std::ostream& warningStream,
218 std::ostream& infoStream); 172 std::ostream& infoStream);
219 } 173 }