Mercurial > hg > orthanc-stone
comparison Resources/Orthanc/Core/Logging.cpp @ 200:03afbee0cc7b
integration of Orthanc core into Stone
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 23 Mar 2018 11:04:03 +0100 |
parents | |
children | e7f90aba3c97 |
comparison
equal
deleted
inserted
replaced
199:dabe9982fca3 | 200:03afbee0cc7b |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "PrecompiledHeaders.h" | |
35 #include "Logging.h" | |
36 | |
37 #if ORTHANC_ENABLE_LOGGING != 1 | |
38 | |
39 namespace Orthanc | |
40 { | |
41 namespace Logging | |
42 { | |
43 void Initialize() | |
44 { | |
45 } | |
46 | |
47 void Finalize() | |
48 { | |
49 } | |
50 | |
51 void Reset() | |
52 { | |
53 } | |
54 | |
55 void Flush() | |
56 { | |
57 } | |
58 | |
59 void EnableInfoLevel(bool enabled) | |
60 { | |
61 } | |
62 | |
63 void EnableTraceLevel(bool enabled) | |
64 { | |
65 } | |
66 | |
67 void SetTargetFile(const std::string& path) | |
68 { | |
69 } | |
70 | |
71 void SetTargetFolder(const std::string& path) | |
72 { | |
73 } | |
74 } | |
75 } | |
76 | |
77 | |
78 #elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1 | |
79 | |
80 /********************************************************* | |
81 * Logger compatible with the Orthanc plugin SDK | |
82 *********************************************************/ | |
83 | |
84 #include <boost/lexical_cast.hpp> | |
85 | |
86 namespace Orthanc | |
87 { | |
88 namespace Logging | |
89 { | |
90 static OrthancPluginContext* context_ = NULL; | |
91 | |
92 void Initialize(OrthancPluginContext* context) | |
93 { | |
94 context_ = context; | |
95 } | |
96 | |
97 InternalLogger::InternalLogger(Level level, | |
98 const char* file /* ignored */, | |
99 int line /* ignored */) : | |
100 level_(level) | |
101 { | |
102 } | |
103 | |
104 InternalLogger::~InternalLogger() | |
105 { | |
106 if (context_ != NULL) | |
107 { | |
108 switch (level_) | |
109 { | |
110 case ERROR: | |
111 OrthancPluginLogError(context_, message_.c_str()); | |
112 break; | |
113 | |
114 case WARNING: | |
115 OrthancPluginLogWarning(context_, message_.c_str()); | |
116 break; | |
117 | |
118 case INFO: | |
119 OrthancPluginLogInfo(context_, message_.c_str()); | |
120 break; | |
121 | |
122 case TRACE: | |
123 // Not used by plugins | |
124 break; | |
125 | |
126 default: | |
127 { | |
128 std::string s = ("Unknown log level (" + boost::lexical_cast<std::string>(level_) + | |
129 ") for message: " + message_); | |
130 OrthancPluginLogError(context_, s.c_str()); | |
131 break; | |
132 } | |
133 } | |
134 } | |
135 } | |
136 } | |
137 } | |
138 | |
139 | |
140 #elif ORTHANC_ENABLE_LOGGING_STDIO == 1 | |
141 | |
142 /********************************************************* | |
143 * Logger compatible with <stdio.h> | |
144 *********************************************************/ | |
145 | |
146 #include <stdio.h> | |
147 #include <boost/lexical_cast.hpp> | |
148 | |
149 namespace Orthanc | |
150 { | |
151 namespace Logging | |
152 { | |
153 static bool globalVerbose_ = false; | |
154 static bool globalTrace_ = false; | |
155 | |
156 InternalLogger::InternalLogger(Level level, | |
157 const char* file /* ignored */, | |
158 int line /* ignored */) : | |
159 level_(level) | |
160 { | |
161 } | |
162 | |
163 InternalLogger::~InternalLogger() | |
164 { | |
165 switch (level_) | |
166 { | |
167 case ERROR: | |
168 fprintf(stderr, "E: %s\n", message_.c_str()); | |
169 break; | |
170 | |
171 case WARNING: | |
172 fprintf(stdout, "W: %s\n", message_.c_str()); | |
173 break; | |
174 | |
175 case INFO: | |
176 if (globalVerbose_) | |
177 { | |
178 fprintf(stdout, "I: %s\n", message_.c_str()); | |
179 } | |
180 break; | |
181 | |
182 case TRACE: | |
183 if (globalTrace_) | |
184 { | |
185 fprintf(stdout, "T: %s\n", message_.c_str()); | |
186 } | |
187 break; | |
188 | |
189 default: | |
190 fprintf(stderr, "Unknown log level (%d) for message: %s\n", level_, message_.c_str()); | |
191 } | |
192 } | |
193 | |
194 void EnableInfoLevel(bool enabled) | |
195 { | |
196 globalVerbose_ = enabled; | |
197 } | |
198 | |
199 void EnableTraceLevel(bool enabled) | |
200 { | |
201 globalTrace_ = enabled; | |
202 } | |
203 } | |
204 } | |
205 | |
206 | |
207 #else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && | |
208 ORTHANC_ENABLE_LOGGING_STDIO == 0 && | |
209 ORTHANC_ENABLE_LOGGING == 1 */ | |
210 | |
211 /********************************************************* | |
212 * Internal logger of Orthanc, that mimics some | |
213 * behavior from Google Log. | |
214 *********************************************************/ | |
215 | |
216 #include "OrthancException.h" | |
217 #include "Enumerations.h" | |
218 #include "Toolbox.h" | |
219 | |
220 #if ORTHANC_SANDBOXED == 1 | |
221 # include <stdio.h> | |
222 #else | |
223 # include "SystemToolbox.h" | |
224 #endif | |
225 | |
226 #include <fstream> | |
227 #include <boost/filesystem.hpp> | |
228 #include <boost/thread.hpp> | |
229 #include <boost/date_time/posix_time/posix_time.hpp> | |
230 | |
231 | |
232 namespace | |
233 { | |
234 struct LoggingContext | |
235 { | |
236 bool infoEnabled_; | |
237 bool traceEnabled_; | |
238 std::string targetFile_; | |
239 std::string targetFolder_; | |
240 | |
241 std::ostream* error_; | |
242 std::ostream* warning_; | |
243 std::ostream* info_; | |
244 | |
245 std::auto_ptr<std::ofstream> file_; | |
246 | |
247 LoggingContext() : | |
248 infoEnabled_(false), | |
249 traceEnabled_(false), | |
250 error_(&std::cerr), | |
251 warning_(&std::cerr), | |
252 info_(&std::cerr) | |
253 { | |
254 } | |
255 }; | |
256 } | |
257 | |
258 | |
259 | |
260 static std::auto_ptr<LoggingContext> loggingContext_; | |
261 static boost::mutex loggingMutex_; | |
262 | |
263 | |
264 | |
265 namespace Orthanc | |
266 { | |
267 namespace Logging | |
268 { | |
269 static void GetLogPath(boost::filesystem::path& log, | |
270 boost::filesystem::path& link, | |
271 const std::string& suffix, | |
272 const std::string& directory) | |
273 { | |
274 /** | |
275 From Google Log documentation: | |
276 | |
277 Unless otherwise specified, logs will be written to the filename | |
278 "<program name>.<hostname>.<user name>.log<suffix>.", | |
279 followed by the date, time, and pid (you can't prevent the date, | |
280 time, and pid from being in the filename). | |
281 | |
282 In this implementation : "hostname" and "username" are not used | |
283 **/ | |
284 | |
285 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); | |
286 boost::filesystem::path root(directory); | |
287 boost::filesystem::path exe(SystemToolbox::GetPathToExecutable()); | |
288 | |
289 if (!boost::filesystem::exists(root) || | |
290 !boost::filesystem::is_directory(root)) | |
291 { | |
292 throw OrthancException(ErrorCode_CannotWriteFile); | |
293 } | |
294 | |
295 char date[64]; | |
296 sprintf(date, "%04d%02d%02d-%02d%02d%02d.%d", | |
297 static_cast<int>(now.date().year()), | |
298 now.date().month().as_number(), | |
299 now.date().day().as_number(), | |
300 static_cast<int>(now.time_of_day().hours()), | |
301 static_cast<int>(now.time_of_day().minutes()), | |
302 static_cast<int>(now.time_of_day().seconds()), | |
303 SystemToolbox::GetProcessId()); | |
304 | |
305 std::string programName = exe.filename().replace_extension("").string(); | |
306 | |
307 log = (root / (programName + ".log" + suffix + "." + std::string(date))); | |
308 link = (root / (programName + ".log" + suffix)); | |
309 } | |
310 | |
311 | |
312 static void PrepareLogFolder(std::auto_ptr<std::ofstream>& file, | |
313 const std::string& suffix, | |
314 const std::string& directory) | |
315 { | |
316 boost::filesystem::path log, link; | |
317 GetLogPath(log, link, suffix, directory); | |
318 | |
319 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) | |
320 boost::filesystem::remove(link); | |
321 boost::filesystem::create_symlink(log.filename(), link); | |
322 #endif | |
323 | |
324 file.reset(new std::ofstream(log.string().c_str())); | |
325 } | |
326 | |
327 | |
328 void Initialize() | |
329 { | |
330 boost::mutex::scoped_lock lock(loggingMutex_); | |
331 loggingContext_.reset(new LoggingContext); | |
332 } | |
333 | |
334 void Finalize() | |
335 { | |
336 boost::mutex::scoped_lock lock(loggingMutex_); | |
337 loggingContext_.reset(NULL); | |
338 } | |
339 | |
340 void Reset() | |
341 { | |
342 // Recover the old logging context | |
343 std::auto_ptr<LoggingContext> old; | |
344 | |
345 { | |
346 boost::mutex::scoped_lock lock(loggingMutex_); | |
347 if (loggingContext_.get() == NULL) | |
348 { | |
349 return; | |
350 } | |
351 else | |
352 { | |
353 old = loggingContext_; | |
354 | |
355 // Create a new logging context, | |
356 loggingContext_.reset(new LoggingContext); | |
357 } | |
358 } | |
359 | |
360 EnableInfoLevel(old->infoEnabled_); | |
361 EnableTraceLevel(old->traceEnabled_); | |
362 | |
363 if (!old->targetFolder_.empty()) | |
364 { | |
365 SetTargetFolder(old->targetFolder_); | |
366 } | |
367 else if (!old->targetFile_.empty()) | |
368 { | |
369 SetTargetFile(old->targetFile_); | |
370 } | |
371 } | |
372 | |
373 void EnableInfoLevel(bool enabled) | |
374 { | |
375 boost::mutex::scoped_lock lock(loggingMutex_); | |
376 assert(loggingContext_.get() != NULL); | |
377 | |
378 loggingContext_->infoEnabled_ = enabled; | |
379 | |
380 if (!enabled) | |
381 { | |
382 // Also disable the "TRACE" level when info-level debugging is disabled | |
383 loggingContext_->traceEnabled_ = false; | |
384 } | |
385 } | |
386 | |
387 void EnableTraceLevel(bool enabled) | |
388 { | |
389 boost::mutex::scoped_lock lock(loggingMutex_); | |
390 assert(loggingContext_.get() != NULL); | |
391 | |
392 loggingContext_->traceEnabled_ = enabled; | |
393 | |
394 if (enabled) | |
395 { | |
396 // Also enable the "INFO" level when trace-level debugging is enabled | |
397 loggingContext_->infoEnabled_ = true; | |
398 } | |
399 } | |
400 | |
401 | |
402 static void CheckFile(std::auto_ptr<std::ofstream>& f) | |
403 { | |
404 if (loggingContext_->file_.get() == NULL || | |
405 !loggingContext_->file_->is_open()) | |
406 { | |
407 throw OrthancException(ErrorCode_CannotWriteFile); | |
408 } | |
409 } | |
410 | |
411 void SetTargetFolder(const std::string& path) | |
412 { | |
413 boost::mutex::scoped_lock lock(loggingMutex_); | |
414 assert(loggingContext_.get() != NULL); | |
415 | |
416 PrepareLogFolder(loggingContext_->file_, "" /* no suffix */, path); | |
417 CheckFile(loggingContext_->file_); | |
418 | |
419 loggingContext_->targetFile_.clear(); | |
420 loggingContext_->targetFolder_ = path; | |
421 loggingContext_->warning_ = loggingContext_->file_.get(); | |
422 loggingContext_->error_ = loggingContext_->file_.get(); | |
423 loggingContext_->info_ = loggingContext_->file_.get(); | |
424 } | |
425 | |
426 | |
427 void SetTargetFile(const std::string& path) | |
428 { | |
429 boost::mutex::scoped_lock lock(loggingMutex_); | |
430 assert(loggingContext_.get() != NULL); | |
431 | |
432 loggingContext_->file_.reset(new std::ofstream(path.c_str(), std::fstream::app)); | |
433 CheckFile(loggingContext_->file_); | |
434 | |
435 loggingContext_->targetFile_ = path; | |
436 loggingContext_->targetFolder_.clear(); | |
437 loggingContext_->warning_ = loggingContext_->file_.get(); | |
438 loggingContext_->error_ = loggingContext_->file_.get(); | |
439 loggingContext_->info_ = loggingContext_->file_.get(); | |
440 } | |
441 | |
442 | |
443 InternalLogger::InternalLogger(const char* level, | |
444 const char* file, | |
445 int line) : | |
446 lock_(loggingMutex_), | |
447 stream_(&null_) // By default, logging to "/dev/null" is simulated | |
448 { | |
449 if (loggingContext_.get() == NULL) | |
450 { | |
451 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n"); | |
452 return; | |
453 } | |
454 | |
455 try | |
456 { | |
457 LogLevel l = StringToLogLevel(level); | |
458 | |
459 if ((l == LogLevel_Info && !loggingContext_->infoEnabled_) || | |
460 (l == LogLevel_Trace && !loggingContext_->traceEnabled_)) | |
461 { | |
462 // This logging level is disabled, directly exit and unlock | |
463 // the mutex to speed-up things. The stream is set to "/dev/null" | |
464 lock_.unlock(); | |
465 return; | |
466 } | |
467 | |
468 // Compute the header of the line, temporary release the lock as | |
469 // this is a time-consuming operation | |
470 lock_.unlock(); | |
471 std::string header; | |
472 | |
473 { | |
474 boost::filesystem::path path(file); | |
475 boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); | |
476 boost::posix_time::time_duration duration = now.time_of_day(); | |
477 | |
478 /** | |
479 From Google Log documentation: | |
480 | |
481 "Log lines have this form: | |
482 | |
483 Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... | |
484 | |
485 where the fields are defined as follows: | |
486 | |
487 L A single character, representing the log level (eg 'I' for INFO) | |
488 mm The month (zero padded; ie May is '05') | |
489 dd The day (zero padded) | |
490 hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds | |
491 threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux) | |
492 file The file name | |
493 line The line number | |
494 msg The user-supplied message" | |
495 | |
496 In this implementation, "threadid" is not printed. | |
497 **/ | |
498 | |
499 char date[32]; | |
500 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", | |
501 level[0], | |
502 now.date().month().as_number(), | |
503 now.date().day().as_number(), | |
504 static_cast<int>(duration.hours()), | |
505 static_cast<int>(duration.minutes()), | |
506 static_cast<int>(duration.seconds()), | |
507 static_cast<int>(duration.fractional_seconds())); | |
508 | |
509 header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast<std::string>(line) + "] "; | |
510 } | |
511 | |
512 | |
513 // The header is computed, we now re-lock the mutex to access | |
514 // the stream objects. Pay attention that "loggingContext_", | |
515 // "infoEnabled_" or "traceEnabled_" might have changed while | |
516 // the mutex was unlocked. | |
517 lock_.lock(); | |
518 | |
519 if (loggingContext_.get() == NULL) | |
520 { | |
521 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n"); | |
522 return; | |
523 } | |
524 | |
525 switch (l) | |
526 { | |
527 case LogLevel_Error: | |
528 stream_ = loggingContext_->error_; | |
529 break; | |
530 | |
531 case LogLevel_Warning: | |
532 stream_ = loggingContext_->warning_; | |
533 break; | |
534 | |
535 case LogLevel_Info: | |
536 if (loggingContext_->infoEnabled_) | |
537 { | |
538 stream_ = loggingContext_->info_; | |
539 } | |
540 | |
541 break; | |
542 | |
543 case LogLevel_Trace: | |
544 if (loggingContext_->traceEnabled_) | |
545 { | |
546 stream_ = loggingContext_->info_; | |
547 } | |
548 | |
549 break; | |
550 | |
551 default: | |
552 throw OrthancException(ErrorCode_InternalError); | |
553 } | |
554 | |
555 if (stream_ == &null_) | |
556 { | |
557 // The logging is disabled for this level. The stream is the | |
558 // "null_" member of this object, so we can release the global | |
559 // mutex. | |
560 lock_.unlock(); | |
561 } | |
562 | |
563 (*stream_) << header; | |
564 } | |
565 catch (...) | |
566 { | |
567 // Something is going really wrong, probably running out of | |
568 // memory. Fallback to a degraded mode. | |
569 stream_ = loggingContext_->error_; | |
570 (*stream_) << "E???? ??:??:??.?????? ] "; | |
571 } | |
572 } | |
573 | |
574 | |
575 InternalLogger::~InternalLogger() | |
576 { | |
577 if (stream_ != &null_) | |
578 { | |
579 #if defined(_WIN32) | |
580 *stream_ << "\r\n"; | |
581 #else | |
582 *stream_ << "\n"; | |
583 #endif | |
584 | |
585 stream_->flush(); | |
586 } | |
587 } | |
588 | |
589 | |
590 void Flush() | |
591 { | |
592 boost::mutex::scoped_lock lock(loggingMutex_); | |
593 | |
594 if (loggingContext_.get() != NULL && | |
595 loggingContext_->file_.get() != NULL) | |
596 { | |
597 loggingContext_->file_->flush(); | |
598 } | |
599 } | |
600 } | |
601 } | |
602 | |
603 #endif // ORTHANC_ENABLE_LOGGING |