Mercurial > hg > orthanc
comparison Core/Logging.cpp @ 3998:b3f09bc9734b
sharing more code between the loggers
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 05 Jun 2020 18:56:35 +0200 |
parents | c2b9a7a1c74a |
children | 02e61695cd83 |
comparison
equal
deleted
inserted
replaced
3997:febe25d03f08 | 3998:b3f09bc9734b |
---|---|
32 | 32 |
33 | 33 |
34 #include "PrecompiledHeaders.h" | 34 #include "PrecompiledHeaders.h" |
35 #include "Logging.h" | 35 #include "Logging.h" |
36 | 36 |
37 #include "OrthancException.h" | |
38 | |
39 | |
40 namespace Orthanc | |
41 { | |
42 namespace Logging | |
43 { | |
44 const char* EnumerationToString(LogLevel level) | |
45 { | |
46 switch (level) | |
47 { | |
48 case LogLevel_ERROR: | |
49 return "ERROR"; | |
50 | |
51 case LogLevel_WARNING: | |
52 return "WARNING"; | |
53 | |
54 case LogLevel_INFO: | |
55 return "INFO"; | |
56 | |
57 case LogLevel_TRACE: | |
58 return "TRACE"; | |
59 | |
60 default: | |
61 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
62 } | |
63 } | |
64 | |
65 | |
66 LogLevel StringToLogLevel(const char *level) | |
67 { | |
68 if (strcmp(level, "ERROR") == 0) | |
69 { | |
70 return LogLevel_ERROR; | |
71 } | |
72 else if (strcmp(level, "WARNING") == 0) | |
73 { | |
74 return LogLevel_WARNING; | |
75 } | |
76 else if (strcmp(level, "INFO") == 0) | |
77 { | |
78 return LogLevel_INFO; | |
79 } | |
80 else if (strcmp(level, "TRACE") == 0) | |
81 { | |
82 return LogLevel_TRACE; | |
83 } | |
84 else | |
85 { | |
86 throw OrthancException(ErrorCode_InternalError); | |
87 } | |
88 } | |
89 } | |
90 } | |
91 | |
92 | |
37 #if ORTHANC_ENABLE_LOGGING != 1 | 93 #if ORTHANC_ENABLE_LOGGING != 1 |
38 | 94 |
39 namespace Orthanc | 95 namespace Orthanc |
40 { | 96 { |
41 namespace Logging | 97 namespace Logging |
165 globalWarningLogFunc = warningLogfunc; | 221 globalWarningLogFunc = warningLogfunc; |
166 globalInfoLogFunc = infoLogFunc; | 222 globalInfoLogFunc = infoLogFunc; |
167 globalTraceLogFunc = traceLogFunc; | 223 globalTraceLogFunc = traceLogFunc; |
168 } | 224 } |
169 | 225 |
170 InternalLogger::InternalLogger(Level level, | 226 InternalLogger::InternalLogger(LogLevel level, |
171 const char* file /* ignored */, | 227 const char* file /* ignored */, |
172 int line /* ignored */) : | 228 int line /* ignored */) : |
173 level_(level) | 229 level_(level) |
174 { | 230 { |
175 } | 231 } |
178 { | 234 { |
179 std::string message = messageStream_.str(); | 235 std::string message = messageStream_.str(); |
180 | 236 |
181 switch (level_) | 237 switch (level_) |
182 { | 238 { |
183 case Level_ERROR: | 239 case LogLevel_ERROR: |
184 globalErrorLogFunc(message.c_str()); | 240 globalErrorLogFunc(message.c_str()); |
185 break; | 241 break; |
186 | 242 |
187 case Level_WARNING: | 243 case LogLevel_WARNING: |
188 globalWarningLogFunc(message.c_str()); | 244 globalWarningLogFunc(message.c_str()); |
189 break; | 245 break; |
190 | 246 |
191 case Level_INFO: | 247 case LogLevel_INFO: |
192 if (globalVerbose_) | 248 if (globalVerbose_) |
193 { | 249 { |
194 globalInfoLogFunc(message.c_str()); | 250 globalInfoLogFunc(message.c_str()); |
195 // TODO: stone_console_info(message_.c_str()); | 251 // TODO: stone_console_info(message_.c_str()); |
196 } | 252 } |
197 break; | 253 break; |
198 | 254 |
199 case Level_TRACE: | 255 case LogLevel_TRACE: |
200 if (globalTrace_) | 256 if (globalTrace_) |
201 { | 257 { |
202 globalTraceLogFunc(message.c_str()); | 258 globalTraceLogFunc(message.c_str()); |
203 } | 259 } |
204 break; | 260 break; |
308 { | 364 { |
309 assert(sizeof(_OrthancPluginService) == sizeof(int32_t)); | 365 assert(sizeof(_OrthancPluginService) == sizeof(int32_t)); |
310 context_ = reinterpret_cast<OrthancPluginContext*>(context); | 366 context_ = reinterpret_cast<OrthancPluginContext*>(context); |
311 } | 367 } |
312 | 368 |
313 InternalLogger::InternalLogger(Level level, | 369 InternalLogger::InternalLogger(LogLevel level, |
314 const char* file /* ignored */, | 370 const char* file /* ignored */, |
315 int line /* ignored */) : | 371 int line /* ignored */) : |
316 level_(level) | 372 level_(level) |
317 { | 373 { |
318 } | 374 } |
322 std::string message = messageStream_.str(); | 378 std::string message = messageStream_.str(); |
323 if (context_ != NULL) | 379 if (context_ != NULL) |
324 { | 380 { |
325 switch (level_) | 381 switch (level_) |
326 { | 382 { |
327 case Level_ERROR: | 383 case LogLevel_ERROR: |
328 context_->InvokeService(context_, _OrthancPluginService_LogError, message.c_str()); | 384 context_->InvokeService(context_, _OrthancPluginService_LogError, message.c_str()); |
329 break; | 385 break; |
330 | 386 |
331 case Level_WARNING: | 387 case LogLevel_WARNING: |
332 context_->InvokeService(context_, _OrthancPluginService_LogWarning, message.c_str()); | 388 context_->InvokeService(context_, _OrthancPluginService_LogWarning, message.c_str()); |
333 break; | 389 break; |
334 | 390 |
335 case Level_INFO: | 391 case LogLevel_INFO: |
336 context_->InvokeService(context_, _OrthancPluginService_LogInfo, message.c_str()); | 392 context_->InvokeService(context_, _OrthancPluginService_LogInfo, message.c_str()); |
337 break; | 393 break; |
338 | 394 |
339 case Level_TRACE: | 395 case LogLevel_TRACE: |
340 // Not used by plugins | 396 // Not used by plugins |
341 break; | 397 break; |
342 | 398 |
343 default: | 399 default: |
344 { | 400 { |
362 * Internal logger of Orthanc, that mimics some | 418 * Internal logger of Orthanc, that mimics some |
363 * behavior from Google Log. | 419 * behavior from Google Log. |
364 *********************************************************/ | 420 *********************************************************/ |
365 | 421 |
366 #include "Compatibility.h" | 422 #include "Compatibility.h" |
367 #include "OrthancException.h" | |
368 #include "Enumerations.h" | 423 #include "Enumerations.h" |
369 #include "Toolbox.h" | 424 #include "Toolbox.h" |
370 | 425 |
371 #if ORTHANC_SANDBOXED == 1 | 426 #if ORTHANC_SANDBOXED == 1 |
372 # include <stdio.h> | 427 # include <stdio.h> |
676 loggingContext_->error_ = loggingContext_->file_.get(); | 731 loggingContext_->error_ = loggingContext_->file_.get(); |
677 loggingContext_->info_ = loggingContext_->file_.get(); | 732 loggingContext_->info_ = loggingContext_->file_.get(); |
678 } | 733 } |
679 | 734 |
680 | 735 |
681 InternalLogger::InternalLogger(const char* level, | 736 InternalLogger::InternalLogger(LogLevel level, |
682 const char* file, | 737 const char* file, |
683 int line) : | 738 int line) : |
684 lock_(loggingMutex_), | 739 lock_(loggingMutex_), |
685 stream_(&null_) // By default, logging to "/dev/null" is simulated | 740 stream_(&null_) // By default, logging to "/dev/null" is simulated |
686 { | 741 { |
690 return; | 745 return; |
691 } | 746 } |
692 | 747 |
693 try | 748 try |
694 { | 749 { |
695 LogLevel l = StringToLogLevel(level); | 750 if ((level == LogLevel_INFO && !loggingContext_->infoEnabled_) || |
696 | 751 (level == LogLevel_TRACE && !loggingContext_->traceEnabled_)) |
697 if ((l == LogLevel_Info && !loggingContext_->infoEnabled_) || | |
698 (l == LogLevel_Trace && !loggingContext_->traceEnabled_)) | |
699 { | 752 { |
700 // This logging level is disabled, directly exit and unlock | 753 // This logging level is disabled, directly exit and unlock |
701 // the mutex to speed-up things. The stream is set to "/dev/null" | 754 // the mutex to speed-up things. The stream is set to "/dev/null" |
702 lock_.unlock(); | 755 lock_.unlock(); |
703 return; | 756 return; |
732 msg The user-supplied message" | 785 msg The user-supplied message" |
733 | 786 |
734 In this implementation, "threadid" is not printed. | 787 In this implementation, "threadid" is not printed. |
735 **/ | 788 **/ |
736 | 789 |
790 char prefix; | |
791 switch (level) | |
792 { | |
793 case LogLevel_ERROR: | |
794 prefix = 'E'; | |
795 break; | |
796 | |
797 case LogLevel_WARNING: | |
798 prefix = 'W'; | |
799 break; | |
800 | |
801 case LogLevel_INFO: | |
802 prefix = 'I'; | |
803 break; | |
804 | |
805 case LogLevel_TRACE: | |
806 prefix = 'T'; | |
807 break; | |
808 | |
809 default: | |
810 throw OrthancException(ErrorCode_InternalError); | |
811 } | |
812 | |
737 char date[64]; | 813 char date[64]; |
738 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", | 814 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", |
739 level[0], | 815 prefix, |
740 now.date().month().as_number(), | 816 now.date().month().as_number(), |
741 now.date().day().as_number(), | 817 now.date().day().as_number(), |
742 static_cast<int>(duration.hours()), | 818 static_cast<int>(duration.hours()), |
743 static_cast<int>(duration.minutes()), | 819 static_cast<int>(duration.minutes()), |
744 static_cast<int>(duration.seconds()), | 820 static_cast<int>(duration.seconds()), |
758 { | 834 { |
759 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n"); | 835 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n"); |
760 return; | 836 return; |
761 } | 837 } |
762 | 838 |
763 switch (l) | 839 switch (level) |
764 { | 840 { |
765 case LogLevel_Error: | 841 case LogLevel_ERROR: |
766 stream_ = loggingContext_->error_; | 842 stream_ = loggingContext_->error_; |
767 break; | 843 break; |
768 | 844 |
769 case LogLevel_Warning: | 845 case LogLevel_WARNING: |
770 stream_ = loggingContext_->warning_; | 846 stream_ = loggingContext_->warning_; |
771 break; | 847 break; |
772 | 848 |
773 case LogLevel_Info: | 849 case LogLevel_INFO: |
774 if (loggingContext_->infoEnabled_) | 850 if (loggingContext_->infoEnabled_) |
775 { | 851 { |
776 stream_ = loggingContext_->info_; | 852 stream_ = loggingContext_->info_; |
777 } | 853 } |
778 | 854 |
779 break; | 855 break; |
780 | 856 |
781 case LogLevel_Trace: | 857 case LogLevel_TRACE: |
782 if (loggingContext_->traceEnabled_) | 858 if (loggingContext_->traceEnabled_) |
783 { | 859 { |
784 stream_ = loggingContext_->info_; | 860 stream_ = loggingContext_->info_; |
785 } | 861 } |
786 | 862 |