Mercurial > hg > orthanc
comparison Resources/Samples/CppHelpers/Logging/ILogger.h @ 3460:fbe22748cd9c
added logging OrthancHelpers
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Tue, 09 Jul 2019 10:30:30 +0200 |
parents | |
children | b61e74e68d41 |
comparison
equal
deleted
inserted
replaced
3457:9ea218c90057 | 3460:fbe22748cd9c |
---|---|
1 #pragma once | |
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 { | |
39 boost::thread_specific_ptr<std::vector<std::string>> contexts_; | |
40 bool logContextChanges_; | |
41 | |
42 public: | |
43 | |
44 BaseLogger() | |
45 : logContextChanges_(false) | |
46 { | |
47 } | |
48 | |
49 void EnableLogContextChanges(bool enable) | |
50 { | |
51 logContextChanges_ = enable; | |
52 } | |
53 | |
54 virtual void EnterContext(const char* message) | |
55 { | |
56 EnterContext(std::string(message)); | |
57 } | |
58 | |
59 virtual void EnterContext(const std::string& message) | |
60 { | |
61 if (!contexts_.get()) | |
62 { | |
63 contexts_.reset(new std::vector<std::string>()); | |
64 } | |
65 contexts_->push_back(message); | |
66 | |
67 if (logContextChanges_) | |
68 { | |
69 Info(".. entering"); | |
70 } | |
71 } | |
72 | |
73 virtual void LeaveContext() | |
74 { | |
75 if (logContextChanges_) | |
76 { | |
77 Info(".. leaving"); | |
78 } | |
79 | |
80 contexts_->pop_back(); | |
81 if (contexts_->size() == 0) | |
82 { | |
83 contexts_.reset(NULL); | |
84 } | |
85 } | |
86 | |
87 protected: | |
88 | |
89 virtual std::string GetContext() | |
90 { | |
91 if (contexts_.get() != NULL && contexts_->size() > 0) | |
92 { | |
93 return "|" + boost::algorithm::join(*contexts_, " | ") + "|"; | |
94 } | |
95 else | |
96 { | |
97 return std::string("|"); | |
98 } | |
99 } | |
100 }; | |
101 | |
102 | |
103 /* RAII to set a Log context. | |
104 * Example: | |
105 * ILogger* logger = new OrthancPluginLogger(..); | |
106 * { | |
107 * LogContext logContext(logger, "A"); | |
108 * { | |
109 * LogContext nestedLogContext(logger, "B"); | |
110 * logger->Error("out of memory"); | |
111 * } | |
112 * } | |
113 * will produce: | |
114 * |A | B| out of memory | |
115 * | |
116 * furthermore, if LogContextChanges are enabled in the BaseLogger, | |
117 * you'll get; | |
118 * |A| .. entering | |
119 * |A | B| .. entering | |
120 * |A | B| out of memory | |
121 * |A | B| .. leaving | |
122 * |A| .. leaving | |
123 */ | |
124 class LogContext | |
125 { | |
126 ILogger* logger_; | |
127 public: | |
128 LogContext(ILogger* logger, const char* context) : | |
129 logger_(logger) | |
130 { | |
131 logger_->EnterContext(context); | |
132 } | |
133 | |
134 LogContext(ILogger* logger, const std::string& context) : | |
135 logger_(logger) | |
136 { | |
137 logger_->EnterContext(context); | |
138 } | |
139 | |
140 ~LogContext() | |
141 { | |
142 logger_->LeaveContext(); | |
143 } | |
144 | |
145 }; | |
146 } |