Mercurial > hg > orthanc
comparison 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 |
comparison
equal
deleted
inserted
replaced
1488:c8763b603b0e | 1489:1389834e130f |
---|---|
33 #include "PrecompiledHeaders.h" | 33 #include "PrecompiledHeaders.h" |
34 #include "Logging.h" | 34 #include "Logging.h" |
35 | 35 |
36 #if ORTHANC_ENABLE_LOGGING == 1 | 36 #if ORTHANC_ENABLE_LOGGING == 1 |
37 | 37 |
38 | |
39 #if ORTHANC_ENABLE_GOOGLE_LOG == 1 | |
40 | |
41 /********************************************************* | |
42 * Wrapper around Google Log | |
43 *********************************************************/ | |
44 | |
38 namespace Orthanc | 45 namespace Orthanc |
39 { | 46 { |
40 namespace Logging | 47 namespace Logging |
41 { | 48 { |
42 #if ORTHANC_ENABLE_GOOGLE_LOG == 1 | |
43 void Initialize() | 49 void Initialize() |
44 { | 50 { |
45 // Initialize Google's logging library. | 51 // Initialize Google's logging library. |
46 FLAGS_logtostderr = true; | 52 FLAGS_logtostderr = true; |
47 FLAGS_minloglevel = 1; // Do not print LOG(INFO) by default | 53 FLAGS_minloglevel = 1; // Do not print LOG(INFO) by default |
70 else | 76 else |
71 { | 77 { |
72 FLAGS_v = 0; | 78 FLAGS_v = 0; |
73 } | 79 } |
74 } | 80 } |
75 #endif | 81 |
82 void SetTargetFolder(const std::string& path) | |
83 { | |
84 FLAGS_logtostderr = false; | |
85 FLAGS_log_dir = path; | |
86 } | |
76 } | 87 } |
77 } | 88 } |
78 | 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 | |
79 #endif | 105 #endif |
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 } | |
237 } | |
238 } | |
239 | |
240 #endif | |
241 | |
242 | |
243 #endif // ORTHANC_ENABLE_LOGGING |