Mercurial > hg > orthanc
comparison OrthancFramework/Sources/Logging.h @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Core/Logging.h@c783f4f29390 |
children | bf7b9edf6b81 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
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 #pragma once | |
35 | |
36 // To have ORTHANC_ENABLE_LOGGING defined if using the shared library | |
37 #include "OrthancFramework.h" | |
38 | |
39 #include <iostream> | |
40 | |
41 #if !defined(ORTHANC_ENABLE_LOGGING) | |
42 # error The macro ORTHANC_ENABLE_LOGGING must be defined | |
43 #endif | |
44 | |
45 #if !defined(ORTHANC_ENABLE_LOGGING_STDIO) | |
46 # if ORTHANC_ENABLE_LOGGING == 1 | |
47 # error The macro ORTHANC_ENABLE_LOGGING_STDIO must be defined | |
48 # else | |
49 # define ORTHANC_ENABLE_LOGGING_STDIO 0 | |
50 # endif | |
51 #endif | |
52 | |
53 | |
54 namespace Orthanc | |
55 { | |
56 namespace Logging | |
57 { | |
58 enum LogLevel | |
59 { | |
60 LogLevel_ERROR, | |
61 LogLevel_WARNING, | |
62 LogLevel_INFO, | |
63 LogLevel_TRACE | |
64 }; | |
65 | |
66 ORTHANC_PUBLIC const char* EnumerationToString(LogLevel level); | |
67 | |
68 ORTHANC_PUBLIC LogLevel StringToLogLevel(const char* level); | |
69 | |
70 // "pluginContext" must be of type "OrthancPluginContext" | |
71 ORTHANC_PUBLIC void InitializePluginContext(void* pluginContext); | |
72 | |
73 ORTHANC_PUBLIC void Initialize(); | |
74 | |
75 ORTHANC_PUBLIC void Finalize(); | |
76 | |
77 ORTHANC_PUBLIC void Reset(); | |
78 | |
79 ORTHANC_PUBLIC void Flush(); | |
80 | |
81 ORTHANC_PUBLIC void EnableInfoLevel(bool enabled); | |
82 | |
83 ORTHANC_PUBLIC void EnableTraceLevel(bool enabled); | |
84 | |
85 ORTHANC_PUBLIC bool IsTraceLevelEnabled(); | |
86 | |
87 ORTHANC_PUBLIC bool IsInfoLevelEnabled(); | |
88 | |
89 ORTHANC_PUBLIC void SetTargetFile(const std::string& path); | |
90 | |
91 ORTHANC_PUBLIC void SetTargetFolder(const std::string& path); | |
92 | |
93 struct NullStream : public std::ostream | |
94 { | |
95 NullStream() : | |
96 std::ios(0), | |
97 std::ostream(0) | |
98 { | |
99 } | |
100 | |
101 template <typename T> | |
102 std::ostream& operator<< (const T& message) | |
103 { | |
104 return *this; | |
105 } | |
106 }; | |
107 } | |
108 } | |
109 | |
110 | |
111 #if ORTHANC_ENABLE_LOGGING != 1 | |
112 # define LOG(level) ::Orthanc::Logging::NullStream() | |
113 # define VLOG(level) ::Orthanc::Logging::NullStream() | |
114 #else /* ORTHANC_ENABLE_LOGGING == 1 */ | |
115 # define LOG(level) ::Orthanc::Logging::InternalLogger \ | |
116 (::Orthanc::Logging::LogLevel_ ## level, __FILE__, __LINE__) | |
117 # define VLOG(level) ::Orthanc::Logging::InternalLogger \ | |
118 (::Orthanc::Logging::LogLevel_TRACE, __FILE__, __LINE__) | |
119 #endif | |
120 | |
121 | |
122 | |
123 #if (ORTHANC_ENABLE_LOGGING == 1 && \ | |
124 ORTHANC_ENABLE_LOGGING_STDIO == 1) | |
125 // This is notably for WebAssembly | |
126 | |
127 #include <boost/lexical_cast.hpp> | |
128 #include <boost/noncopyable.hpp> | |
129 #include <sstream> | |
130 | |
131 namespace Orthanc | |
132 { | |
133 namespace Logging | |
134 { | |
135 class ORTHANC_PUBLIC InternalLogger : public boost::noncopyable | |
136 { | |
137 private: | |
138 LogLevel level_; | |
139 std::stringstream messageStream_; | |
140 | |
141 public: | |
142 InternalLogger(LogLevel level, | |
143 const char* file /* ignored */, | |
144 int line /* ignored */) : | |
145 level_(level) | |
146 { | |
147 } | |
148 | |
149 ~InternalLogger(); | |
150 | |
151 template <typename T> | |
152 std::ostream& operator<< (const T& message) | |
153 { | |
154 return messageStream_ << boost::lexical_cast<std::string>(message); | |
155 } | |
156 }; | |
157 } | |
158 } | |
159 | |
160 #endif | |
161 | |
162 | |
163 | |
164 #if (ORTHANC_ENABLE_LOGGING == 1 && \ | |
165 ORTHANC_ENABLE_LOGGING_STDIO == 0) | |
166 | |
167 #include "Compatibility.h" // For std::unique_ptr<> | |
168 | |
169 #include <boost/lexical_cast.hpp> | |
170 #include <boost/noncopyable.hpp> | |
171 #include <boost/thread/mutex.hpp> | |
172 #include <sstream> | |
173 | |
174 namespace Orthanc | |
175 { | |
176 namespace Logging | |
177 { | |
178 class ORTHANC_PUBLIC InternalLogger : public boost::noncopyable | |
179 { | |
180 private: | |
181 boost::mutex::scoped_lock lock_; | |
182 LogLevel level_; | |
183 std::unique_ptr<std::stringstream> pluginStream_; | |
184 std::ostream* stream_; | |
185 | |
186 public: | |
187 InternalLogger(LogLevel level, | |
188 const char* file, | |
189 int line); | |
190 | |
191 ~InternalLogger(); | |
192 | |
193 template <typename T> | |
194 std::ostream& operator<< (const T& message) | |
195 { | |
196 return (*stream_) << boost::lexical_cast<std::string>(message); | |
197 } | |
198 }; | |
199 | |
200 | |
201 /** | |
202 * Set custom logging streams for the error, warning and info | |
203 * logs. This function may not be called if a log file or folder | |
204 * has been set beforehand. All three references must be valid. | |
205 * | |
206 * Please ensure the supplied streams remain alive and valid as | |
207 * long as logging calls are performed. In order to prevent | |
208 * dangling pointer usage, it is mandatory to call | |
209 * Orthanc::Logging::Reset() before the stream objects are | |
210 * destroyed and the references become invalid. | |
211 * | |
212 * This function must only be used by unit tests. It is ignored if | |
213 * InitializePluginContext() was called. | |
214 **/ | |
215 ORTHANC_PUBLIC void SetErrorWarnInfoLoggingStreams(std::ostream& errorStream, | |
216 std::ostream& warningStream, | |
217 std::ostream& infoStream); | |
218 } | |
219 } | |
220 | |
221 #endif |