1 | // |
---|
2 | // NestedDiagnosticContext.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/NestedDiagnosticContext.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: NestedDiagnosticContext |
---|
9 | // |
---|
10 | // Definition of the NestedDiagnosticContext class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_NestedDiagnosticContext_INCLUDED |
---|
40 | #define Foundation_NestedDiagnosticContext_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include <vector> |
---|
45 | #include <ostream> |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | |
---|
50 | |
---|
51 | class NDCScope; |
---|
52 | |
---|
53 | |
---|
54 | class Foundation_API NestedDiagnosticContext |
---|
55 | /// This class implements a Nested Diagnostic Context (NDC), |
---|
56 | /// as described in Neil Harrison's article "Patterns for Logging |
---|
57 | /// Diagnostic Messages" in "Pattern Languages of Program Design 3" |
---|
58 | /// (Addison-Wesley). |
---|
59 | /// |
---|
60 | /// A NDC maintains a stack of context information, consisting of |
---|
61 | /// an informational string (e.g., a method name), as well as an |
---|
62 | /// optional source code line number and file name. |
---|
63 | /// NDCs are especially useful for tagging log messages with |
---|
64 | /// context information which is very helpful in a multithreaded |
---|
65 | /// server scenario. |
---|
66 | /// Every thread has its own private NDC, which is automatically |
---|
67 | /// created when needed and destroyed when the thread terminates. |
---|
68 | /// |
---|
69 | /// The NDCScope (or NDC::Scope) class can be used to automatically |
---|
70 | /// push information at the beginning of a scope, and to pop it |
---|
71 | /// at the end. |
---|
72 | /// The poco_ndc(info) macro augments the information with a |
---|
73 | /// source code line number and file name. |
---|
74 | { |
---|
75 | public: |
---|
76 | typedef NDCScope Scope; |
---|
77 | |
---|
78 | NestedDiagnosticContext(); |
---|
79 | /// Creates the NestedDiagnosticContext. |
---|
80 | |
---|
81 | NestedDiagnosticContext(const NestedDiagnosticContext& ctx); |
---|
82 | /// Copy constructor. |
---|
83 | |
---|
84 | ~NestedDiagnosticContext(); |
---|
85 | /// Destroys the NestedDiagnosticContext. |
---|
86 | |
---|
87 | NestedDiagnosticContext& operator = (const NestedDiagnosticContext& ctx); |
---|
88 | /// Assignment operator. |
---|
89 | |
---|
90 | void push(const std::string& info); |
---|
91 | /// Pushes a context (without line number and filename) onto the stack. |
---|
92 | |
---|
93 | void push(const std::string& info, int line, const char* filename); |
---|
94 | /// Pushes a context (including line number and filename) |
---|
95 | /// onto the stack. Filename must be a static string, such as the |
---|
96 | /// one produced by the __FILE__ preprocessor macro. |
---|
97 | |
---|
98 | void pop(); |
---|
99 | /// Pops the top-most context off the stack. |
---|
100 | |
---|
101 | int depth() const; |
---|
102 | /// Returns the depth (number of contexts) of the stack. |
---|
103 | |
---|
104 | std::string toString() const; |
---|
105 | /// Returns the stack as a string with entries |
---|
106 | /// delimited by colons. The string does not contain |
---|
107 | /// line numbers and filenames. |
---|
108 | |
---|
109 | void dump(std::ostream& ostr) const; |
---|
110 | /// Dumps the stack (including line number and filenames) |
---|
111 | /// to the given stream. The entries are delimited by |
---|
112 | /// a newline. |
---|
113 | |
---|
114 | void dump(std::ostream& ostr, const std::string& delimiter) const; |
---|
115 | /// Dumps the stack (including line number and filenames) |
---|
116 | /// to the given stream. |
---|
117 | |
---|
118 | void clear(); |
---|
119 | /// Clears the NDC stack. |
---|
120 | |
---|
121 | static NestedDiagnosticContext& current(); |
---|
122 | /// Returns the current thread's NDC. |
---|
123 | |
---|
124 | private: |
---|
125 | struct Context |
---|
126 | { |
---|
127 | std::string info; |
---|
128 | const char* file; |
---|
129 | int line; |
---|
130 | }; |
---|
131 | typedef std::vector<Context> Stack; |
---|
132 | |
---|
133 | Stack _stack; |
---|
134 | }; |
---|
135 | |
---|
136 | |
---|
137 | typedef NestedDiagnosticContext NDC; |
---|
138 | |
---|
139 | |
---|
140 | class Foundation_API NDCScope |
---|
141 | /// This class can be used to automatically push a context onto |
---|
142 | /// the NDC stack at the beginning of a scope, and to pop |
---|
143 | /// the context at the end of the scope. |
---|
144 | { |
---|
145 | public: |
---|
146 | NDCScope(const std::string& info); |
---|
147 | /// Pushes a context on the stack. |
---|
148 | |
---|
149 | NDCScope(const std::string& info, int line, const char* filename); |
---|
150 | /// Pushes a context on the stack. |
---|
151 | |
---|
152 | ~NDCScope(); |
---|
153 | /// Pops the top-most context off the stack. |
---|
154 | }; |
---|
155 | |
---|
156 | |
---|
157 | // |
---|
158 | // inlines |
---|
159 | // |
---|
160 | inline NDCScope::NDCScope(const std::string& info) |
---|
161 | { |
---|
162 | NestedDiagnosticContext::current().push(info); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | inline NDCScope::NDCScope(const std::string& info, int line, const char* filename) |
---|
167 | { |
---|
168 | NestedDiagnosticContext::current().push(info, line, filename); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | inline NDCScope::~NDCScope() |
---|
173 | { |
---|
174 | NestedDiagnosticContext::current().pop(); |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | // |
---|
179 | // helper macros |
---|
180 | // |
---|
181 | #define poco_ndc(func) \ |
---|
182 | Poco::NDCScope _theNdcScope(#func, __LINE__, __FILE__) |
---|
183 | |
---|
184 | |
---|
185 | #if defined(_DEBUG) |
---|
186 | #define poco_ndc_dbg(func) \ |
---|
187 | Poco::NDCScope _theNdcScope(#func, __LINE__, __FILE__) |
---|
188 | #else |
---|
189 | #define poco_ndc_dbg(func) |
---|
190 | #endif |
---|
191 | |
---|
192 | |
---|
193 | } // namespace Poco |
---|
194 | |
---|
195 | |
---|
196 | #endif // Foundation_NestedDiagnosticContext_INCLUDED |
---|