comparison Resources/Orthanc/Core/Logging.h @ 200:03afbee0cc7b

integration of Orthanc core into Stone
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Mar 2018 11:04:03 +0100
parents
children e7f90aba3c97
comparison
equal deleted inserted replaced
199:dabe9982fca3 200:03afbee0cc7b
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-2018 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 #include <iostream>
37
38 #if !defined(ORTHANC_ENABLE_LOGGING)
39 # error The macro ORTHANC_ENABLE_LOGGING must be defined
40 #endif
41
42 #if !defined(ORTHANC_ENABLE_LOGGING_PLUGIN)
43 # if ORTHANC_ENABLE_LOGGING == 1
44 # error The macro ORTHANC_ENABLE_LOGGING_PLUGIN must be defined
45 # else
46 # define ORTHANC_ENABLE_LOGGING_PLUGIN 0
47 # endif
48 #endif
49
50 #if !defined(ORTHANC_ENABLE_LOGGING_STDIO)
51 # if ORTHANC_ENABLE_LOGGING == 1
52 # error The macro ORTHANC_ENABLE_LOGGING_STDIO must be defined
53 # else
54 # define ORTHANC_ENABLE_LOGGING_STDIO 0
55 # endif
56 #endif
57
58 #if ORTHANC_ENABLE_LOGGING_PLUGIN == 1
59 # include <orthanc/OrthancCPlugin.h>
60 #endif
61
62 namespace Orthanc
63 {
64 namespace Logging
65 {
66 #if ORTHANC_ENABLE_LOGGING_PLUGIN == 1
67 void Initialize(OrthancPluginContext* context);
68 #else
69 void Initialize();
70 #endif
71
72 void Finalize();
73
74 void Reset();
75
76 void Flush();
77
78 void EnableInfoLevel(bool enabled);
79
80 void EnableTraceLevel(bool enabled);
81
82 void SetTargetFile(const std::string& path);
83
84 void SetTargetFolder(const std::string& path);
85
86 struct NullStream : public std::ostream
87 {
88 NullStream() :
89 std::ios(0),
90 std::ostream(0)
91 {
92 }
93
94 std::ostream& operator<< (const std::string& message)
95 {
96 return *this;
97 }
98
99 // This overload fixes build problems with Visual Studio 2015
100 std::ostream& operator<< (const char* message)
101 {
102 return *this;
103 }
104 };
105 }
106 }
107
108
109 #if ORTHANC_ENABLE_LOGGING != 1
110
111 # define LOG(level) ::Orthanc::Logging::NullStream()
112 # define VLOG(level) ::Orthanc::Logging::NullStream()
113
114
115 #elif (ORTHANC_ENABLE_LOGGING_PLUGIN == 1 || \
116 ORTHANC_ENABLE_LOGGING_STDIO == 1)
117
118 # include <boost/noncopyable.hpp>
119 # include <boost/lexical_cast.hpp>
120 # define LOG(level) ::Orthanc::Logging::InternalLogger \
121 (::Orthanc::Logging::level, __FILE__, __LINE__)
122 # define VLOG(level) ::Orthanc::Logging::InternalLogger \
123 (::Orthanc::Logging::TRACE, __FILE__, __LINE__)
124
125 namespace Orthanc
126 {
127 namespace Logging
128 {
129 enum Level
130 {
131 ERROR,
132 WARNING,
133 INFO,
134 TRACE
135 };
136
137 class InternalLogger : public boost::noncopyable
138 {
139 private:
140 Level level_;
141 std::string message_;
142
143 public:
144 InternalLogger(Level level,
145 const char* file,
146 int line);
147
148 ~InternalLogger();
149
150 template <typename T>
151 InternalLogger& operator<< (T message)
152 {
153 message_ += boost::lexical_cast<std::string>(message);
154 return *this;
155 }
156 };
157 }
158 }
159
160
161
162
163 #else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 &&
164 ORTHANC_ENABLE_LOGGING_STDIO == 0 &&
165 ORTHANC_ENABLE_LOGGING == 1 */
166
167 # include <boost/thread/mutex.hpp>
168 # define LOG(level) ::Orthanc::Logging::InternalLogger(#level, __FILE__, __LINE__)
169 # define VLOG(level) ::Orthanc::Logging::InternalLogger("TRACE", __FILE__, __LINE__)
170
171 namespace Orthanc
172 {
173 namespace Logging
174 {
175 class InternalLogger
176 {
177 private:
178 boost::mutex::scoped_lock lock_;
179 NullStream null_;
180 std::ostream* stream_;
181
182 public:
183 InternalLogger(const char* level,
184 const char* file,
185 int line);
186
187 ~InternalLogger();
188
189 std::ostream& operator<< (const std::string& message)
190 {
191 return (*stream_) << message;
192 }
193
194 // This overload fixes build problems with Visual Studio 2015
195 std::ostream& operator<< (const char* message)
196 {
197 return (*stream_) << message;
198 }
199 };
200 }
201 }
202
203 #endif // ORTHANC_ENABLE_LOGGING