Mercurial > hg > orthanc
comparison Core/Logging.cpp @ 3995:1f405a3fdeca
reorganizing Logging
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 05 Jun 2020 18:08:17 +0200 |
parents | 2a170a8f1faf |
children | c2b9a7a1c74a |
comparison
equal
deleted
inserted
replaced
3993:7e8bfda62b43 | 3995:1f405a3fdeca |
---|---|
83 } | 83 } |
84 } | 84 } |
85 } | 85 } |
86 | 86 |
87 | 87 |
88 #elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1 | |
89 | |
90 /********************************************************* | |
91 * Logger compatible with the Orthanc plugin SDK | |
92 *********************************************************/ | |
93 | |
94 #include <boost/lexical_cast.hpp> | |
95 #include <sstream> | |
96 | |
97 namespace Orthanc | |
98 { | |
99 namespace Logging | |
100 { | |
101 static OrthancPluginContext* context_ = NULL; | |
102 | |
103 void Initialize(OrthancPluginContext* context) | |
104 { | |
105 context_ = context; | |
106 } | |
107 | |
108 InternalLogger::InternalLogger(InternalLevel level, | |
109 const char* file /* ignored */, | |
110 int line /* ignored */) : | |
111 level_(level) | |
112 { | |
113 } | |
114 | |
115 InternalLogger::~InternalLogger() | |
116 { | |
117 std::string message = messageStream_.str(); | |
118 if (context_ != NULL) | |
119 { | |
120 switch (level_) | |
121 { | |
122 case InternalLevel_ERROR: | |
123 OrthancPluginLogError(context_, message.c_str()); | |
124 break; | |
125 | |
126 case InternalLevel_WARNING: | |
127 OrthancPluginLogWarning(context_, message.c_str()); | |
128 break; | |
129 | |
130 case InternalLevel_INFO: | |
131 OrthancPluginLogInfo(context_, message.c_str()); | |
132 break; | |
133 | |
134 case InternalLevel_TRACE: | |
135 // Not used by plugins | |
136 break; | |
137 | |
138 default: | |
139 { | |
140 std::string s = ("Unknown log level (" + boost::lexical_cast<std::string>(level_) + | |
141 ") for message: " + message); | |
142 OrthancPluginLogError(context_, s.c_str()); | |
143 break; | |
144 } | |
145 } | |
146 } | |
147 } | |
148 } | |
149 } | |
150 | |
151 | |
152 #elif ORTHANC_ENABLE_LOGGING_STDIO == 1 | 88 #elif ORTHANC_ENABLE_LOGGING_STDIO == 1 |
153 | 89 |
154 /********************************************************* | 90 /********************************************************* |
155 * Logger compatible with <stdio.h> OR logger that sends its | 91 * Logger compatible with <stdio.h> OR logger that sends its |
156 * output to the emscripten html5 api (depending on the | 92 * output to the emscripten html5 api (depending on the |
170 { | 106 { |
171 static bool globalVerbose_ = false; | 107 static bool globalVerbose_ = false; |
172 static bool globalTrace_ = false; | 108 static bool globalTrace_ = false; |
173 | 109 |
174 #ifdef __EMSCRIPTEN__ | 110 #ifdef __EMSCRIPTEN__ |
175 void defaultErrorLogFunc(const char* msg) | 111 static void defaultErrorLogFunc(const char* msg) |
176 { | 112 { |
177 emscripten_console_error(msg); | 113 emscripten_console_error(msg); |
178 } | 114 } |
179 | 115 |
180 void defaultWarningLogFunc(const char* msg) | 116 static void defaultWarningLogFunc(const char* msg) |
181 { | 117 { |
182 emscripten_console_warn(msg); | 118 emscripten_console_warn(msg); |
183 } | 119 } |
184 | 120 |
185 void defaultInfoLogFunc(const char* msg) | 121 static void defaultInfoLogFunc(const char* msg) |
186 { | 122 { |
187 emscripten_console_log(msg); | 123 emscripten_console_log(msg); |
188 } | 124 } |
189 | 125 |
190 void defaultTraceLogFunc(const char* msg) | 126 static void defaultTraceLogFunc(const char* msg) |
191 { | 127 { |
192 emscripten_console_log(msg); | 128 emscripten_console_log(msg); |
193 } | 129 } |
194 #else | 130 #else |
195 // __EMSCRIPTEN__ not #defined | 131 // __EMSCRIPTEN__ not #defined |
196 void defaultErrorLogFunc(const char* msg) | 132 static void defaultErrorLogFunc(const char* msg) |
197 { | 133 { |
198 fprintf(stderr, "E: %s\n", msg); | 134 fprintf(stderr, "E: %s\n", msg); |
199 } | 135 } |
200 | 136 |
201 void defaultWarningLogFunc(const char*) | 137 static void defaultWarningLogFunc(const char*) |
202 { | 138 { |
203 fprintf(stdout, "W: %s\n", msg); | 139 fprintf(stdout, "W: %s\n", msg); |
204 } | 140 } |
205 | 141 |
206 void defaultInfoLogFunc(const char*) | 142 static void defaultInfoLogFunc(const char*) |
207 { | 143 { |
208 fprintf(stdout, "I: %s\n", msg); | 144 fprintf(stdout, "I: %s\n", msg); |
209 } | 145 } |
210 | 146 |
211 void defaultTraceLogFunc(const char*) | 147 static void defaultTraceLogFunc(const char*) |
212 { | 148 { |
213 fprintf(stdout, "T: %s\n", msg); | 149 fprintf(stdout, "T: %s\n", msg); |
214 } | 150 } |
215 #endif | 151 #endif |
216 // __EMSCRIPTEN__ | 152 // __EMSCRIPTEN__ |
218 static LoggingFunction globalErrorLogFunc = defaultErrorLogFunc; | 154 static LoggingFunction globalErrorLogFunc = defaultErrorLogFunc; |
219 static LoggingFunction globalWarningLogFunc = defaultWarningLogFunc; | 155 static LoggingFunction globalWarningLogFunc = defaultWarningLogFunc; |
220 static LoggingFunction globalInfoLogFunc = defaultInfoLogFunc; | 156 static LoggingFunction globalInfoLogFunc = defaultInfoLogFunc; |
221 static LoggingFunction globalTraceLogFunc = defaultTraceLogFunc; | 157 static LoggingFunction globalTraceLogFunc = defaultTraceLogFunc; |
222 | 158 |
223 void SetErrorWarnInfoTraceLoggingFunctions( | 159 void SetErrorWarnInfoTraceLoggingFunctions(LoggingFunction errorLogFunc, |
224 LoggingFunction errorLogFunc, | 160 LoggingFunction warningLogfunc, |
225 LoggingFunction warningLogfunc, | 161 LoggingFunction infoLogFunc, |
226 LoggingFunction infoLogFunc, | 162 LoggingFunction traceLogFunc) |
227 LoggingFunction traceLogFunc) | |
228 { | 163 { |
229 globalErrorLogFunc = errorLogFunc; | 164 globalErrorLogFunc = errorLogFunc; |
230 globalWarningLogFunc = warningLogfunc; | 165 globalWarningLogFunc = warningLogfunc; |
231 globalInfoLogFunc = infoLogFunc; | 166 globalInfoLogFunc = infoLogFunc; |
232 globalTraceLogFunc = traceLogFunc; | 167 globalTraceLogFunc = traceLogFunc; |
280 | 215 |
281 void Initialize() | 216 void Initialize() |
282 { | 217 { |
283 } | 218 } |
284 | 219 |
220 void Finalize() | |
221 { | |
222 } | |
223 | |
224 void Reset() | |
225 { | |
226 } | |
227 | |
228 void Flush() | |
229 { | |
230 } | |
231 | |
285 void EnableInfoLevel(bool enabled) | 232 void EnableInfoLevel(bool enabled) |
286 { | 233 { |
287 globalVerbose_ = enabled; | 234 globalVerbose_ = enabled; |
288 } | 235 } |
289 | 236 |
300 bool IsTraceLevelEnabled() | 247 bool IsTraceLevelEnabled() |
301 { | 248 { |
302 return globalTrace_; | 249 return globalTrace_; |
303 } | 250 } |
304 | 251 |
252 void SetTargetFile(const std::string& path) | |
253 { | |
254 } | |
255 | |
256 void SetTargetFolder(const std::string& path) | |
257 { | |
258 } | |
259 } | |
260 } | |
261 | |
262 | |
263 #elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1 | |
264 | |
265 /********************************************************* | |
266 * Logger compatible with the Orthanc plugin SDK | |
267 *********************************************************/ | |
268 | |
269 #include <cassert> | |
270 | |
271 namespace | |
272 { | |
273 /** | |
274 * This is minimal implementation of the context for an Orthanc | |
275 * plugin, limited to the logging facilities, and that is binary | |
276 * compatible with the definitions of "OrthancCPlugin.h" | |
277 **/ | |
278 typedef enum | |
279 { | |
280 _OrthancPluginService_LogInfo = 1, | |
281 _OrthancPluginService_LogWarning = 2, | |
282 _OrthancPluginService_LogError = 3, | |
283 _OrthancPluginService_INTERNAL = 0x7fffffff | |
284 } _OrthancPluginService; | |
285 | |
286 typedef struct _OrthancPluginContext_t | |
287 { | |
288 void* pluginsManager; | |
289 const char* orthancVersion; | |
290 void (*Free) (void* buffer); | |
291 int32_t (*InvokeService) (struct _OrthancPluginContext_t* context, | |
292 _OrthancPluginService service, | |
293 const void* params); | |
294 } OrthancPluginContext; | |
295 } | |
296 | |
297 | |
298 #include <boost/lexical_cast.hpp> | |
299 #include <sstream> | |
300 | |
301 namespace Orthanc | |
302 { | |
303 namespace Logging | |
304 { | |
305 static OrthancPluginContext* context_ = NULL; | |
306 | |
307 void Initialize(void* context) | |
308 { | |
309 assert(sizeof(_OrthancPluginService) == sizeof(int32_t)); | |
310 context_ = reinterpret_cast<OrthancPluginContext*>(context); | |
311 } | |
312 | |
313 InternalLogger::InternalLogger(InternalLevel level, | |
314 const char* file /* ignored */, | |
315 int line /* ignored */) : | |
316 level_(level) | |
317 { | |
318 } | |
319 | |
320 InternalLogger::~InternalLogger() | |
321 { | |
322 std::string message = messageStream_.str(); | |
323 if (context_ != NULL) | |
324 { | |
325 switch (level_) | |
326 { | |
327 case InternalLevel_ERROR: | |
328 context_->InvokeService(context_, _OrthancPluginService_LogError, message.c_str()); | |
329 break; | |
330 | |
331 case InternalLevel_WARNING: | |
332 context_->InvokeService(context_, _OrthancPluginService_LogWarning, message.c_str()); | |
333 break; | |
334 | |
335 case InternalLevel_INFO: | |
336 context_->InvokeService(context_, _OrthancPluginService_LogInfo, message.c_str()); | |
337 break; | |
338 | |
339 case InternalLevel_TRACE: | |
340 // Not used by plugins | |
341 break; | |
342 | |
343 default: | |
344 { | |
345 std::string s = ("Unknown log level (" + boost::lexical_cast<std::string>(level_) + | |
346 ") for message: " + message); | |
347 context_->InvokeService(context_, _OrthancPluginService_LogInfo, s.c_str()); | |
348 break; | |
349 } | |
350 } | |
351 } | |
352 } | |
305 } | 353 } |
306 } | 354 } |
307 | 355 |
308 | 356 |
309 #else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && | 357 #else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && |