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