Mercurial > hg > orthanc
comparison OrthancFramework/Sources/Logging.cpp @ 4273:0034f855c023
tuning log categories from command-line, and binary compat with orthanc framework 1.7.2
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 03 Nov 2020 12:24:50 +0100 |
parents | 1661544ea94d |
children | d7a50b7b8466 |
comparison
equal
deleted
inserted
replaced
4272:1661544ea94d | 4273:0034f855c023 |
---|---|
191 return false; | 191 return false; |
192 } | 192 } |
193 } | 193 } |
194 | 194 |
195 | 195 |
196 LogCategory StringToCategory(const std::string& category) | 196 bool LookupCategory(LogCategory& target, |
197 const std::string& category) | |
197 { | 198 { |
198 if (category == "generic") | 199 if (category == "generic") |
199 { | 200 { |
200 return LogCategory_GENERIC; | 201 target = LogCategory_GENERIC; |
202 return true; | |
201 } | 203 } |
202 else if (category == "plugins") | 204 else if (category == "plugins") |
203 { | 205 { |
204 return LogCategory_PLUGINS; | 206 target = LogCategory_PLUGINS; |
207 return true; | |
205 } | 208 } |
206 else if (category == "rest") | 209 else if (category == "rest") |
207 { | 210 { |
208 return LogCategory_REST; | 211 target = LogCategory_REST; |
212 return true; | |
209 } | 213 } |
210 else if (category == "dicom") | 214 else if (category == "dicom") |
211 { | 215 { |
212 return LogCategory_DICOM; | 216 target = LogCategory_DICOM; |
217 return true; | |
213 } | 218 } |
214 else if (category == "sqlite") | 219 else if (category == "sqlite") |
215 { | 220 { |
216 return LogCategory_SQLITE; | 221 target = LogCategory_SQLITE; |
222 return true; | |
217 } | 223 } |
218 else | 224 else |
219 { | 225 { |
220 throw OrthancException(ErrorCode_ParameterOutOfRange, | 226 return false; |
221 "Unknown log category: " + category); | 227 } |
228 } | |
229 | |
230 | |
231 size_t GetCategoriesCount() | |
232 { | |
233 return 5; | |
234 } | |
235 | |
236 | |
237 const char* GetCategoryName(size_t i) | |
238 { | |
239 switch (i) | |
240 { | |
241 case 0: | |
242 return "generic"; | |
243 | |
244 case 1: | |
245 return "plugins"; | |
246 | |
247 case 2: | |
248 return "rest"; | |
249 | |
250 case 3: | |
251 return "dicom"; | |
252 | |
253 case 4: | |
254 return "sqlite"; | |
255 | |
256 default: | |
257 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
222 } | 258 } |
223 } | 259 } |
224 } | 260 } |
225 } | 261 } |
226 | 262 |
707 loggingStreamsContext_->info_ = loggingStreamsContext_->file_.get(); | 743 loggingStreamsContext_->info_ = loggingStreamsContext_->file_.get(); |
708 } | 744 } |
709 } | 745 } |
710 | 746 |
711 | 747 |
712 InternalLogger::InternalLogger(LogLevel level, | 748 void InternalLogger::Setup(LogCategory category, |
713 LogCategory category, | 749 const char* file, |
714 const char* file, | 750 int line) |
715 int line) : | |
716 lock_(loggingStreamsMutex_, boost::defer_lock_t()), | |
717 level_(level), | |
718 stream_(&nullStream_) // By default, logging to "/dev/null" is simulated | |
719 { | 751 { |
720 if (pluginContext_ != NULL) | 752 if (pluginContext_ != NULL) |
721 { | 753 { |
722 // We are logging using the Orthanc plugin SDK | 754 // We are logging using the Orthanc plugin SDK |
723 | 755 |
724 if (level == LogLevel_TRACE || | 756 if (level_ == LogLevel_TRACE || |
725 !IsCategoryEnabled(level, category)) | 757 !IsCategoryEnabled(level_, category)) |
726 { | 758 { |
727 // No trace level in plugins, directly exit as the stream is | 759 // No trace level in plugins, directly exit as the stream is |
728 // set to "/dev/null" | 760 // set to "/dev/null" |
729 return; | 761 return; |
730 } | 762 } |
736 } | 768 } |
737 else | 769 else |
738 { | 770 { |
739 // We are logging in a standalone application, not inside an Orthanc plugin | 771 // We are logging in a standalone application, not inside an Orthanc plugin |
740 | 772 |
741 if (!IsCategoryEnabled(level, category)) | 773 if (!IsCategoryEnabled(level_, category)) |
742 { | 774 { |
743 // This logging level is disabled, directly exit as the | 775 // This logging level is disabled, directly exit as the |
744 // stream is set to "/dev/null" | 776 // stream is set to "/dev/null" |
745 return; | 777 return; |
746 } | 778 } |
747 | 779 |
748 std::string prefix; | 780 std::string prefix; |
749 GetLinePrefix(prefix, level, file, line); | 781 GetLinePrefix(prefix, level_, file, line); |
750 | 782 |
751 { | 783 { |
752 // We lock the global mutex. The mutex is locked until the | 784 // We lock the global mutex. The mutex is locked until the |
753 // destructor is called: No change in the output can be done. | 785 // destructor is called: No change in the output can be done. |
754 lock_.lock(); | 786 lock_.lock(); |
758 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n"); | 790 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n"); |
759 lock_.unlock(); | 791 lock_.unlock(); |
760 return; | 792 return; |
761 } | 793 } |
762 | 794 |
763 switch (level) | 795 switch (level_) |
764 { | 796 { |
765 case LogLevel_ERROR: | 797 case LogLevel_ERROR: |
766 stream_ = loggingStreamsContext_->error_; | 798 stream_ = loggingStreamsContext_->error_; |
767 break; | 799 break; |
768 | 800 |
803 } | 835 } |
804 } | 836 } |
805 } | 837 } |
806 | 838 |
807 | 839 |
840 InternalLogger::InternalLogger(LogLevel level, | |
841 LogCategory category, | |
842 const char* file, | |
843 int line) : | |
844 lock_(loggingStreamsMutex_, boost::defer_lock_t()), | |
845 level_(level), | |
846 stream_(&nullStream_) // By default, logging to "/dev/null" is simulated | |
847 { | |
848 Setup(category, file, line); | |
849 } | |
850 | |
851 | |
852 InternalLogger::InternalLogger(LogLevel level, | |
853 const char* file, | |
854 int line) : | |
855 lock_(loggingStreamsMutex_, boost::defer_lock_t()), | |
856 level_(level), | |
857 stream_(&nullStream_) // By default, logging to "/dev/null" is simulated | |
858 { | |
859 Setup(LogCategory_GENERIC, file, line); | |
860 } | |
861 | |
862 | |
808 InternalLogger::~InternalLogger() | 863 InternalLogger::~InternalLogger() |
809 { | 864 { |
810 if (pluginStream_.get() != NULL) | 865 if (pluginStream_.get() != NULL) |
811 { | 866 { |
812 // We are logging through the Orthanc SDK | 867 // We are logging through the Orthanc SDK |