Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/CppHelpers/Logging/ILogger.h @ 5640:f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 30 May 2024 21:19:57 +0200 |
parents | 48b8dae6dc77 |
children |
rev | line source |
---|---|
4829
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
1 /** |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
5640
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
4829
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
8 * |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * This program is free software: you can redistribute it and/or |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * modify it under the terms of the GNU General Public License as |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * published by the Free Software Foundation, either version 3 of the |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
12 * License, or (at your option) any later version. |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
13 * |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
14 * This program is distributed in the hope that it will be useful, but |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
15 * WITHOUT ANY WARRANTY; without even the implied warranty of |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * General Public License for more details. |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * You should have received a copy of the GNU General Public License |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
21 **/ |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
22 |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
23 |
3468
4c89b6160563
fix the fix of the fix (this time for good !)
Alain Mazy <alain@mazy.be>
parents:
3467
diff
changeset
|
24 #pragma once |
3460 | 25 |
26 #include <string> | |
27 #include <vector> | |
28 #include <boost/algorithm/string.hpp> | |
29 #include <boost/thread.hpp> | |
30 | |
31 namespace OrthancHelpers | |
32 { | |
33 | |
3654
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
34 |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
35 inline std::string ShortenId(const std::string& orthancUuid) |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
36 { |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
37 size_t firstHyphenPos = orthancUuid.find_first_of('-'); |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
38 if (firstHyphenPos == std::string::npos) |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
39 { |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
40 return orthancUuid; |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
41 } |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
42 else |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
43 { |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
44 return orthancUuid.substr(0, firstHyphenPos); |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
45 } |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
46 } |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
47 |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
48 |
3460 | 49 // Interface for loggers providing the same interface |
50 // in Orthanc framework or in an Orthanc plugins. | |
51 // Furthermore, compared to the LOG and VLOG macros, | |
52 // these loggers will provide "contexts". | |
53 class ILogger | |
54 { | |
55 public: | |
56 virtual ~ILogger() {} | |
57 virtual void Trace(const char* message) = 0; | |
58 virtual void Trace(const std::string& message) = 0; | |
59 virtual void Info(const char* message) = 0; | |
60 virtual void Info(const std::string& message) = 0; | |
61 virtual void Warning(const char* message) = 0; | |
62 virtual void Warning(const std::string& message) = 0; | |
63 virtual void Error(const char* message) = 0; | |
64 virtual void Error(const std::string& message) = 0; | |
65 | |
3588 | 66 virtual void EnterContext(const char* message, bool forceLogContextChange = false) = 0; |
67 virtual void EnterContext(const std::string& message, bool forceLogContextChange = false) = 0; | |
68 virtual void LeaveContext(bool forceLogContextChange = false) = 0; | |
3460 | 69 }; |
70 | |
71 | |
72 // Implements ILogger by providing contexts. Contexts defines | |
73 // the "call-stack" of the logs and are prepended to the log. | |
74 // check LogContext class for more details | |
75 class BaseLogger : public ILogger | |
76 { | |
3473
37e908965e5a
improved detection of threads for ILogger
Alain Mazy <alain@mazy.be>
parents:
3468
diff
changeset
|
77 #if ORTHANC_ENABLE_THREADS == 1 |
3460 | 78 boost::thread_specific_ptr<std::vector<std::string>> contexts_; |
3466
b61e74e68d41
fix ILogger in non threaded environment (Emscripten)
Alain Mazy <alain@mazy.be>
parents:
3460
diff
changeset
|
79 #else |
3467 | 80 std::auto_ptr<std::vector<std::string>> contexts_; |
3466
b61e74e68d41
fix ILogger in non threaded environment (Emscripten)
Alain Mazy <alain@mazy.be>
parents:
3460
diff
changeset
|
81 #endif |
3460 | 82 bool logContextChanges_; |
83 | |
84 public: | |
85 | |
86 BaseLogger() | |
87 : logContextChanges_(false) | |
88 { | |
89 } | |
90 | |
91 void EnableLogContextChanges(bool enable) | |
92 { | |
93 logContextChanges_ = enable; | |
94 } | |
95 | |
3588 | 96 virtual void EnterContext(const char* message, bool forceLogContextChange = false) |
3460 | 97 { |
3588 | 98 EnterContext(std::string(message), forceLogContextChange); |
3460 | 99 } |
100 | |
3588 | 101 virtual void EnterContext(const std::string& message, bool forceLogContextChange = false) |
3460 | 102 { |
103 if (!contexts_.get()) | |
104 { | |
105 contexts_.reset(new std::vector<std::string>()); | |
106 } | |
107 contexts_->push_back(message); | |
108 | |
3588 | 109 if (logContextChanges_ || forceLogContextChange) |
3460 | 110 { |
111 Info(".. entering"); | |
112 } | |
113 } | |
114 | |
3588 | 115 virtual void LeaveContext(bool forceLogContextChange = false) |
3460 | 116 { |
3588 | 117 if (logContextChanges_ || forceLogContextChange) |
3460 | 118 { |
119 Info(".. leaving"); | |
120 } | |
121 | |
122 contexts_->pop_back(); | |
123 if (contexts_->size() == 0) | |
124 { | |
125 contexts_.reset(NULL); | |
126 } | |
127 } | |
128 | |
129 protected: | |
130 | |
131 virtual std::string GetContext() | |
132 { | |
133 if (contexts_.get() != NULL && contexts_->size() > 0) | |
134 { | |
135 return "|" + boost::algorithm::join(*contexts_, " | ") + "|"; | |
136 } | |
137 else | |
138 { | |
139 return std::string("|"); | |
140 } | |
141 } | |
142 }; | |
143 | |
144 | |
145 /* RAII to set a Log context. | |
146 * Example: | |
147 * ILogger* logger = new OrthancPluginLogger(..); | |
148 * { | |
149 * LogContext logContext(logger, "A"); | |
150 * { | |
151 * LogContext nestedLogContext(logger, "B"); | |
152 * logger->Error("out of memory"); | |
153 * } | |
154 * } | |
155 * will produce: | |
156 * |A | B| out of memory | |
157 * | |
158 * furthermore, if LogContextChanges are enabled in the BaseLogger, | |
159 * you'll get; | |
160 * |A| .. entering | |
161 * |A | B| .. entering | |
162 * |A | B| out of memory | |
163 * |A | B| .. leaving | |
164 * |A| .. leaving | |
165 */ | |
166 class LogContext | |
167 { | |
168 ILogger* logger_; | |
3588 | 169 bool forceLogContextChange_; |
3460 | 170 public: |
3588 | 171 LogContext(ILogger* logger, const char* context, bool forceLogContextChange = false) : |
172 logger_(logger), | |
173 forceLogContextChange_(forceLogContextChange) | |
3460 | 174 { |
3588 | 175 logger_->EnterContext(context, forceLogContextChange_); |
3460 | 176 } |
177 | |
3588 | 178 LogContext(ILogger* logger, const std::string& context, bool forceLogContextChange = false) : |
179 logger_(logger), | |
180 forceLogContextChange_(forceLogContextChange) | |
3460 | 181 { |
3588 | 182 logger_->EnterContext(context, forceLogContextChange_); |
3460 | 183 } |
184 | |
185 ~LogContext() | |
186 { | |
3588 | 187 logger_->LeaveContext(forceLogContextChange_); |
3460 | 188 } |
189 | |
190 }; | |
191 } |