comparison OrthancFramework/Sources/Logging.h @ 5807:8279eaab0d1d attach-custom-data

merged default -> attach-custom-data
author Alain Mazy <am@orthanc.team>
date Tue, 24 Sep 2024 11:39:52 +0200
parents f7adfb22e20e
children
comparison
equal deleted inserted replaced
5085:79f98ee4f04b 5807:8279eaab0d1d
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium 4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium 5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium 6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 * 8 *
8 * This program is free software: you can redistribute it and/or 9 * This program is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License 10 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3 of 11 * as published by the Free Software Foundation, either version 3 of
11 * the License, or (at your option) any later version. 12 * the License, or (at your option) any later version.
26 // To have ORTHANC_ENABLE_LOGGING defined if using the shared library 27 // To have ORTHANC_ENABLE_LOGGING defined if using the shared library
27 #include "OrthancFramework.h" 28 #include "OrthancFramework.h"
28 #include "Compatibility.h" 29 #include "Compatibility.h"
29 30
30 #include <iostream> 31 #include <iostream>
32 #include <stdint.h>
31 33
32 #if !defined(ORTHANC_ENABLE_LOGGING) 34 #if !defined(ORTHANC_ENABLE_LOGGING)
33 # error The macro ORTHANC_ENABLE_LOGGING must be defined 35 # error The macro ORTHANC_ENABLE_LOGGING must be defined
34 #endif 36 #endif
35 37
44 46
45 namespace Orthanc 47 namespace Orthanc
46 { 48 {
47 namespace Logging 49 namespace Logging
48 { 50 {
51 // Note: these values must match the ones in OrthancCPlugin.h
49 enum LogLevel 52 enum LogLevel
50 { 53 {
51 LogLevel_ERROR, 54 LogLevel_ERROR = 0,
52 LogLevel_WARNING, 55 LogLevel_WARNING = 1,
53 LogLevel_INFO, 56 LogLevel_INFO = 2,
54 LogLevel_TRACE 57 LogLevel_TRACE = 3
55 }; 58 };
56 59
57 /** 60 /**
58 * NB: The log level for each category is encoded as a bit 61 * NB: The log level for each category is encoded as a bit
59 * mask. As a consequence, there can be up to 31 log categories 62 * mask. As a consequence, there can be up to 31 log categories
60 * (not 32, as the value GENERIC is reserved for the log commands 63 * (not 32, as the value GENERIC is reserved for the log commands
61 * that don't fall in a specific category). 64 * that don't fall in a specific category).
65 * Note: these values must match the ones in OrthancCPlugin.h
62 **/ 66 **/
63 enum LogCategory 67 enum LogCategory
64 { 68 {
65 LogCategory_GENERIC = (1 << 0), 69 LogCategory_GENERIC = (1 << 0),
66 LogCategory_PLUGINS = (1 << 1), 70 LogCategory_PLUGINS = (1 << 1),
76 ORTHANC_PUBLIC LogLevel StringToLogLevel(const char* level); 80 ORTHANC_PUBLIC LogLevel StringToLogLevel(const char* level);
77 81
78 // "pluginContext" must be of type "OrthancPluginContext" 82 // "pluginContext" must be of type "OrthancPluginContext"
79 ORTHANC_PUBLIC void InitializePluginContext(void* pluginContext); 83 ORTHANC_PUBLIC void InitializePluginContext(void* pluginContext);
80 84
85 ORTHANC_PUBLIC void InitializePluginContext(void* pluginContext,
86 const std::string& pluginName);
87
81 ORTHANC_PUBLIC void Initialize(); 88 ORTHANC_PUBLIC void Initialize();
82 89
83 ORTHANC_PUBLIC void Finalize(); 90 ORTHANC_PUBLIC void Finalize();
84 91
85 ORTHANC_PUBLIC void Reset(); 92 ORTHANC_PUBLIC void Reset();
86 93
87 ORTHANC_PUBLIC void Flush(); 94 ORTHANC_PUBLIC void Flush();
95
96 ORTHANC_PUBLIC void SetCurrentThreadName(const std::string& name);
97
98 ORTHANC_PUBLIC bool HasCurrentThreadName();
99
100 ORTHANC_PUBLIC void EnableThreadNames(bool enabled);
88 101
89 ORTHANC_PUBLIC void EnableInfoLevel(bool enabled); 102 ORTHANC_PUBLIC void EnableInfoLevel(bool enabled);
90 103
91 ORTHANC_PUBLIC void EnableTraceLevel(bool enabled); 104 ORTHANC_PUBLIC void EnableTraceLevel(bool enabled);
92 105
154 167
155 #if ORTHANC_ENABLE_LOGGING != 1 168 #if ORTHANC_ENABLE_LOGGING != 1
156 # define LOG(level) ::Orthanc::Logging::NullStream() 169 # define LOG(level) ::Orthanc::Logging::NullStream()
157 # define VLOG(unused) ::Orthanc::Logging::NullStream() 170 # define VLOG(unused) ::Orthanc::Logging::NullStream()
158 # define CLOG(level, category) ::Orthanc::Logging::NullStream() 171 # define CLOG(level, category) ::Orthanc::Logging::NullStream()
172 # define LOG_FROM_PLUGIN(level, category, pluginName, file, line) ::Orthanc::Logging::NullStream()
159 #else /* ORTHANC_ENABLE_LOGGING == 1 */ 173 #else /* ORTHANC_ENABLE_LOGGING == 1 */
160 # define LOG(level) ::Orthanc::Logging::InternalLogger \ 174
161 (::Orthanc::Logging::LogLevel_ ## level, \ 175 #if !defined(__ORTHANC_FILE__)
162 ::Orthanc::Logging::LogCategory_GENERIC, __FILE__, __LINE__) 176 # if defined(_MSC_VER)
163 # define VLOG(unused) ::Orthanc::Logging::InternalLogger \ 177 # pragma message("Warning: Macro __ORTHANC_FILE__ is not defined, this will leak the full path of the source files in the binaries")
164 (::Orthanc::Logging::LogLevel_TRACE, \ 178 # else
165 ::Orthanc::Logging::LogCategory_GENERIC, __FILE__, __LINE__) 179 # warning Warning: Macro __ORTHANC_FILE__ is not defined, this will leak the full path of the source files in the binaries
180 # endif
181 # define __ORTHANC_FILE__ __FILE__
182 #endif
183
184 # define LOG(level) ::Orthanc::Logging::InternalLogger \
185 (::Orthanc::Logging::LogLevel_ ## level, \
186 ::Orthanc::Logging::LogCategory_GENERIC, NULL /* no plugin */, \
187 __ORTHANC_FILE__, __LINE__)
188
189 # define VLOG(unused) ::Orthanc::Logging::InternalLogger \
190 (::Orthanc::Logging::LogLevel_TRACE, \
191 ::Orthanc::Logging::LogCategory_GENERIC, NULL /* no plugin */, \
192 __ORTHANC_FILE__, __LINE__)
193
166 # define CLOG(level, category) ::Orthanc::Logging::InternalLogger \ 194 # define CLOG(level, category) ::Orthanc::Logging::InternalLogger \
167 (::Orthanc::Logging::LogLevel_ ## level, \ 195 (::Orthanc::Logging::LogLevel_ ## level, \
168 ::Orthanc::Logging::LogCategory_ ## category, __FILE__, __LINE__) 196 ::Orthanc::Logging::LogCategory_ ## category, NULL /* no plugin */, \
197 __ORTHANC_FILE__, __LINE__)
198
199 # define LOG_FROM_PLUGIN(level, category, pluginName, file, line) \
200 ::Orthanc::Logging::InternalLogger(level, category, pluginName, file, line)
201
169 #endif 202 #endif
170 203
171 204
172 205
173 #if (ORTHANC_ENABLE_LOGGING == 1 && \ 206 #if (ORTHANC_ENABLE_LOGGING == 1 && \
190 std::stringstream messageStream_; 223 std::stringstream messageStream_;
191 224
192 public: 225 public:
193 InternalLogger(LogLevel level, 226 InternalLogger(LogLevel level,
194 LogCategory category, 227 LogCategory category,
228 const char* pluginName /* ignored */,
195 const char* file /* ignored */, 229 const char* file /* ignored */,
196 int line /* ignored */) : 230 int line /* ignored */) :
197 level_(level), 231 level_(level),
198 category_(category) 232 category_(category)
199 {
200 }
201
202 // For backward binary compatibility with Orthanc Framework <= 1.8.0
203 InternalLogger(LogLevel level,
204 const char* file /* ignored */,
205 int line /* ignored */) :
206 level_(level),
207 category_(LogCategory_GENERIC)
208 { 233 {
209 } 234 }
210 235
211 ~InternalLogger(); 236 ~InternalLogger();
212 237
240 private: 265 private:
241 boost::mutex::scoped_lock lock_; 266 boost::mutex::scoped_lock lock_;
242 LogLevel level_; 267 LogLevel level_;
243 std::unique_ptr<std::stringstream> pluginStream_; 268 std::unique_ptr<std::stringstream> pluginStream_;
244 std::ostream* stream_; 269 std::ostream* stream_;
245 270 LogCategory category_;
246 void Setup(LogCategory category, 271 const char* file_;
247 const char* file, 272 uint32_t line_;
248 int line);
249 273
250 public: 274 public:
251 InternalLogger(LogLevel level, 275 InternalLogger(LogLevel level,
252 LogCategory category, 276 LogCategory category,
253 const char* file, 277 const char* pluginName,
254 int line);
255
256 // For backward binary compatibility with Orthanc Framework <= 1.8.0
257 InternalLogger(LogLevel level,
258 const char* file, 278 const char* file,
259 int line); 279 int line);
260 280
261 ~InternalLogger(); 281 ~InternalLogger();
262 282