Mercurial > hg > orthanc
comparison Core/Logging.h @ 4023:cbdf62468d77 more-changes
merge
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Tue, 09 Jun 2020 11:54:58 +0200 |
parents | c783f4f29390 |
children |
comparison
equal
deleted
inserted
replaced
4022:a2e4edc7b9aa | 4023:cbdf62468d77 |
---|---|
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 |
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 #else /* ORTHANC_ENABLE_LOGGING == 1 */ |
126 # define LOG(level) ::Orthanc::Logging::InternalLogger \ | 115 # define LOG(level) ::Orthanc::Logging::InternalLogger \ |
127 (::Orthanc::Logging::LogLevel_ ## level, __FILE__, __LINE__) | 116 (::Orthanc::Logging::LogLevel_ ## level, __FILE__, __LINE__) |
128 # define VLOG(level) ::Orthanc::Logging::InternalLogger \ | 117 # define VLOG(level) ::Orthanc::Logging::InternalLogger \ |
129 (::Orthanc::Logging::LogLevel_TRACE, __FILE__, __LINE__) | 118 (::Orthanc::Logging::LogLevel_TRACE, __FILE__, __LINE__) |
130 #endif | 119 #endif |
131 | 120 |
132 | 121 |
122 | |
133 #if (ORTHANC_ENABLE_LOGGING == 1 && \ | 123 #if (ORTHANC_ENABLE_LOGGING == 1 && \ |
134 (ORTHANC_ENABLE_LOGGING_PLUGIN == 1 || \ | 124 ORTHANC_ENABLE_LOGGING_STDIO == 1) |
135 ORTHANC_ENABLE_LOGGING_STDIO == 1)) | 125 // This is notably for WebAssembly |
136 | 126 |
127 #include <boost/lexical_cast.hpp> | |
137 #include <boost/noncopyable.hpp> | 128 #include <boost/noncopyable.hpp> |
129 #include <sstream> | |
138 | 130 |
139 namespace Orthanc | 131 namespace Orthanc |
140 { | 132 { |
141 namespace Logging | 133 namespace Logging |
142 { | 134 { |
146 LogLevel level_; | 138 LogLevel level_; |
147 std::stringstream messageStream_; | 139 std::stringstream messageStream_; |
148 | 140 |
149 public: | 141 public: |
150 InternalLogger(LogLevel level, | 142 InternalLogger(LogLevel level, |
151 const char* file, | 143 const char* file /* ignored */, |
152 int line); | 144 int line /* ignored */) : |
145 level_(level) | |
146 { | |
147 } | |
153 | 148 |
154 ~InternalLogger(); | 149 ~InternalLogger(); |
155 | 150 |
156 template <typename T> | 151 template <typename T> |
157 std::ostream& operator<< (const T& message) | 152 std::ostream& operator<< (const T& message) |
158 { | 153 { |
159 messageStream_ << message; | 154 return messageStream_ << boost::lexical_cast<std::string>(message); |
160 return messageStream_; | |
161 } | 155 } |
162 }; | 156 }; |
163 } | 157 } |
164 } | 158 } |
165 | 159 |
166 #endif | 160 #endif |
167 | 161 |
168 | 162 |
169 | 163 |
170 | |
171 #if (ORTHANC_ENABLE_LOGGING == 1 && \ | 164 #if (ORTHANC_ENABLE_LOGGING == 1 && \ |
172 ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && \ | |
173 ORTHANC_ENABLE_LOGGING_STDIO == 0) | 165 ORTHANC_ENABLE_LOGGING_STDIO == 0) |
174 | 166 |
167 #include "Compatibility.h" // For std::unique_ptr<> | |
168 | |
169 #include <boost/lexical_cast.hpp> | |
170 #include <boost/noncopyable.hpp> | |
175 #include <boost/thread/mutex.hpp> | 171 #include <boost/thread/mutex.hpp> |
172 #include <sstream> | |
176 | 173 |
177 namespace Orthanc | 174 namespace Orthanc |
178 { | 175 { |
179 namespace Logging | 176 namespace Logging |
180 { | 177 { |
181 class ORTHANC_PUBLIC InternalLogger | 178 class ORTHANC_PUBLIC InternalLogger : public boost::noncopyable |
182 { | 179 { |
183 private: | 180 private: |
184 boost::mutex::scoped_lock lock_; | 181 boost::mutex::scoped_lock lock_; |
185 NullStream null_; | 182 LogLevel level_; |
186 std::ostream* stream_; | 183 std::unique_ptr<std::stringstream> pluginStream_; |
184 std::ostream* stream_; | |
187 | 185 |
188 public: | 186 public: |
189 InternalLogger(LogLevel level, | 187 InternalLogger(LogLevel level, |
190 const char* file, | 188 const char* file, |
191 int line); | 189 int line); |
209 * long as logging calls are performed. In order to prevent | 207 * long as logging calls are performed. In order to prevent |
210 * dangling pointer usage, it is mandatory to call | 208 * dangling pointer usage, it is mandatory to call |
211 * Orthanc::Logging::Reset() before the stream objects are | 209 * Orthanc::Logging::Reset() before the stream objects are |
212 * destroyed and the references become invalid. | 210 * destroyed and the references become invalid. |
213 * | 211 * |
214 * This function must only be used by unit tests. | 212 * This function must only be used by unit tests. It is ignored if |
213 * InitializePluginContext() was called. | |
215 **/ | 214 **/ |
216 ORTHANC_PUBLIC void SetErrorWarnInfoLoggingStreams(std::ostream& errorStream, | 215 ORTHANC_PUBLIC void SetErrorWarnInfoLoggingStreams(std::ostream& errorStream, |
217 std::ostream& warningStream, | 216 std::ostream& warningStream, |
218 std::ostream& infoStream); | 217 std::ostream& infoStream); |
219 } | 218 } |