Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/CppHelpers/Logging/ILogger.h @ 4829:c847b0dfd255
added missing copyright headers
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 25 Nov 2021 18:53:08 +0100 |
parents | d25f4c0fa160 |
children | 7053502fbf97 |
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 |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
6 * |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
7 * 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
|
8 * 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
|
9 * 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
|
10 * License, or (at your option) any later version. |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
12 * 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
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
14 * 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
|
15 * General Public License for more details. |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * 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
|
18 * 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
|
19 **/ |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 |
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
21 |
3468
4c89b6160563
fix the fix of the fix (this time for good !)
Alain Mazy <alain@mazy.be>
parents:
3467
diff
changeset
|
22 #pragma once |
3460 | 23 |
24 #include <string> | |
25 #include <vector> | |
26 #include <boost/algorithm/string.hpp> | |
27 #include <boost/thread.hpp> | |
28 | |
29 namespace OrthancHelpers | |
30 { | |
31 | |
3654
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
32 |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
33 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
|
34 { |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
35 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
|
36 if (firstHyphenPos == std::string::npos) |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
37 { |
427a13084241
OrthancHelpers::ShortenId to display shortened uuid in logs
Alain Mazy <alain@mazy.be>
parents:
3588
diff
changeset
|
38 return orthancUuid; |
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 else |
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 return orthancUuid.substr(0, firstHyphenPos); |
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 } |
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 |
3460 | 47 // Interface for loggers providing the same interface |
48 // in Orthanc framework or in an Orthanc plugins. | |
49 // Furthermore, compared to the LOG and VLOG macros, | |
50 // these loggers will provide "contexts". | |
51 class ILogger | |
52 { | |
53 public: | |
54 virtual ~ILogger() {} | |
55 virtual void Trace(const char* message) = 0; | |
56 virtual void Trace(const std::string& message) = 0; | |
57 virtual void Info(const char* message) = 0; | |
58 virtual void Info(const std::string& message) = 0; | |
59 virtual void Warning(const char* message) = 0; | |
60 virtual void Warning(const std::string& message) = 0; | |
61 virtual void Error(const char* message) = 0; | |
62 virtual void Error(const std::string& message) = 0; | |
63 | |
3588 | 64 virtual void EnterContext(const char* message, bool forceLogContextChange = false) = 0; |
65 virtual void EnterContext(const std::string& message, bool forceLogContextChange = false) = 0; | |
66 virtual void LeaveContext(bool forceLogContextChange = false) = 0; | |
3460 | 67 }; |
68 | |
69 | |
70 // Implements ILogger by providing contexts. Contexts defines | |
71 // the "call-stack" of the logs and are prepended to the log. | |
72 // check LogContext class for more details | |
73 class BaseLogger : public ILogger | |
74 { | |
3473
37e908965e5a
improved detection of threads for ILogger
Alain Mazy <alain@mazy.be>
parents:
3468
diff
changeset
|
75 #if ORTHANC_ENABLE_THREADS == 1 |
3460 | 76 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
|
77 #else |
3467 | 78 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
|
79 #endif |
3460 | 80 bool logContextChanges_; |
81 | |
82 public: | |
83 | |
84 BaseLogger() | |
85 : logContextChanges_(false) | |
86 { | |
87 } | |
88 | |
89 void EnableLogContextChanges(bool enable) | |
90 { | |
91 logContextChanges_ = enable; | |
92 } | |
93 | |
3588 | 94 virtual void EnterContext(const char* message, bool forceLogContextChange = false) |
3460 | 95 { |
3588 | 96 EnterContext(std::string(message), forceLogContextChange); |
3460 | 97 } |
98 | |
3588 | 99 virtual void EnterContext(const std::string& message, bool forceLogContextChange = false) |
3460 | 100 { |
101 if (!contexts_.get()) | |
102 { | |
103 contexts_.reset(new std::vector<std::string>()); | |
104 } | |
105 contexts_->push_back(message); | |
106 | |
3588 | 107 if (logContextChanges_ || forceLogContextChange) |
3460 | 108 { |
109 Info(".. entering"); | |
110 } | |
111 } | |
112 | |
3588 | 113 virtual void LeaveContext(bool forceLogContextChange = false) |
3460 | 114 { |
3588 | 115 if (logContextChanges_ || forceLogContextChange) |
3460 | 116 { |
117 Info(".. leaving"); | |
118 } | |
119 | |
120 contexts_->pop_back(); | |
121 if (contexts_->size() == 0) | |
122 { | |
123 contexts_.reset(NULL); | |
124 } | |
125 } | |
126 | |
127 protected: | |
128 | |
129 virtual std::string GetContext() | |
130 { | |
131 if (contexts_.get() != NULL && contexts_->size() > 0) | |
132 { | |
133 return "|" + boost::algorithm::join(*contexts_, " | ") + "|"; | |
134 } | |
135 else | |
136 { | |
137 return std::string("|"); | |
138 } | |
139 } | |
140 }; | |
141 | |
142 | |
143 /* RAII to set a Log context. | |
144 * Example: | |
145 * ILogger* logger = new OrthancPluginLogger(..); | |
146 * { | |
147 * LogContext logContext(logger, "A"); | |
148 * { | |
149 * LogContext nestedLogContext(logger, "B"); | |
150 * logger->Error("out of memory"); | |
151 * } | |
152 * } | |
153 * will produce: | |
154 * |A | B| out of memory | |
155 * | |
156 * furthermore, if LogContextChanges are enabled in the BaseLogger, | |
157 * you'll get; | |
158 * |A| .. entering | |
159 * |A | B| .. entering | |
160 * |A | B| out of memory | |
161 * |A | B| .. leaving | |
162 * |A| .. leaving | |
163 */ | |
164 class LogContext | |
165 { | |
166 ILogger* logger_; | |
3588 | 167 bool forceLogContextChange_; |
3460 | 168 public: |
3588 | 169 LogContext(ILogger* logger, const char* context, bool forceLogContextChange = false) : |
170 logger_(logger), | |
171 forceLogContextChange_(forceLogContextChange) | |
3460 | 172 { |
3588 | 173 logger_->EnterContext(context, forceLogContextChange_); |
3460 | 174 } |
175 | |
3588 | 176 LogContext(ILogger* logger, const std::string& context, bool forceLogContextChange = false) : |
177 logger_(logger), | |
178 forceLogContextChange_(forceLogContextChange) | |
3460 | 179 { |
3588 | 180 logger_->EnterContext(context, forceLogContextChange_); |
3460 | 181 } |
182 | |
183 ~LogContext() | |
184 { | |
3588 | 185 logger_->LeaveContext(forceLogContextChange_); |
3460 | 186 } |
187 | |
188 }; | |
189 } |