Mercurial > hg > orthanc
annotate Core/Logging.cpp @ 1543:8b6d8f9b9f71
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 13 Aug 2015 17:26:46 +0200 |
parents | 46b2794042d6 |
children | 0dba274074eb |
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" | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
97 #include "Enumerations.h" |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
98 #include "Toolbox.h" |
1489 | 99 |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
100 #include <fstream> |
1489 | 101 #include <boost/filesystem.hpp> |
102 #include <boost/thread.hpp> | |
103 | |
104 #if BOOST_HAS_DATE_TIME == 1 | |
105 # include <boost/date_time/posix_time/posix_time.hpp> | |
106 #else | |
107 # error Boost::date_time is required | |
1487
23083810d543
removal of unneeded static libraries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1485
diff
changeset
|
108 #endif |
1489 | 109 |
110 | |
111 namespace | |
112 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
113 struct LoggingState |
1489 | 114 { |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
115 bool infoEnabled_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
116 bool traceEnabled_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
117 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
118 std::ostream* error_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
119 std::ostream* warning_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
120 std::ostream* info_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
121 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
122 std::auto_ptr<std::ofstream> errorFile_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
123 std::auto_ptr<std::ofstream> warningFile_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
124 std::auto_ptr<std::ofstream> infoFile_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
125 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
126 LoggingState() : |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
127 infoEnabled_(false), |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
128 traceEnabled_(false), |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
129 error_(&std::cerr), |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
130 warning_(&std::cerr), |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
131 info_(&std::cerr) |
1489 | 132 { |
133 } | |
134 }; | |
135 } | |
136 | |
137 | |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
138 |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
139 static std::auto_ptr<LoggingState> loggingState_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
140 static boost::mutex loggingMutex_; |
1489 | 141 |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
142 |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
143 |
1489 | 144 namespace Orthanc |
145 { | |
146 namespace Logging | |
147 { | |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
148 static void GetLogPath(boost::filesystem::path& log, |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
149 boost::filesystem::path& link, |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
150 const char* level, |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
151 const std::string& directory) |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
152 { |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
153 /** |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
154 From Google Log documentation: |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
155 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
156 Unless otherwise specified, logs will be written to the filename |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
157 "<program name>.<hostname>.<user name>.log.<severity level>.", |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
158 followed by the date, time, and pid (you can't prevent the date, |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
159 time, and pid from being in the filename). |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
160 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
161 In this implementation : "hostname" and "username" are not used |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
162 **/ |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
163 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
164 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
165 boost::filesystem::path root(directory); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
166 boost::filesystem::path exe(Toolbox::GetPathToExecutable()); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
167 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
168 if (!boost::filesystem::exists(root) || |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
169 !boost::filesystem::is_directory(root)) |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
170 { |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
171 throw OrthancException(ErrorCode_CannotWriteFile); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
172 } |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
173 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
174 char date[64]; |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
175 sprintf(date, "%04d%02d%02d-%02d%02d%02d.%d", |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
176 static_cast<int>(now.date().year()), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
177 now.date().month().as_number(), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
178 now.date().day().as_number(), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
179 now.time_of_day().hours(), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
180 now.time_of_day().minutes(), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
181 now.time_of_day().seconds(), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
182 Toolbox::GetProcessId()); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
183 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
184 std::string programName = exe.filename().replace_extension("").string(); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
185 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
186 log = (root / (programName + ".log." + |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
187 std::string(level) + "." + |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
188 std::string(date))); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
189 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
190 link = (root / (programName + "." + std::string(level))); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
191 } |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
192 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
193 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
194 static void PrepareLogFile(std::ostream*& stream, |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
195 std::auto_ptr<std::ofstream>& file, |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
196 const char* level, |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
197 const std::string& directory) |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
198 { |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
199 boost::filesystem::path log, link; |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
200 GetLogPath(log, link, level, directory); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
201 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
202 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
203 boost::filesystem::remove(link); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
204 boost::filesystem::create_symlink(log.filename(), link); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
205 #endif |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
206 |
1494 | 207 file.reset(new std::ofstream(log.string().c_str())); |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
208 stream = file.get(); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
209 } |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
210 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
211 |
1489 | 212 void Initialize() |
213 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
214 boost::mutex::scoped_lock lock(loggingMutex_); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
215 loggingState_.reset(new LoggingState); |
1489 | 216 } |
217 | |
218 void Finalize() | |
219 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
220 boost::mutex::scoped_lock lock(loggingMutex_); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
221 loggingState_.reset(NULL); |
1489 | 222 } |
223 | |
224 void EnableInfoLevel(bool enabled) | |
225 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
226 boost::mutex::scoped_lock lock(loggingMutex_); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
227 assert(loggingState_.get() != NULL); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
228 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
229 loggingState_->infoEnabled_ = enabled; |
1489 | 230 } |
231 | |
232 void EnableTraceLevel(bool enabled) | |
233 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
234 boost::mutex::scoped_lock lock(loggingMutex_); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
235 assert(loggingState_.get() != NULL); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
236 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
237 loggingState_->traceEnabled_ = enabled; |
1489 | 238 |
239 if (enabled) | |
240 { | |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
241 // Also enable the "INFO" level when trace-level debugging is enabled |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
242 loggingState_->infoEnabled_ = true; |
1489 | 243 } |
244 } | |
245 | |
246 void SetTargetFolder(const std::string& path) | |
247 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
248 boost::mutex::scoped_lock lock(loggingMutex_); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
249 assert(loggingState_.get() != NULL); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
250 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
251 PrepareLogFile(loggingState_->error_, loggingState_->errorFile_, "ERROR", path); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
252 PrepareLogFile(loggingState_->warning_, loggingState_->warningFile_, "WARNING", path); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
253 PrepareLogFile(loggingState_->info_, loggingState_->infoFile_, "INFO", path); |
1489 | 254 } |
255 | |
256 InternalLogger::InternalLogger(const char* level, | |
257 const char* file, | |
258 int line) : | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
259 lock_(loggingMutex_), |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
260 stream_(&null_) // By default, logging to "/dev/null" is simulated |
1489 | 261 { |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
262 if (loggingState_.get() == NULL) |
1489 | 263 { |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
264 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n"); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
265 return; |
1489 | 266 } |
267 | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
268 LogLevel l = StringToLogLevel(level); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
269 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
270 if ((l == LogLevel_Info && !loggingState_->infoEnabled_) || |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
271 (l == LogLevel_Trace && !loggingState_->traceEnabled_)) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
272 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
273 // This logging level is disabled, directly exit and unlock |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
274 // the mutex to speed-up things. The stream is set to "/dev/null" |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
275 lock_.unlock(); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
276 return; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
277 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
278 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
279 // Compute the header of the line, temporary release the lock as |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
280 // this is a time-consuming operation |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
281 lock_.unlock(); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
282 std::string header; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
283 |
1489 | 284 { |
285 boost::filesystem::path path(file); | |
286 boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); | |
287 boost::posix_time::time_duration duration = now.time_of_day(); | |
288 | |
289 /** | |
290 From Google Log documentation: | |
291 | |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
292 "Log lines have this form: |
1489 | 293 |
294 Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... | |
295 | |
296 where the fields are defined as follows: | |
297 | |
298 L A single character, representing the log level (eg 'I' for INFO) | |
299 mm The month (zero padded; ie May is '05') | |
300 dd The day (zero padded) | |
301 hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds | |
302 threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux) | |
303 file The file name | |
304 line The line number | |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
305 msg The user-supplied message" |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
306 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
307 In this implementation, "threadid" is not printed. |
1489 | 308 **/ |
309 | |
310 char date[32]; | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
311 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
312 level[0], |
1489 | 313 now.date().month().as_number(), |
314 now.date().day().as_number(), | |
315 duration.hours(), | |
316 duration.minutes(), | |
317 duration.seconds(), | |
318 static_cast<int>(duration.fractional_seconds())); | |
319 | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
320 header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast<std::string>(line) + "] "; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
321 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
322 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
323 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
324 // The header is computed, we now re-lock the mutex to access |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
325 // the stream objects. Pay attention that "loggingState_", |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
326 // "infoEnabled_" or "traceEnabled_" might have changed while |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
327 // the mutex was unlocked. |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
328 lock_.lock(); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
329 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
330 if (loggingState_.get() == NULL) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
331 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
332 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n"); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
333 return; |
1489 | 334 } |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
335 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
336 switch (l) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
337 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
338 case LogLevel_Error: |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
339 stream_ = loggingState_->error_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
340 break; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
341 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
342 case LogLevel_Warning: |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
343 stream_ = loggingState_->warning_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
344 break; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
345 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
346 case LogLevel_Info: |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
347 if (loggingState_->infoEnabled_) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
348 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
349 stream_ = loggingState_->info_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
350 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
351 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
352 break; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
353 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
354 case LogLevel_Trace: |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
355 if (loggingState_->traceEnabled_) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
356 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
357 stream_ = loggingState_->info_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
358 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
359 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
360 break; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
361 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
362 default: |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
363 throw OrthancException(ErrorCode_InternalError); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
364 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
365 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
366 if (stream_ == &null_) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
367 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
368 // The logging is disabled for this level. The stream is the |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
369 // "null_" member of this object, so we can release the global |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
370 // mutex. |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
371 lock_.unlock(); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
372 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
373 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
374 (*stream_) << header; |
1489 | 375 } |
1507 | 376 |
377 | |
378 InternalLogger::~InternalLogger() | |
379 { | |
380 if (stream_ != &null_) | |
381 { | |
382 #if defined(_WIN32) | |
383 *stream_ << "\r\n"; | |
384 #else | |
385 *stream_ << "\n"; | |
386 #endif | |
387 | |
388 stream_->flush(); | |
389 } | |
390 } | |
391 | |
392 | |
1485 | 393 } |
394 } | |
1488 | 395 |
396 #endif | |
1489 | 397 |
398 | |
399 #endif // ORTHANC_ENABLE_LOGGING |