Mercurial > hg > orthanc
annotate Core/Logging.cpp @ 1489:1389834e130f
basic logger
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 04 Aug 2015 12:22:08 +0200 |
parents | c8763b603b0e |
children | 596927722403 |
rev | line source |
---|---|
1485 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "PrecompiledHeaders.h" | |
34 #include "Logging.h" | |
35 | |
1488 | 36 #if ORTHANC_ENABLE_LOGGING == 1 |
1485 | 37 |
1489 | 38 |
39 #if ORTHANC_ENABLE_GOOGLE_LOG == 1 | |
40 | |
41 /********************************************************* | |
42 * Wrapper around Google Log | |
43 *********************************************************/ | |
44 | |
1485 | 45 namespace Orthanc |
46 { | |
47 namespace Logging | |
48 { | |
49 void Initialize() | |
50 { | |
51 // Initialize Google's logging library. | |
52 FLAGS_logtostderr = true; | |
53 FLAGS_minloglevel = 1; // Do not print LOG(INFO) by default | |
54 FLAGS_v = 0; // Do not print trace-level VLOG(1) by default | |
55 | |
56 google::InitGoogleLogging("Orthanc"); | |
57 } | |
58 | |
59 void Finalize() | |
60 { | |
61 google::ShutdownGoogleLogging(); | |
62 } | |
63 | |
64 void EnableInfoLevel(bool enabled) | |
65 { | |
66 FLAGS_minloglevel = (enabled ? 0 : 1); | |
67 } | |
68 | |
69 void EnableTraceLevel(bool enabled) | |
70 { | |
71 if (enabled) | |
72 { | |
73 FLAGS_minloglevel = 0; | |
74 FLAGS_v = 1; | |
75 } | |
76 else | |
77 { | |
78 FLAGS_v = 0; | |
79 } | |
80 } | |
1489 | 81 |
82 void SetTargetFolder(const std::string& path) | |
83 { | |
84 FLAGS_logtostderr = false; | |
85 FLAGS_log_dir = path; | |
86 } | |
87 } | |
88 } | |
89 | |
90 #else | |
91 | |
92 /********************************************************* | |
93 * Use internal logger, not Google Log | |
94 *********************************************************/ | |
95 | |
96 #include "OrthancException.h" | |
97 | |
98 #include <boost/filesystem.hpp> | |
99 #include <boost/thread.hpp> | |
100 | |
101 #if BOOST_HAS_DATE_TIME == 1 | |
102 # include <boost/date_time/posix_time/posix_time.hpp> | |
103 #else | |
104 # error Boost::date_time is required | |
1487
23083810d543
removal of unneeded static libraries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1485
diff
changeset
|
105 #endif |
1489 | 106 |
107 | |
108 namespace | |
109 { | |
110 struct NullStream : public std::ostream | |
111 { | |
112 NullStream() : std::ios(0), std::ostream(0) | |
113 { | |
114 } | |
115 }; | |
116 } | |
117 | |
118 | |
119 static boost::mutex mutex_; | |
120 static bool infoEnabled_ = false; | |
121 static bool traceEnabled_ = false; | |
122 static std::ostream& error_ = std::cerr; | |
123 static std::ostream& warning_ = std::cerr; | |
124 static std::ostream& info_ = std::cerr; | |
125 static std::ostream& trace_ = std::cerr; | |
126 static NullStream null_; | |
127 | |
128 namespace Orthanc | |
129 { | |
130 namespace Logging | |
131 { | |
132 void Initialize() | |
133 { | |
134 infoEnabled_ = false; | |
135 traceEnabled_ = false; | |
136 } | |
137 | |
138 void Finalize() | |
139 { | |
140 } | |
141 | |
142 void EnableInfoLevel(bool enabled) | |
143 { | |
144 boost::mutex::scoped_lock lock(mutex_); | |
145 infoEnabled_ = enabled; | |
146 } | |
147 | |
148 void EnableTraceLevel(bool enabled) | |
149 { | |
150 boost::mutex::scoped_lock lock(mutex_); | |
151 traceEnabled_ = enabled; | |
152 | |
153 if (enabled) | |
154 { | |
155 // Also enable the "INFO" level when trace-level debugging is | |
156 // enabled | |
157 infoEnabled_ = true; | |
158 } | |
159 } | |
160 | |
161 void SetTargetFolder(const std::string& path) | |
162 { | |
163 boost::mutex::scoped_lock lock(mutex_); | |
164 // TODO | |
165 } | |
166 | |
167 InternalLogger::InternalLogger(const char* level, | |
168 const char* file, | |
169 int line) : | |
170 lock_(mutex_) | |
171 { | |
172 char c; | |
173 | |
174 if (strcmp(level, "ERROR") == 0) | |
175 { | |
176 stream_ = &error_; | |
177 c = 'E'; | |
178 } | |
179 else if (strcmp(level, "WARNING") == 0) | |
180 { | |
181 stream_ = &warning_; | |
182 c = 'W'; | |
183 } | |
184 else if (strcmp(level, "INFO") == 0) | |
185 { | |
186 stream_ = infoEnabled_ ? &info_ : &null_; | |
187 c = 'I'; | |
188 } | |
189 else if (strcmp(level, "TRACE") == 0) | |
190 { | |
191 stream_ = traceEnabled_ ? &trace_ : &null_; | |
192 c = 'T'; | |
193 } | |
194 else | |
195 { | |
196 // Unknown logging level | |
197 throw OrthancException(ErrorCode_InternalError); | |
198 } | |
199 | |
200 if (stream_ != &null_) | |
201 { | |
202 boost::filesystem::path path(file); | |
203 boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); | |
204 boost::posix_time::time_duration duration = now.time_of_day(); | |
205 | |
206 /** | |
207 From Google Log documentation: | |
208 | |
209 Log lines have this form: | |
210 | |
211 Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... | |
212 | |
213 where the fields are defined as follows: | |
214 | |
215 L A single character, representing the log level (eg 'I' for INFO) | |
216 mm The month (zero padded; ie May is '05') | |
217 dd The day (zero padded) | |
218 hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds | |
219 threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux) | |
220 file The file name | |
221 line The line number | |
222 msg The user-supplied message | |
223 **/ | |
224 | |
225 char date[32]; | |
226 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", c, | |
227 now.date().month().as_number(), | |
228 now.date().day().as_number(), | |
229 duration.hours(), | |
230 duration.minutes(), | |
231 duration.seconds(), | |
232 static_cast<int>(duration.fractional_seconds())); | |
233 | |
234 *stream_ << date << path.filename().string() << ":" << line << "] "; | |
235 } | |
236 } | |
1485 | 237 } |
238 } | |
1488 | 239 |
240 #endif | |
1489 | 241 |
242 | |
243 #endif // ORTHANC_ENABLE_LOGGING |