Mercurial > hg > orthanc
annotate Core/Logging.cpp @ 1938:48ad54f7b21f
endianness
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Mar 2016 10:24:51 +0100 |
parents | b1291df2f780 |
children | e39a2657f1c6 |
rev | line source |
---|---|
1485 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1485 | 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 | |
1551
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
36 #if ORTHANC_ENABLE_LOGGING != 1 |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
37 |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
38 namespace Orthanc |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
39 { |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
40 namespace Logging |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
41 { |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
42 void Initialize() |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
43 { |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
44 } |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
45 |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
46 void Finalize() |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
47 { |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
48 } |
1485 | 49 |
1551
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
50 void EnableInfoLevel(bool enabled) |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
51 { |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
52 } |
1489 | 53 |
1551
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
54 void EnableTraceLevel(bool enabled) |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
55 { |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
56 } |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
57 |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
58 void SetTargetFolder(const std::string& path) |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
59 { |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
60 } |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
61 } |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
62 } |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
63 |
0dba274074eb
standalone logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1507
diff
changeset
|
64 #elif ORTHANC_ENABLE_GOOGLE_LOG == 1 |
1489 | 65 |
66 /********************************************************* | |
67 * Wrapper around Google Log | |
68 *********************************************************/ | |
69 | |
1485 | 70 namespace Orthanc |
71 { | |
72 namespace Logging | |
73 { | |
74 void Initialize() | |
75 { | |
76 // Initialize Google's logging library. | |
77 FLAGS_logtostderr = true; | |
78 FLAGS_minloglevel = 1; // Do not print LOG(INFO) by default | |
79 FLAGS_v = 0; // Do not print trace-level VLOG(1) by default | |
80 | |
81 google::InitGoogleLogging("Orthanc"); | |
82 } | |
83 | |
84 void Finalize() | |
85 { | |
86 google::ShutdownGoogleLogging(); | |
87 } | |
88 | |
89 void EnableInfoLevel(bool enabled) | |
90 { | |
91 FLAGS_minloglevel = (enabled ? 0 : 1); | |
92 } | |
93 | |
94 void EnableTraceLevel(bool enabled) | |
95 { | |
96 if (enabled) | |
97 { | |
98 FLAGS_minloglevel = 0; | |
99 FLAGS_v = 1; | |
100 } | |
101 else | |
102 { | |
103 FLAGS_v = 0; | |
104 } | |
105 } | |
1489 | 106 |
107 void SetTargetFolder(const std::string& path) | |
108 { | |
109 FLAGS_logtostderr = false; | |
110 FLAGS_log_dir = path; | |
111 } | |
112 } | |
113 } | |
114 | |
115 #else | |
116 | |
117 /********************************************************* | |
118 * Use internal logger, not Google Log | |
119 *********************************************************/ | |
120 | |
121 #include "OrthancException.h" | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
122 #include "Enumerations.h" |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
123 #include "Toolbox.h" |
1489 | 124 |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
125 #include <fstream> |
1489 | 126 #include <boost/filesystem.hpp> |
127 #include <boost/thread.hpp> | |
128 | |
129 #if BOOST_HAS_DATE_TIME == 1 | |
130 # include <boost/date_time/posix_time/posix_time.hpp> | |
131 #else | |
132 # error Boost::date_time is required | |
1487
23083810d543
removal of unneeded static libraries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1485
diff
changeset
|
133 #endif |
1489 | 134 |
135 | |
136 namespace | |
137 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
138 struct LoggingState |
1489 | 139 { |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
140 bool infoEnabled_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
141 bool traceEnabled_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
142 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
143 std::ostream* error_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
144 std::ostream* warning_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
145 std::ostream* info_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
146 |
1731
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
147 std::auto_ptr<std::ofstream> file_; |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
148 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
149 LoggingState() : |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
150 infoEnabled_(false), |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
151 traceEnabled_(false), |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
152 error_(&std::cerr), |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
153 warning_(&std::cerr), |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
154 info_(&std::cerr) |
1489 | 155 { |
156 } | |
157 }; | |
158 } | |
159 | |
160 | |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
161 |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
162 static std::auto_ptr<LoggingState> loggingState_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
163 static boost::mutex loggingMutex_; |
1489 | 164 |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
165 |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
166 |
1489 | 167 namespace Orthanc |
168 { | |
169 namespace Logging | |
170 { | |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
171 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
|
172 boost::filesystem::path& link, |
1731
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
173 const std::string& suffix, |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
174 const std::string& directory) |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
175 { |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
176 /** |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
177 From Google Log documentation: |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
178 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
179 Unless otherwise specified, logs will be written to the filename |
1731
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
180 "<program name>.<hostname>.<user name>.log<suffix>.", |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
181 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
|
182 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
|
183 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
184 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
|
185 **/ |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
186 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
187 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
|
188 boost::filesystem::path root(directory); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
189 boost::filesystem::path exe(Toolbox::GetPathToExecutable()); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
190 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
191 if (!boost::filesystem::exists(root) || |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
192 !boost::filesystem::is_directory(root)) |
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 throw OrthancException(ErrorCode_CannotWriteFile); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
195 } |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
196 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
197 char date[64]; |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
198 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
|
199 static_cast<int>(now.date().year()), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
200 now.date().month().as_number(), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
201 now.date().day().as_number(), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
202 now.time_of_day().hours(), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
203 now.time_of_day().minutes(), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
204 now.time_of_day().seconds(), |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
205 Toolbox::GetProcessId()); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
206 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
207 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
|
208 |
1731
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
209 log = (root / (programName + ".log" + suffix + "." + std::string(date))); |
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
210 link = (root / (programName + ".log" + suffix)); |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
211 } |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
212 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
213 |
1731
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
214 static void PrepareLogFile(std::auto_ptr<std::ofstream>& file, |
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
215 const std::string& suffix, |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
216 const std::string& directory) |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
217 { |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
218 boost::filesystem::path log, link; |
1731
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
219 GetLogPath(log, link, suffix, directory); |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
220 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
221 #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
|
222 boost::filesystem::remove(link); |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
223 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
|
224 #endif |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
225 |
1494 | 226 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
|
227 } |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
228 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
229 |
1489 | 230 void Initialize() |
231 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
232 boost::mutex::scoped_lock lock(loggingMutex_); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
233 loggingState_.reset(new LoggingState); |
1489 | 234 } |
235 | |
236 void Finalize() | |
237 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
238 boost::mutex::scoped_lock lock(loggingMutex_); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
239 loggingState_.reset(NULL); |
1489 | 240 } |
241 | |
242 void EnableInfoLevel(bool enabled) | |
243 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
244 boost::mutex::scoped_lock lock(loggingMutex_); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
245 assert(loggingState_.get() != NULL); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
246 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
247 loggingState_->infoEnabled_ = enabled; |
1489 | 248 } |
249 | |
250 void EnableTraceLevel(bool enabled) | |
251 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
252 boost::mutex::scoped_lock lock(loggingMutex_); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
253 assert(loggingState_.get() != NULL); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
254 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
255 loggingState_->traceEnabled_ = enabled; |
1489 | 256 |
257 if (enabled) | |
258 { | |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
259 // 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
|
260 loggingState_->infoEnabled_ = true; |
1489 | 261 } |
262 } | |
263 | |
264 void SetTargetFolder(const std::string& path) | |
265 { | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
266 boost::mutex::scoped_lock lock(loggingMutex_); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
267 assert(loggingState_.get() != NULL); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
268 |
1731
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
269 PrepareLogFile(loggingState_->file_, "" /* no suffix */, path); |
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
270 |
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
271 loggingState_->warning_ = loggingState_->file_.get(); |
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
272 loggingState_->error_ = loggingState_->file_.get(); |
b6f656a0bf2c
"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1551
diff
changeset
|
273 loggingState_->info_ = loggingState_->file_.get(); |
1489 | 274 } |
275 | |
276 InternalLogger::InternalLogger(const char* level, | |
277 const char* file, | |
278 int line) : | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
279 lock_(loggingMutex_), |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
280 stream_(&null_) // By default, logging to "/dev/null" is simulated |
1489 | 281 { |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
282 if (loggingState_.get() == NULL) |
1489 | 283 { |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
284 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
|
285 return; |
1489 | 286 } |
287 | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
288 LogLevel l = StringToLogLevel(level); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
289 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
290 if ((l == LogLevel_Info && !loggingState_->infoEnabled_) || |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
291 (l == LogLevel_Trace && !loggingState_->traceEnabled_)) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
292 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
293 // 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
|
294 // 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
|
295 lock_.unlock(); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
296 return; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
297 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
298 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
299 // 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
|
300 // this is a time-consuming operation |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
301 lock_.unlock(); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
302 std::string header; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
303 |
1489 | 304 { |
305 boost::filesystem::path path(file); | |
306 boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); | |
307 boost::posix_time::time_duration duration = now.time_of_day(); | |
308 | |
309 /** | |
310 From Google Log documentation: | |
311 | |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
312 "Log lines have this form: |
1489 | 313 |
314 Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... | |
315 | |
316 where the fields are defined as follows: | |
317 | |
318 L A single character, representing the log level (eg 'I' for INFO) | |
319 mm The month (zero padded; ie May is '05') | |
320 dd The day (zero padded) | |
321 hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds | |
322 threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux) | |
323 file The file name | |
324 line The line number | |
1490
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
325 msg The user-supplied message" |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
326 |
596927722403
support of --logdir by the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1489
diff
changeset
|
327 In this implementation, "threadid" is not printed. |
1489 | 328 **/ |
329 | |
330 char date[32]; | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
331 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
|
332 level[0], |
1489 | 333 now.date().month().as_number(), |
334 now.date().day().as_number(), | |
335 duration.hours(), | |
336 duration.minutes(), | |
337 duration.seconds(), | |
338 static_cast<int>(duration.fractional_seconds())); | |
339 | |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
340 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
|
341 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
342 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
343 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
344 // 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
|
345 // 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
|
346 // "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
|
347 // the mutex was unlocked. |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
348 lock_.lock(); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
349 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
350 if (loggingState_.get() == NULL) |
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 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
|
353 return; |
1489 | 354 } |
1495
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
355 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
356 switch (l) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
357 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
358 case LogLevel_Error: |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
359 stream_ = loggingState_->error_; |
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 case LogLevel_Warning: |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
363 stream_ = loggingState_->warning_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
364 break; |
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 case LogLevel_Info: |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
367 if (loggingState_->infoEnabled_) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
368 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
369 stream_ = loggingState_->info_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
370 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
371 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
372 break; |
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 case LogLevel_Trace: |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
375 if (loggingState_->traceEnabled_) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
376 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
377 stream_ = loggingState_->info_; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
378 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
379 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
380 break; |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
381 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
382 default: |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
383 throw OrthancException(ErrorCode_InternalError); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
384 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
385 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
386 if (stream_ == &null_) |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
387 { |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
388 // 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
|
389 // "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
|
390 // mutex. |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
391 lock_.unlock(); |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
392 } |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
393 |
fbe40117eb21
improve the performance of the internal logger
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1494
diff
changeset
|
394 (*stream_) << header; |
1489 | 395 } |
1507 | 396 |
397 | |
398 InternalLogger::~InternalLogger() | |
399 { | |
400 if (stream_ != &null_) | |
401 { | |
402 #if defined(_WIN32) | |
403 *stream_ << "\r\n"; | |
404 #else | |
405 *stream_ << "\n"; | |
406 #endif | |
407 | |
408 stream_->flush(); | |
409 } | |
410 } | |
411 | |
412 | |
1485 | 413 } |
414 } | |
1488 | 415 |
1489 | 416 #endif // ORTHANC_ENABLE_LOGGING |