/src/dcmtk/oflog/libsrc/ndc.cc
Line | Count | Source |
1 | | // Module: Log4CPLUS |
2 | | // File: ndc.cxx |
3 | | // Created: 6/2001 |
4 | | // Author: Tad E. Smith |
5 | | // |
6 | | // |
7 | | // Copyright 2001-2010 Tad E. Smith |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | // you may not use this file except in compliance with the License. |
11 | | // You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software |
16 | | // distributed under the License is distributed on an "AS IS" BASIS, |
17 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | // See the License for the specific language governing permissions and |
19 | | // limitations under the License. |
20 | | |
21 | | #include "dcmtk/oflog/ndc.h" |
22 | | #include "dcmtk/oflog/internal/internal.h" |
23 | | #include <utility> |
24 | | #include <algorithm> |
25 | | |
26 | | |
27 | | namespace dcmtk |
28 | | { |
29 | | namespace log4cplus |
30 | | { |
31 | | |
32 | | |
33 | | |
34 | | /////////////////////////////////////////////////////////////////////////////// |
35 | | // log4cplus::DiagnosticContext ctors |
36 | | /////////////////////////////////////////////////////////////////////////////// |
37 | | |
38 | | |
39 | | namespace |
40 | | { |
41 | | |
42 | | |
43 | | static |
44 | | void |
45 | | init_full_message (log4cplus::tstring & fullMessage, |
46 | | log4cplus::tstring const & message, DiagnosticContext const * parent) |
47 | 0 | { |
48 | 0 | if (parent) |
49 | 0 | { |
50 | 0 | fullMessage.reserve (parent->fullMessage.size () + 1 |
51 | 0 | + message.size ()); |
52 | 0 | fullMessage = parent->fullMessage; |
53 | 0 | fullMessage += DCMTK_LOG4CPLUS_TEXT(" "); |
54 | 0 | fullMessage += message; |
55 | 0 | } |
56 | 0 | else |
57 | 0 | fullMessage = message; |
58 | 0 | } |
59 | | |
60 | | |
61 | | } // namespace |
62 | | |
63 | | |
64 | | DiagnosticContext::DiagnosticContext() |
65 | 0 | : message() |
66 | 0 | , fullMessage() |
67 | 0 | { |
68 | 0 | } |
69 | | |
70 | | DiagnosticContext::DiagnosticContext(const log4cplus::tstring& message_, |
71 | | DiagnosticContext const * parent) |
72 | 0 | : message(message_) |
73 | 0 | , fullMessage() |
74 | 0 | { |
75 | 0 | init_full_message (fullMessage, message, parent); |
76 | 0 | } |
77 | | |
78 | | |
79 | | DiagnosticContext::DiagnosticContext(tchar const * message_, |
80 | | DiagnosticContext const * parent) |
81 | 0 | : message(message_) |
82 | 0 | , fullMessage() |
83 | 0 | { |
84 | 0 | init_full_message (fullMessage, message, parent); |
85 | 0 | } |
86 | | |
87 | | |
88 | | DiagnosticContext::DiagnosticContext(const log4cplus::tstring& message_) |
89 | 0 | : message(message_) |
90 | 0 | , fullMessage(message) |
91 | 0 | { |
92 | 0 | } |
93 | | |
94 | | |
95 | | DiagnosticContext::DiagnosticContext(tchar const * message_) |
96 | 0 | : message(message_) |
97 | 0 | , fullMessage(message) |
98 | 0 | { |
99 | 0 | } |
100 | | |
101 | | |
102 | | DiagnosticContext::DiagnosticContext (DiagnosticContext const & other) |
103 | 0 | : message (other.message) |
104 | 0 | , fullMessage (other.fullMessage) |
105 | 0 | { } |
106 | | |
107 | | |
108 | | DiagnosticContext & DiagnosticContext::operator = ( |
109 | | DiagnosticContext const & other) |
110 | 0 | { |
111 | 0 | DiagnosticContext (other).swap (*this); |
112 | 0 | return *this; |
113 | 0 | } |
114 | | |
115 | | |
116 | | #if defined (DCMTK_LOG4CPLUS_HAVE_RVALUE_REFS) |
117 | | DiagnosticContext::DiagnosticContext (DiagnosticContext && other) |
118 | 0 | : message (STD_NAMESPACE move (other.message)) |
119 | 0 | , fullMessage (STD_NAMESPACE move (other.fullMessage)) |
120 | 0 | { } |
121 | | |
122 | | |
123 | | DiagnosticContext & |
124 | | DiagnosticContext::operator = (DiagnosticContext && other) |
125 | 0 | { |
126 | 0 | DiagnosticContext (STD_NAMESPACE move (other)).swap (*this); |
127 | 0 | return *this; |
128 | 0 | } |
129 | | |
130 | | #endif |
131 | | |
132 | | |
133 | | void |
134 | | DiagnosticContext::swap (DiagnosticContext & other) |
135 | 0 | { |
136 | 0 | using STD_NAMESPACE swap; |
137 | 0 | swap (message, other.message); |
138 | 0 | swap (fullMessage, other.fullMessage); |
139 | 0 | } |
140 | | |
141 | | /////////////////////////////////////////////////////////////////////////////// |
142 | | // log4cplus::NDC ctor and dtor |
143 | | /////////////////////////////////////////////////////////////////////////////// |
144 | | |
145 | | NDC::NDC() |
146 | 3 | { } |
147 | | |
148 | | |
149 | | NDC::~NDC() |
150 | 0 | { } |
151 | | |
152 | | |
153 | | |
154 | | /////////////////////////////////////////////////////////////////////////////// |
155 | | // log4cplus::NDC public methods |
156 | | /////////////////////////////////////////////////////////////////////////////// |
157 | | |
158 | | void |
159 | | NDC::clear() |
160 | 0 | { |
161 | 0 | DiagnosticContextStack* ptr = getPtr(); |
162 | 0 | DiagnosticContextStack ().swap (*ptr); |
163 | 0 | } |
164 | | |
165 | | |
166 | | DiagnosticContextStack |
167 | | NDC::cloneStack() const |
168 | 0 | { |
169 | 0 | DiagnosticContextStack* ptr = getPtr(); |
170 | 0 | return DiagnosticContextStack(*ptr); |
171 | 0 | } |
172 | | |
173 | | |
174 | | void |
175 | | NDC::inherit(const DiagnosticContextStack& stack) |
176 | 0 | { |
177 | 0 | DiagnosticContextStack* ptr = getPtr(); |
178 | 0 | DiagnosticContextStack (stack).swap (*ptr); |
179 | 0 | } |
180 | | |
181 | | |
182 | | log4cplus::tstring const & |
183 | | NDC::get() const |
184 | 0 | { |
185 | 0 | DiagnosticContextStack* ptr = getPtr(); |
186 | 0 | if(!ptr->empty()) |
187 | 0 | return ptr->back().fullMessage; |
188 | 0 | else |
189 | 0 | return internal::empty_str; |
190 | 0 | } |
191 | | |
192 | | |
193 | | size_t |
194 | | NDC::getDepth() const |
195 | 0 | { |
196 | 0 | DiagnosticContextStack* ptr = getPtr(); |
197 | 0 | return ptr->size(); |
198 | 0 | } |
199 | | |
200 | | |
201 | | log4cplus::tstring |
202 | | NDC::pop() |
203 | 0 | { |
204 | 0 | DiagnosticContextStack* ptr = getPtr(); |
205 | 0 | if(!ptr->empty()) |
206 | 0 | { |
207 | 0 | tstring message; |
208 | 0 | message.swap (ptr->back ().message); |
209 | 0 | ptr->pop_back(); |
210 | 0 | return message; |
211 | 0 | } |
212 | 0 | else |
213 | 0 | return log4cplus::tstring (); |
214 | 0 | } |
215 | | |
216 | | |
217 | | void |
218 | | NDC::pop_void () |
219 | 0 | { |
220 | 0 | DiagnosticContextStack* ptr = getPtr (); |
221 | 0 | if (! ptr->empty ()) |
222 | 0 | ptr->pop_back (); |
223 | 0 | } |
224 | | |
225 | | |
226 | | log4cplus::tstring const & |
227 | | NDC::peek() const |
228 | 0 | { |
229 | 0 | DiagnosticContextStack* ptr = getPtr(); |
230 | 0 | if(!ptr->empty()) |
231 | 0 | return ptr->back().message; |
232 | 0 | else |
233 | 0 | return internal::empty_str; |
234 | 0 | } |
235 | | |
236 | | |
237 | | void |
238 | | NDC::push(const log4cplus::tstring& message) |
239 | 0 | { |
240 | 0 | DiagnosticContextStack* ptr = getPtr(); |
241 | 0 | if (ptr->empty()) |
242 | 0 | ptr->push_back( DiagnosticContext(message, NULL) ); |
243 | 0 | else |
244 | 0 | { |
245 | 0 | DiagnosticContext const & dc = ptr->back(); |
246 | 0 | ptr->push_back( DiagnosticContext(message, &dc) ); |
247 | 0 | } |
248 | 0 | } |
249 | | |
250 | | |
251 | | void |
252 | | NDC::push(tchar const * message) |
253 | 0 | { |
254 | 0 | DiagnosticContextStack* ptr = getPtr(); |
255 | 0 | if (ptr->empty()) |
256 | 0 | ptr->push_back( DiagnosticContext(message, NULL) ); |
257 | 0 | else |
258 | 0 | { |
259 | 0 | DiagnosticContext const & dc = ptr->back(); |
260 | 0 | ptr->push_back( DiagnosticContext(message, &dc) ); |
261 | 0 | } |
262 | 0 | } |
263 | | |
264 | | |
265 | | void |
266 | | NDC::remove() |
267 | 0 | { |
268 | 0 | DiagnosticContextStack* ptr = getPtr(); |
269 | 0 | DiagnosticContextStack ().swap (*ptr); |
270 | 0 | } |
271 | | |
272 | | |
273 | | void |
274 | | NDC::setMaxDepth(size_t maxDepth) |
275 | 0 | { |
276 | 0 | DiagnosticContextStack* ptr = getPtr(); |
277 | 0 | while(maxDepth < ptr->size()) |
278 | 0 | ptr->pop_back(); |
279 | 0 | } |
280 | | |
281 | | |
282 | | DiagnosticContextStack* NDC::getPtr() |
283 | 0 | { |
284 | 0 | internal::per_thread_data * ptd = internal::get_ptd (); |
285 | 0 | return &ptd->ndc_dcs; |
286 | 0 | } |
287 | | |
288 | | |
289 | | // |
290 | | // |
291 | | // |
292 | | |
293 | | NDCContextCreator::NDCContextCreator(const log4cplus::tstring& msg) |
294 | 0 | { |
295 | 0 | getNDC().push(msg); |
296 | 0 | } |
297 | | |
298 | | |
299 | | NDCContextCreator::NDCContextCreator(tchar const * msg) |
300 | 0 | { |
301 | 0 | getNDC().push(msg); |
302 | 0 | } |
303 | | |
304 | | |
305 | | NDCContextCreator::~NDCContextCreator() |
306 | 0 | { |
307 | 0 | getNDC().pop_void(); |
308 | 0 | } |
309 | | |
310 | | |
311 | | } // namespace log4cplus |
312 | | } // end namespace dcmtk |