Mercurial > hg > orthanc-stone
annotate Resources/Orthanc/Core/Logging.cpp @ 81:8677d95753f8 wasm
switching to Oracle in SDL samples
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 24 May 2017 22:54:09 +0200 |
parents | 81e2651dca17 |
children |
rev | line source |
---|---|
1 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
1 | 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 | |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
77 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
78 #elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
79 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
80 /********************************************************* |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
81 * Logger compatible with the Orthanc plugin SDK |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
82 *********************************************************/ |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
83 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
84 #include <boost/lexical_cast.hpp> |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
85 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
86 namespace Orthanc |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
87 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
88 namespace Logging |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
89 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
90 static OrthancPluginContext* context_ = NULL; |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
91 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
92 void Initialize(OrthancPluginContext* context) |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
93 { |
42 | 94 context_ = context; |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
95 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
96 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
97 InternalLogger::InternalLogger(const char* level, |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
98 const char* file /* ignored */, |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
99 int line /* ignored */) : |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
100 level_(level) |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
101 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
102 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
103 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
104 InternalLogger::~InternalLogger() |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
105 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
106 if (context_ != NULL) |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
107 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
108 if (level_ == "ERROR") |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
109 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
110 OrthancPluginLogError(context_, message_.c_str()); |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
111 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
112 else if (level_ == "WARNING") |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
113 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
114 OrthancPluginLogWarning(context_, message_.c_str()); |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
115 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
116 else if (level_ == "INFO") |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
117 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
118 OrthancPluginLogInfo(context_, message_.c_str()); |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
119 } |
42 | 120 else |
121 { | |
122 std::string s = "Unknown log level (" + level_ + ") for message: " + message_; | |
123 OrthancPluginLogError(context_, s.c_str()); | |
124 } | |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
125 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
126 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
127 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
128 InternalLogger& InternalLogger::operator<< (const std::string& message) |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
129 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
130 message_ += message; |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
131 return *this; |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
132 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
133 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
134 InternalLogger& InternalLogger::operator<< (const char* message) |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
135 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
136 message_ += std::string(message); |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
137 return *this; |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
138 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
139 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
140 InternalLogger& InternalLogger::operator<< (int message) |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
141 { |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
142 message_ += boost::lexical_cast<std::string>(message); |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
143 return *this; |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
144 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
145 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
146 } |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
147 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
148 |
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
149 #else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */ |
1 | 150 |
151 /********************************************************* | |
152 * Internal logger of Orthanc, that mimics some | |
153 * behavior from Google Log. | |
154 *********************************************************/ | |
155 | |
156 #include "OrthancException.h" | |
157 #include "Enumerations.h" | |
158 #include "Toolbox.h" | |
15 | 159 #include "SystemToolbox.h" |
1 | 160 |
161 #include <fstream> | |
162 #include <boost/filesystem.hpp> | |
163 #include <boost/thread.hpp> | |
164 | |
165 #if BOOST_HAS_DATE_TIME == 1 | |
166 # include <boost/date_time/posix_time/posix_time.hpp> | |
167 #else | |
168 # error Boost::date_time is required | |
169 #endif | |
170 | |
171 | |
172 namespace | |
173 { | |
174 struct LoggingContext | |
175 { | |
176 bool infoEnabled_; | |
177 bool traceEnabled_; | |
178 std::string targetFile_; | |
179 std::string targetFolder_; | |
180 | |
181 std::ostream* error_; | |
182 std::ostream* warning_; | |
183 std::ostream* info_; | |
184 | |
185 std::auto_ptr<std::ofstream> file_; | |
186 | |
187 LoggingContext() : | |
188 infoEnabled_(false), | |
189 traceEnabled_(false), | |
190 error_(&std::cerr), | |
191 warning_(&std::cerr), | |
192 info_(&std::cerr) | |
193 { | |
194 } | |
195 }; | |
196 } | |
197 | |
198 | |
199 | |
200 static std::auto_ptr<LoggingContext> loggingContext_; | |
201 static boost::mutex loggingMutex_; | |
202 | |
203 | |
204 | |
205 namespace Orthanc | |
206 { | |
207 namespace Logging | |
208 { | |
209 static void GetLogPath(boost::filesystem::path& log, | |
210 boost::filesystem::path& link, | |
211 const std::string& suffix, | |
212 const std::string& directory) | |
213 { | |
214 /** | |
215 From Google Log documentation: | |
216 | |
217 Unless otherwise specified, logs will be written to the filename | |
218 "<program name>.<hostname>.<user name>.log<suffix>.", | |
219 followed by the date, time, and pid (you can't prevent the date, | |
220 time, and pid from being in the filename). | |
221 | |
222 In this implementation : "hostname" and "username" are not used | |
223 **/ | |
224 | |
225 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); | |
226 boost::filesystem::path root(directory); | |
15 | 227 boost::filesystem::path exe(SystemToolbox::GetPathToExecutable()); |
1 | 228 |
229 if (!boost::filesystem::exists(root) || | |
230 !boost::filesystem::is_directory(root)) | |
231 { | |
232 throw OrthancException(ErrorCode_CannotWriteFile); | |
233 } | |
234 | |
235 char date[64]; | |
236 sprintf(date, "%04d%02d%02d-%02d%02d%02d.%d", | |
237 static_cast<int>(now.date().year()), | |
238 now.date().month().as_number(), | |
239 now.date().day().as_number(), | |
240 now.time_of_day().hours(), | |
241 now.time_of_day().minutes(), | |
242 now.time_of_day().seconds(), | |
15 | 243 SystemToolbox::GetProcessId()); |
1 | 244 |
245 std::string programName = exe.filename().replace_extension("").string(); | |
246 | |
247 log = (root / (programName + ".log" + suffix + "." + std::string(date))); | |
248 link = (root / (programName + ".log" + suffix)); | |
249 } | |
250 | |
251 | |
252 static void PrepareLogFolder(std::auto_ptr<std::ofstream>& file, | |
253 const std::string& suffix, | |
254 const std::string& directory) | |
255 { | |
256 boost::filesystem::path log, link; | |
257 GetLogPath(log, link, suffix, directory); | |
258 | |
259 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) | |
260 boost::filesystem::remove(link); | |
261 boost::filesystem::create_symlink(log.filename(), link); | |
262 #endif | |
263 | |
264 file.reset(new std::ofstream(log.string().c_str())); | |
265 } | |
266 | |
267 | |
268 void Initialize() | |
269 { | |
270 boost::mutex::scoped_lock lock(loggingMutex_); | |
271 loggingContext_.reset(new LoggingContext); | |
272 } | |
273 | |
274 void Finalize() | |
275 { | |
276 boost::mutex::scoped_lock lock(loggingMutex_); | |
277 loggingContext_.reset(NULL); | |
278 } | |
279 | |
280 void Reset() | |
281 { | |
282 // Recover the old logging context | |
283 std::auto_ptr<LoggingContext> old; | |
284 | |
285 { | |
286 boost::mutex::scoped_lock lock(loggingMutex_); | |
287 if (loggingContext_.get() == NULL) | |
288 { | |
289 return; | |
290 } | |
291 else | |
292 { | |
293 old = loggingContext_; | |
294 | |
295 // Create a new logging context, | |
296 loggingContext_.reset(new LoggingContext); | |
297 } | |
298 } | |
299 | |
300 EnableInfoLevel(old->infoEnabled_); | |
301 EnableTraceLevel(old->traceEnabled_); | |
302 | |
303 if (!old->targetFolder_.empty()) | |
304 { | |
305 SetTargetFolder(old->targetFolder_); | |
306 } | |
307 else if (!old->targetFile_.empty()) | |
308 { | |
309 SetTargetFile(old->targetFile_); | |
310 } | |
311 } | |
312 | |
313 void EnableInfoLevel(bool enabled) | |
314 { | |
315 boost::mutex::scoped_lock lock(loggingMutex_); | |
316 assert(loggingContext_.get() != NULL); | |
317 | |
318 loggingContext_->infoEnabled_ = enabled; | |
319 | |
320 if (!enabled) | |
321 { | |
322 // Also disable the "TRACE" level when info-level debugging is disabled | |
323 loggingContext_->traceEnabled_ = false; | |
324 } | |
325 } | |
326 | |
327 void EnableTraceLevel(bool enabled) | |
328 { | |
329 boost::mutex::scoped_lock lock(loggingMutex_); | |
330 assert(loggingContext_.get() != NULL); | |
331 | |
332 loggingContext_->traceEnabled_ = enabled; | |
333 | |
334 if (enabled) | |
335 { | |
336 // Also enable the "INFO" level when trace-level debugging is enabled | |
337 loggingContext_->infoEnabled_ = true; | |
338 } | |
339 } | |
340 | |
341 | |
342 static void CheckFile(std::auto_ptr<std::ofstream>& f) | |
343 { | |
344 if (loggingContext_->file_.get() == NULL || | |
345 !loggingContext_->file_->is_open()) | |
346 { | |
347 throw OrthancException(ErrorCode_CannotWriteFile); | |
348 } | |
349 } | |
350 | |
351 void SetTargetFolder(const std::string& path) | |
352 { | |
353 boost::mutex::scoped_lock lock(loggingMutex_); | |
354 assert(loggingContext_.get() != NULL); | |
355 | |
356 PrepareLogFolder(loggingContext_->file_, "" /* no suffix */, path); | |
357 CheckFile(loggingContext_->file_); | |
358 | |
359 loggingContext_->targetFile_.clear(); | |
360 loggingContext_->targetFolder_ = path; | |
361 loggingContext_->warning_ = loggingContext_->file_.get(); | |
362 loggingContext_->error_ = loggingContext_->file_.get(); | |
363 loggingContext_->info_ = loggingContext_->file_.get(); | |
364 } | |
365 | |
366 | |
367 void SetTargetFile(const std::string& path) | |
368 { | |
369 boost::mutex::scoped_lock lock(loggingMutex_); | |
370 assert(loggingContext_.get() != NULL); | |
371 | |
372 loggingContext_->file_.reset(new std::ofstream(path.c_str(), std::fstream::app)); | |
373 CheckFile(loggingContext_->file_); | |
374 | |
375 loggingContext_->targetFile_ = path; | |
376 loggingContext_->targetFolder_.clear(); | |
377 loggingContext_->warning_ = loggingContext_->file_.get(); | |
378 loggingContext_->error_ = loggingContext_->file_.get(); | |
379 loggingContext_->info_ = loggingContext_->file_.get(); | |
380 } | |
381 | |
382 | |
383 InternalLogger::InternalLogger(const char* level, | |
384 const char* file, | |
385 int line) : | |
386 lock_(loggingMutex_), | |
387 stream_(&null_) // By default, logging to "/dev/null" is simulated | |
388 { | |
389 if (loggingContext_.get() == NULL) | |
390 { | |
391 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n"); | |
392 return; | |
393 } | |
394 | |
12 | 395 try |
1 | 396 { |
12 | 397 LogLevel l = StringToLogLevel(level); |
398 | |
399 if ((l == LogLevel_Info && !loggingContext_->infoEnabled_) || | |
400 (l == LogLevel_Trace && !loggingContext_->traceEnabled_)) | |
401 { | |
402 // This logging level is disabled, directly exit and unlock | |
403 // the mutex to speed-up things. The stream is set to "/dev/null" | |
404 lock_.unlock(); | |
405 return; | |
406 } | |
1 | 407 |
12 | 408 // Compute the header of the line, temporary release the lock as |
409 // this is a time-consuming operation | |
410 lock_.unlock(); | |
411 std::string header; | |
1 | 412 |
12 | 413 { |
414 boost::filesystem::path path(file); | |
415 boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); | |
416 boost::posix_time::time_duration duration = now.time_of_day(); | |
1 | 417 |
12 | 418 /** |
419 From Google Log documentation: | |
1 | 420 |
12 | 421 "Log lines have this form: |
1 | 422 |
12 | 423 Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... |
1 | 424 |
12 | 425 where the fields are defined as follows: |
1 | 426 |
12 | 427 L A single character, representing the log level (eg 'I' for INFO) |
428 mm The month (zero padded; ie May is '05') | |
429 dd The day (zero padded) | |
430 hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds | |
431 threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux) | |
432 file The file name | |
433 line The line number | |
434 msg The user-supplied message" | |
1 | 435 |
12 | 436 In this implementation, "threadid" is not printed. |
437 **/ | |
1 | 438 |
12 | 439 char date[32]; |
440 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", | |
441 level[0], | |
442 now.date().month().as_number(), | |
443 now.date().day().as_number(), | |
444 duration.hours(), | |
445 duration.minutes(), | |
446 duration.seconds(), | |
447 static_cast<int>(duration.fractional_seconds())); | |
1 | 448 |
12 | 449 header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast<std::string>(line) + "] "; |
450 } | |
1 | 451 |
452 | |
12 | 453 // The header is computed, we now re-lock the mutex to access |
454 // the stream objects. Pay attention that "loggingContext_", | |
455 // "infoEnabled_" or "traceEnabled_" might have changed while | |
456 // the mutex was unlocked. | |
457 lock_.lock(); | |
458 | |
459 if (loggingContext_.get() == NULL) | |
460 { | |
461 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n"); | |
462 return; | |
463 } | |
1 | 464 |
12 | 465 switch (l) |
466 { | |
467 case LogLevel_Error: | |
468 stream_ = loggingContext_->error_; | |
469 break; | |
1 | 470 |
12 | 471 case LogLevel_Warning: |
472 stream_ = loggingContext_->warning_; | |
473 break; | |
1 | 474 |
12 | 475 case LogLevel_Info: |
476 if (loggingContext_->infoEnabled_) | |
477 { | |
478 stream_ = loggingContext_->info_; | |
479 } | |
480 | |
481 break; | |
1 | 482 |
12 | 483 case LogLevel_Trace: |
484 if (loggingContext_->traceEnabled_) | |
485 { | |
486 stream_ = loggingContext_->info_; | |
487 } | |
1 | 488 |
12 | 489 break; |
1 | 490 |
12 | 491 default: |
492 throw OrthancException(ErrorCode_InternalError); | |
493 } | |
1 | 494 |
12 | 495 if (stream_ == &null_) |
496 { | |
497 // The logging is disabled for this level. The stream is the | |
498 // "null_" member of this object, so we can release the global | |
499 // mutex. | |
500 lock_.unlock(); | |
501 } | |
1 | 502 |
12 | 503 (*stream_) << header; |
1 | 504 } |
12 | 505 catch (...) |
506 { | |
507 // Something is going really wrong, probably running out of | |
508 // memory. Fallback to a degraded mode. | |
509 stream_ = loggingContext_->error_; | |
510 (*stream_) << "E???? ??:??:??.?????? ] "; | |
511 } | |
1 | 512 } |
513 | |
514 | |
515 InternalLogger::~InternalLogger() | |
516 { | |
517 if (stream_ != &null_) | |
518 { | |
519 #if defined(_WIN32) | |
520 *stream_ << "\r\n"; | |
521 #else | |
522 *stream_ << "\n"; | |
523 #endif | |
524 | |
525 stream_->flush(); | |
526 } | |
527 } | |
528 | |
529 | |
530 void Flush() | |
531 { | |
532 boost::mutex::scoped_lock lock(loggingMutex_); | |
533 | |
534 if (loggingContext_.get() != NULL && | |
535 loggingContext_->file_.get() != NULL) | |
536 { | |
537 loggingContext_->file_->flush(); | |
538 } | |
539 } | |
540 } | |
541 } | |
542 | |
543 #endif // ORTHANC_ENABLE_LOGGING |