comparison OrthancFramework/Sources/Logging.cpp @ 4270:251a8b07fa37

logging categories
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 02 Nov 2020 18:45:50 +0100
parents c7bd2f21ccc3
children 1bd14c900699
comparison
equal deleted inserted replaced
4269:c7bd2f21ccc3 4270:251a8b07fa37
32 32
33 namespace Orthanc 33 namespace Orthanc
34 { 34 {
35 namespace Logging 35 namespace Logging
36 { 36 {
37 static bool infoEnabled_ = false; 37 static const uint32_t ALL_CATEGORIES_MASK = 0xffffffff;
38 static bool traceEnabled_ = false; 38
39 static uint32_t infoCategoriesMask_ = 0;
40 static uint32_t traceCategoriesMask_ = 0;
39 41
40 const char* EnumerationToString(LogLevel level) 42 const char* EnumerationToString(LogLevel level)
41 { 43 {
42 switch (level) 44 switch (level)
43 { 45 {
81 { 83 {
82 throw OrthancException(ErrorCode_InternalError); 84 throw OrthancException(ErrorCode_InternalError);
83 } 85 }
84 } 86 }
85 87
88
86 void EnableInfoLevel(bool enabled) 89 void EnableInfoLevel(bool enabled)
87 { 90 {
88 infoEnabled_ = enabled; 91 if (enabled)
89 92 {
90 if (!enabled) 93 infoCategoriesMask_ = ALL_CATEGORIES_MASK;
94 }
95 else
91 { 96 {
92 // Also disable the "TRACE" level when info-level debugging is disabled 97 // Also disable the "TRACE" level when info-level debugging is disabled
93 traceEnabled_ = false; 98 infoCategoriesMask_ = 0;
94 } 99 traceCategoriesMask_ = 0;
95 } 100 }
96 101 }
102
103
97 bool IsInfoLevelEnabled() 104 bool IsInfoLevelEnabled()
98 { 105 {
99 return infoEnabled_; 106 return (infoCategoriesMask_ != 0);
100 } 107 }
101 108
109
102 void EnableTraceLevel(bool enabled) 110 void EnableTraceLevel(bool enabled)
103 { 111 {
104 traceEnabled_ = enabled;
105
106 if (enabled) 112 if (enabled)
107 { 113 {
108 // Also enable the "INFO" level when trace-level debugging is enabled 114 // Also enable the "INFO" level when trace-level debugging is enabled
109 infoEnabled_ = true; 115 infoCategoriesMask_ = ALL_CATEGORIES_MASK;
110 } 116 traceCategoriesMask_ = ALL_CATEGORIES_MASK;
111 } 117 }
118 else
119 {
120 traceCategoriesMask_ = 0;
121 }
122 }
123
112 124
113 bool IsTraceLevelEnabled() 125 bool IsTraceLevelEnabled()
114 { 126 {
115 return traceEnabled_; 127 return (traceCategoriesMask_ != 0);
116 } 128 }
117 129
118 static bool IsLoggingEnabled(LogLevel level, 130
119 LogCategory category) 131 void SetCategoryEnabled(LogLevel level,
132 LogCategory category,
133 bool enabled)
134 {
135 if (level == LogLevel_INFO)
136 {
137 if (enabled)
138 {
139 infoCategoriesMask_ |= static_cast<uint32_t>(category);
140 }
141 else
142 {
143 infoCategoriesMask_ &= ~static_cast<uint32_t>(category);
144 traceCategoriesMask_ &= ~static_cast<uint32_t>(category);
145 }
146 }
147 else if (level == LogLevel_TRACE)
148 {
149 if (enabled)
150 {
151 traceCategoriesMask_ |= static_cast<uint32_t>(category);
152 infoCategoriesMask_ |= static_cast<uint32_t>(category);
153 }
154 else
155 {
156 traceCategoriesMask_ &= ~static_cast<uint32_t>(category);
157 }
158 }
159 else
160 {
161 throw OrthancException(ErrorCode_ParameterOutOfRange,
162 "Can only modify the parameters of the INFO and TRACE levels");
163 }
164 }
165
166
167 bool IsCategoryEnabled(LogLevel level,
168 LogCategory category)
120 { 169 {
121 if (level == LogLevel_ERROR || 170 if (level == LogLevel_ERROR ||
122 level == LogLevel_WARNING) 171 level == LogLevel_WARNING)
123 { 172 {
124 return true; 173 return true;
125 } 174 }
126 else if (level == LogLevel_INFO) 175 else if (level == LogLevel_INFO)
127 { 176 {
128 return infoEnabled_; 177 return (infoCategoriesMask_ & category) != 0;
129 } 178 }
130 else if (level == LogLevel_TRACE) 179 else if (level == LogLevel_TRACE)
131 { 180 {
132 return traceEnabled_; 181 return (traceCategoriesMask_ & category) != 0;
133 } 182 }
134 else 183 else
135 { 184 {
136 return false; 185 return false;
186 }
187 }
188
189
190 LogCategory StringToCategory(const std::string& category)
191 {
192 if (category == "generic")
193 {
194 return LogCategory_GENERIC;
195 }
196 else if (category == "dicom")
197 {
198 return LogCategory_DICOM;
199 }
200 else if (category == "sqlite")
201 {
202 return LogCategory_SQLITE;
203 }
204 else
205 {
206 throw OrthancException(ErrorCode_ParameterOutOfRange,
207 "Unknown log category: " + category);
137 } 208 }
138 } 209 }
139 } 210 }
140 } 211 }
141 212
245 316
246 InternalLogger::~InternalLogger() 317 InternalLogger::~InternalLogger()
247 { 318 {
248 std::string message = messageStream_.str(); 319 std::string message = messageStream_.str();
249 320
250 if (IsLoggingEnabled(level_, category_)) 321 if (IsCategoryEnabled(level_, category_))
251 { 322 {
252 switch (level_) 323 switch (level_)
253 { 324 {
254 case LogLevel_ERROR: 325 case LogLevel_ERROR:
255 ErrorLogFunc(message.c_str()); 326 ErrorLogFunc(message.c_str());
635 if (pluginContext_ != NULL) 706 if (pluginContext_ != NULL)
636 { 707 {
637 // We are logging using the Orthanc plugin SDK 708 // We are logging using the Orthanc plugin SDK
638 709
639 if (level == LogLevel_TRACE || 710 if (level == LogLevel_TRACE ||
640 !IsLoggingEnabled(level, category)) 711 !IsCategoryEnabled(level, category))
641 { 712 {
642 // No trace level in plugins, directly exit as the stream is 713 // No trace level in plugins, directly exit as the stream is
643 // set to "/dev/null" 714 // set to "/dev/null"
644 return; 715 return;
645 } 716 }
651 } 722 }
652 else 723 else
653 { 724 {
654 // We are logging in a standalone application, not inside an Orthanc plugin 725 // We are logging in a standalone application, not inside an Orthanc plugin
655 726
656 if (!IsLoggingEnabled(level, category)) 727 if (!IsCategoryEnabled(level, category))
657 { 728 {
658 // This logging level is disabled, directly exit as the 729 // This logging level is disabled, directly exit as the
659 // stream is set to "/dev/null" 730 // stream is set to "/dev/null"
660 return; 731 return;
661 } 732 }