annotate OrthancServer/Resources/Samples/CppHelpers/Logging/ILogger.h @ 5485:48b8dae6dc77

upgrade to year 2024
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 04 Jan 2024 15:10:42 +0100
parents 0ea402b4d901
children f7adfb22e20e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
6 * 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
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
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
24
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
25 #include <string>
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
26 #include <vector>
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
27 #include <boost/algorithm/string.hpp>
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
28 #include <boost/thread.hpp>
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
29
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
30 namespace OrthancHelpers
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
31 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
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
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
48 // Interface for loggers providing the same interface
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
49 // in Orthanc framework or in an Orthanc plugins.
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
50 // Furthermore, compared to the LOG and VLOG macros,
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
51 // these loggers will provide "contexts".
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
52 class ILogger
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
53 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
54 public:
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
55 virtual ~ILogger() {}
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
56 virtual void Trace(const char* message) = 0;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
57 virtual void Trace(const std::string& message) = 0;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
58 virtual void Info(const char* message) = 0;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
59 virtual void Info(const std::string& message) = 0;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
60 virtual void Warning(const char* message) = 0;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
61 virtual void Warning(const std::string& message) = 0;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
62 virtual void Error(const char* message) = 0;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
63 virtual void Error(const std::string& message) = 0;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
64
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
65 virtual void EnterContext(const char* message, bool forceLogContextChange = false) = 0;
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
66 virtual void EnterContext(const std::string& message, bool forceLogContextChange = false) = 0;
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
67 virtual void LeaveContext(bool forceLogContextChange = false) = 0;
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
68 };
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
69
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
70
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
71 // Implements ILogger by providing contexts. Contexts defines
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
72 // the "call-stack" of the logs and are prepended to the log.
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
73 // check LogContext class for more details
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
74 class BaseLogger : public ILogger
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
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
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
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
324f6b966c7a fix the fix
Alain Mazy <alain@mazy.be>
parents: 3466
diff changeset
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
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
81 bool logContextChanges_;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
82
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
83 public:
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
84
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
85 BaseLogger()
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
86 : logContextChanges_(false)
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
87 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
88 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
89
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
90 void EnableLogContextChanges(bool enable)
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
91 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
92 logContextChanges_ = enable;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
93 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
94
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
95 virtual void EnterContext(const char* message, bool forceLogContextChange = false)
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
96 {
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
97 EnterContext(std::string(message), forceLogContextChange);
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
98 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
99
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
100 virtual void EnterContext(const std::string& message, bool forceLogContextChange = false)
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
101 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
102 if (!contexts_.get())
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
103 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
104 contexts_.reset(new std::vector<std::string>());
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
105 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
106 contexts_->push_back(message);
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
107
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
108 if (logContextChanges_ || forceLogContextChange)
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
109 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
110 Info(".. entering");
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
111 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
112 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
113
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
114 virtual void LeaveContext(bool forceLogContextChange = false)
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
115 {
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
116 if (logContextChanges_ || forceLogContextChange)
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
117 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
118 Info(".. leaving");
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
119 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
120
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
121 contexts_->pop_back();
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
122 if (contexts_->size() == 0)
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
123 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
124 contexts_.reset(NULL);
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
125 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
126 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
127
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
128 protected:
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
129
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
130 virtual std::string GetContext()
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
131 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
132 if (contexts_.get() != NULL && contexts_->size() > 0)
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
133 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
134 return "|" + boost::algorithm::join(*contexts_, " | ") + "|";
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
135 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
136 else
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
137 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
138 return std::string("|");
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
139 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
140 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
141 };
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
142
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
143
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
144 /* RAII to set a Log context.
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
145 * Example:
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
146 * ILogger* logger = new OrthancPluginLogger(..);
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
147 * {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
148 * LogContext logContext(logger, "A");
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
149 * {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
150 * LogContext nestedLogContext(logger, "B");
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
151 * logger->Error("out of memory");
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
152 * }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
153 * }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
154 * will produce:
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
155 * |A | B| out of memory
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
156 *
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
157 * furthermore, if LogContextChanges are enabled in the BaseLogger,
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
158 * you'll get;
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
159 * |A| .. entering
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
160 * |A | B| .. entering
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
161 * |A | B| out of memory
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
162 * |A | B| .. leaving
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
163 * |A| .. leaving
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
164 */
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
165 class LogContext
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
166 {
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
167 ILogger* logger_;
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
168 bool forceLogContextChange_;
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
169 public:
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
170 LogContext(ILogger* logger, const char* context, bool forceLogContextChange = false) :
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
171 logger_(logger),
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
172 forceLogContextChange_(forceLogContextChange)
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
173 {
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
174 logger_->EnterContext(context, forceLogContextChange_);
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
175 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
176
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
177 LogContext(ILogger* logger, const std::string& context, bool forceLogContextChange = false) :
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
178 logger_(logger),
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
179 forceLogContextChange_(forceLogContextChange)
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
180 {
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
181 logger_->EnterContext(context, forceLogContextChange_);
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
182 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
183
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
184 ~LogContext()
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
185 {
3588
eb48adfd931e forceLogContextChanges
Alain Mazy <alain@mazy.be>
parents: 3473
diff changeset
186 logger_->LeaveContext(forceLogContextChange_);
3460
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
187 }
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
188
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
189 };
fbe22748cd9c added logging OrthancHelpers
Alain Mazy <alain@mazy.be>
parents:
diff changeset
190 }