log4cplus  2.1.0
ndc.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // Module: Log4CPLUS
3 // File: ndc.h
4 // Created: 6/2001
5 // Author: Tad E. Smith
6 //
7 //
8 // Copyright 2001-2017 Tad E. Smith
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 
26 #ifndef _LO4CPLUS_NDC_HEADER_
27 #define _LO4CPLUS_NDC_HEADER_
28 
29 #include <log4cplus/config.hxx>
30 
31 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
32 #pragma once
33 #endif
34 
35 #include <log4cplus/tstring.h>
36 
37 #include <map>
38 #include <deque>
39 
40 
41 namespace log4cplus {
42  // Forward declarations
43  struct DiagnosticContext;
44  typedef std::deque<DiagnosticContext> DiagnosticContextStack;
45 
106  {
107  public:
116  void clear();
117 
132 
147  void inherit(const DiagnosticContextStack& stack);
148 
152  log4cplus::tstring const & get() const;
153 
159  std::size_t getDepth() const;
160 
178 
182  void pop_void ();
183 
193  log4cplus::tstring const & peek() const;
194 
206  void push(const log4cplus::tstring& message);
207  void push(tchar const * message);
208 
221  void remove();
222 
253  void setMaxDepth(std::size_t maxDepth);
254 
255  // Public ctor but only to be used by internal::DefaultContext.
256  NDC();
257 
258  // Dtor
259  virtual ~NDC();
260 
261  private:
262  // Methods
263  LOG4CPLUS_PRIVATE static DiagnosticContextStack* getPtr();
264 
265  template <typename StringType>
267  void push_worker (StringType const &);
268 
269  // Disallow construction (and copying) except by getNDC()
270  NDC(const NDC&);
271  NDC& operator=(const NDC&);
272  };
273 
274 
279 
280 
285  {
286  // Ctors
288  DiagnosticContext const * parent);
289  DiagnosticContext(tchar const * message,
290  DiagnosticContext const * parent);
292  DiagnosticContext(tchar const * message);
294  DiagnosticContext & operator = (DiagnosticContext const &);
297 
299 
300  // Data
303  };
304 
305 
311  public:
313  explicit NDCContextCreator(const log4cplus::tstring& msg);
314  explicit NDCContextCreator(tchar const * msg);
315 
316  NDCContextCreator() = delete;
319  NDCContextCreator & operator= (NDCContextCreator const &) = delete;
320  NDCContextCreator & operator= (NDCContextCreator &&) = delete;
321 
324  };
325 
326 } // end namespace log4cplus
327 
328 
329 #endif // _LO4CPLUS_NDC_HEADER_
This class ensures that a NDC::push() call is always matched with a NDC::pop() call even in the face ...
Definition: ndc.h:310
~NDCContextCreator()
Pops the NDC stack.
NDCContextCreator(NDCContextCreator const &)=delete
NDCContextCreator(const log4cplus::tstring &msg)
Pushes msg onto the NDC stack.
NDCContextCreator(tchar const *msg)
NDCContextCreator(NDCContextCreator &&)=delete
The NDC class implements nested diagnostic contexts as defined by Neil Harrison in the article "Patte...
Definition: ndc.h:106
std::size_t getDepth() const
Get the current nesting depth of this diagnostic context.
log4cplus::tstring pop()
Clients should call this method before leaving a diagnostic context.
void remove()
Remove the diagnostic context for this thread.
virtual ~NDC()
void push(tchar const *message)
void push(const log4cplus::tstring &message)
Push new diagnostic context information for the current thread.
log4cplus::tstring const & get() const
Used when printing the diagnostic context.
void inherit(const DiagnosticContextStack &stack)
Inherit the diagnostic context of another thread.
log4cplus::tstring const & peek() const
Looks at the last diagnostic context at the top of this NDC without removing it.
DiagnosticContextStack cloneStack() const
Clone the diagnostic context for the current thread.
void clear()
Clear any nested diagnostic information if any.
void pop_void()
Same as pop() but without the return value.
void setMaxDepth(std::size_t maxDepth)
Set maximum depth of this diagnostic context.
#define LOG4CPLUS_PRIVATE
Definition: config.hxx:53
LOG4CPLUS_EXPORT NDC & getNDC()
Return a reference to the singleton object.
std::basic_string< tchar > tstring
Definition: tstring.h:39
char tchar
Definition: tchar.h:56
std::deque< DiagnosticContext > DiagnosticContextStack
Definition: ndc.h:43
This is the internal object that is stored on the NDC stack.
Definition: ndc.h:285
DiagnosticContext(DiagnosticContext &&)
log4cplus::tstring message
Definition: ndc.h:301
log4cplus::tstring fullMessage
Definition: ndc.h:302
DiagnosticContext(const log4cplus::tstring &message, DiagnosticContext const *parent)
DiagnosticContext(tchar const *message)
void swap(DiagnosticContext &)
DiagnosticContext(tchar const *message, DiagnosticContext const *parent)
DiagnosticContext(const log4cplus::tstring &message)
DiagnosticContext(DiagnosticContext const &)
#define LOG4CPLUS_EXPORT
Definition: win32.h:141