Mercurial > hg > orthanc
annotate Resources/Samples/CppHelpers/Logging/ILogger.h @ 4114:cfe805d75e4b Orthanc-1.5.8
closing Orthanc-1.5.8
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 13:08:45 +0200 |
parents | 37e908965e5a |
children | eb48adfd931e |
rev | line source |
---|---|
3468
4c89b6160563
fix the fix of the fix (this time for good !)
Alain Mazy <alain@mazy.be>
parents:
3467
diff
changeset
|
1 #pragma once |
3460 | 2 |
3 #include <string> | |
4 #include <vector> | |
5 #include <boost/algorithm/string.hpp> | |
6 #include <boost/thread.hpp> | |
7 | |
8 namespace OrthancHelpers | |
9 { | |
10 | |
11 // Interface for loggers providing the same interface | |
12 // in Orthanc framework or in an Orthanc plugins. | |
13 // Furthermore, compared to the LOG and VLOG macros, | |
14 // these loggers will provide "contexts". | |
15 class ILogger | |
16 { | |
17 public: | |
18 virtual ~ILogger() {} | |
19 virtual void Trace(const char* message) = 0; | |
20 virtual void Trace(const std::string& message) = 0; | |
21 virtual void Info(const char* message) = 0; | |
22 virtual void Info(const std::string& message) = 0; | |
23 virtual void Warning(const char* message) = 0; | |
24 virtual void Warning(const std::string& message) = 0; | |
25 virtual void Error(const char* message) = 0; | |
26 virtual void Error(const std::string& message) = 0; | |
27 | |
28 virtual void EnterContext(const char* message) = 0; | |
29 virtual void EnterContext(const std::string& message) = 0; | |
30 virtual void LeaveContext() = 0; | |
31 }; | |
32 | |
33 | |
34 // Implements ILogger by providing contexts. Contexts defines | |
35 // the "call-stack" of the logs and are prepended to the log. | |
36 // check LogContext class for more details | |
37 class BaseLogger : public ILogger | |
38 { | |
3473
37e908965e5a
improved detection of threads for ILogger
Alain Mazy <alain@mazy.be>
parents:
3468
diff
changeset
|
39 #if ORTHANC_ENABLE_THREADS == 1 |
3460 | 40 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
|
41 #else |
3467 | 42 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
|
43 #endif |
3460 | 44 bool logContextChanges_; |
45 | |
46 public: | |
47 | |
48 BaseLogger() | |
49 : logContextChanges_(false) | |
50 { | |
51 } | |
52 | |
53 void EnableLogContextChanges(bool enable) | |
54 { | |
55 logContextChanges_ = enable; | |
56 } | |
57 | |
58 virtual void EnterContext(const char* message) | |
59 { | |
60 EnterContext(std::string(message)); | |
61 } | |
62 | |
63 virtual void EnterContext(const std::string& message) | |
64 { | |
65 if (!contexts_.get()) | |
66 { | |
67 contexts_.reset(new std::vector<std::string>()); | |
68 } | |
69 contexts_->push_back(message); | |
70 | |
71 if (logContextChanges_) | |
72 { | |
73 Info(".. entering"); | |
74 } | |
75 } | |
76 | |
77 virtual void LeaveContext() | |
78 { | |
79 if (logContextChanges_) | |
80 { | |
81 Info(".. leaving"); | |
82 } | |
83 | |
84 contexts_->pop_back(); | |
85 if (contexts_->size() == 0) | |
86 { | |
87 contexts_.reset(NULL); | |
88 } | |
89 } | |
90 | |
91 protected: | |
92 | |
93 virtual std::string GetContext() | |
94 { | |
95 if (contexts_.get() != NULL && contexts_->size() > 0) | |
96 { | |
97 return "|" + boost::algorithm::join(*contexts_, " | ") + "|"; | |
98 } | |
99 else | |
100 { | |
101 return std::string("|"); | |
102 } | |
103 } | |
104 }; | |
105 | |
106 | |
107 /* RAII to set a Log context. | |
108 * Example: | |
109 * ILogger* logger = new OrthancPluginLogger(..); | |
110 * { | |
111 * LogContext logContext(logger, "A"); | |
112 * { | |
113 * LogContext nestedLogContext(logger, "B"); | |
114 * logger->Error("out of memory"); | |
115 * } | |
116 * } | |
117 * will produce: | |
118 * |A | B| out of memory | |
119 * | |
120 * furthermore, if LogContextChanges are enabled in the BaseLogger, | |
121 * you'll get; | |
122 * |A| .. entering | |
123 * |A | B| .. entering | |
124 * |A | B| out of memory | |
125 * |A | B| .. leaving | |
126 * |A| .. leaving | |
127 */ | |
128 class LogContext | |
129 { | |
130 ILogger* logger_; | |
131 public: | |
132 LogContext(ILogger* logger, const char* context) : | |
133 logger_(logger) | |
134 { | |
135 logger_->EnterContext(context); | |
136 } | |
137 | |
138 LogContext(ILogger* logger, const std::string& context) : | |
139 logger_(logger) | |
140 { | |
141 logger_->EnterContext(context); | |
142 } | |
143 | |
144 ~LogContext() | |
145 { | |
146 logger_->LeaveContext(); | |
147 } | |
148 | |
149 }; | |
150 } |