Apache Log4cxx Version 1.0.0
Loading...
Searching...
No Matches
log4cxx::NDC Class Reference

the ndc class implements nested diagnostic contexts as defined by neil harrison in the article "patterns for logging diagnostic messages" part of the book "<i>pattern languages of program design 3</i>" edited by martin et al. More...

#include <ndc.h>

Public Types

typedef std::pair< LogString, LogStringDiagnosticContext
 Pair of Message and FullMessage.
 
typedef std::stack< DiagnosticContextStack
 

Public Member Functions

 NDC (const std::string &message)
 Creates a nested diagnostic context.
 
 ~NDC ()
 Removes the topmost element from the NDC stack.
 
 NDC (const std::wstring &message)
 Creates a nested diagnostic context.
 
 NDC (const std::basic_string< UniChar > &message)
 Creates a nested diagnostic context.
 
 NDC (const CFStringRef &message)
 Creates a nested diagnostic context.
 

Static Public Member Functions

static void clear ()
 Clear any nested diagnostic information if any.
 
static StackcloneStack ()
 Clone the diagnostic context for the current thread.
 
static void inherit (Stack *stack)
 Inherit the diagnostic context of another thread.
 
static bool get (LogString &dest)
 Get the current value of the NDC of the currrent thread.
 
static int getDepth ()
 Get the current nesting depth of this diagnostic context.
 
static bool empty ()
 Tests if the NDC is empty.
 
static LogString pop ()
 Pop top value off stack.
 
static bool pop (std::string &buf)
 Pop top value off stack.
 
static LogString peek ()
 Looks at the last diagnostic context at the top of this NDC without removing it.
 
static bool peek (std::string &buf)
 Get top value without removing value.
 
static void push (const std::string &message)
 Push new diagnostic context information for the current thread.
 
static void pushLS (const LogString &message)
 Push new diagnostic context information for the current thread.
 
static void remove ()
 Remove the diagnostic context for this thread.
 
static void push (const std::wstring &message)
 Push new diagnostic context information for the current thread.
 
static bool peek (std::wstring &dst)
 Appends the current NDC content to the provided string.
 
static bool pop (std::wstring &dst)
 Appends the current NDC content to the provided string and removes the value from the NDC.
 
static void push (const std::basic_string< UniChar > &message)
 Push new diagnostic context information for the current thread.
 
static bool peek (std::basic_string< UniChar > &dst)
 Appends the current NDC content to the provided string.
 
static bool pop (std::basic_string< UniChar > &dst)
 Appends the current NDC content to the provided string and removes the value from the NDC.
 
static void push (const CFStringRef &message)
 Push new diagnostic context information for the current thread.
 
static bool peek (CFStringRef &dst)
 Gets the current NDC value.
 
static bool pop (CFStringRef &dst)
 Gets and removes the current NDC value.
 

Detailed Description

the ndc class implements nested diagnostic contexts as defined by neil harrison in the article "patterns for logging diagnostic messages" part of the book "<i>pattern languages of program design 3</i>" edited by martin et al.

a nested diagnostic context, or ndc in short, is an instrument to distinguish interleaved log output from different sources. log output is typically interleaved when a server handles multiple clients near-simultaneously.

interleaved log output can still be meaningful if each log entry from different contexts had a distinctive stamp. this is where ndcs come into play.

note that ndcs are managed on a per thread basis. ndc operations such as push, pop, clear and getDepth affect the ndc of the current thread only. ndcs of other threads remain unaffected.

for example, a servlet can build a per client request ndc consisting the clients host name and other information contained in the the request. cookies are another source of distinctive information. to build an ndc one uses the push operation. simply put,

  • contexts can be nested.

  • when entering a context, call ndc.push. as a side effect, if there is no nested diagnostic context for the current thread, this method will create it.

  • when leaving a context, call ndc.pop.

  • when exiting a thread make sure to call remove .

there is no penalty for forgetting to match each push operation with a corresponding pop, except the obvious mismatch between the real application context and the context set in the ndc.

if configured to do so, PatternLayout and TTCCLayout instances automatically retrieve the nested diagnostic context for the current thread without any user intervention. hence, even if a servlet is serving multiple clients simultaneously, the logs emanating from the same code (belonging to the same logger) can still be distinguished because each client request will have a different ndc tag.

heavy duty systems should call the remove method when leaving the run method of a thread. this ensures that the memory used by the thread can be freed by the java garbage collector. there is a mechanism to lazily remove references to dead threads. in practice, this means that you can be a little sloppy and sometimes forget to call remove before exiting a thread.

Member Typedef Documentation

◆ DiagnosticContext

Pair of Message and FullMessage.

◆ Stack

Constructor & Destructor Documentation

◆ NDC() [1/4]

log4cxx::NDC::NDC ( const std::string &  message)

Creates a nested diagnostic context.

Since java performs no automatic cleanup of objects when a scope is left, in log4j push() and pop() must be used to manage the NDC. For convenience, log4cxx provides an NDC constructor and destructor which simply call the push() and pop() methods, allowing for automatic cleanup when the current scope ends.

Parameters
messageThe new diagnostic context information.
See also
The push method.

◆ ~NDC()

log4cxx::NDC::~NDC ( )

Removes the topmost element from the NDC stack.

See also
The pop method.

◆ NDC() [2/4]

log4cxx::NDC::NDC ( const std::wstring &  message)

Creates a nested diagnostic context.

Since java performs no automatic cleanup of objects when a scope is left, in log4j push() and pop() must be used to manage the NDC. For convenience, log4cxx provides an NDC constructor and destructor which simply call the push() and pop() methods, allowing for automatic cleanup when the current scope ends.

Parameters
messageThe new diagnostic context information.
See also
The push method.

◆ NDC() [3/4]

log4cxx::NDC::NDC ( const std::basic_string< UniChar > &  message)

Creates a nested diagnostic context.

Since java performs no automatic cleanup of objects when a scope is left, in log4j push() and pop() must be used to manage the NDC. For convenience, log4cxx provides an NDC constructor and destructor which simply call the push() and pop() methods, allowing for automatic cleanup when the current scope ends.

Parameters
messageThe new diagnostic context information.
See also
The push method.

◆ NDC() [4/4]

log4cxx::NDC::NDC ( const CFStringRef message)

Creates a nested diagnostic context.

Since java performs no automatic cleanup of objects when a scope is left, in log4j push() and pop() must be used to manage the NDC. For convenience, log4cxx provides an NDC constructor and destructor which simply call the push() and pop() methods, allowing for automatic cleanup when the current scope ends.

Parameters
messageThe new diagnostic context information.
See also
The push method.

Member Function Documentation

◆ clear()

static void log4cxx::NDC::clear ( )
static

Clear any nested diagnostic information if any.

This method is useful in cases where the same thread can be potentially used over and over in different unrelated contexts.

◆ cloneStack()

static Stack * log4cxx::NDC::cloneStack ( )
static

Clone the diagnostic context for the current thread.

Internally a diagnostic context is represented as a stack. A given thread can supply the stack (i.e. diagnostic context) to a child thread so that the child can inherit the parent thread's diagnostic context.

The child thread uses the inherit method to inherit the parent's diagnostic context.

If not passed to inherit, returned stack should be deleted by caller.

Returns
Stack A clone of the current thread's diagnostic context, will not be null.

◆ empty()

static bool log4cxx::NDC::empty ( )
static

Tests if the NDC is empty.

◆ get()

static bool log4cxx::NDC::get ( LogString dest)
static

Get the current value of the NDC of the currrent thread.

Parameters
destdestination to which to append content of NDC.
Returns
true if NDC is set.

◆ getDepth()

static int log4cxx::NDC::getDepth ( )
static

Get the current nesting depth of this diagnostic context.

◆ inherit()

static void log4cxx::NDC::inherit ( Stack stack)
static

Inherit the diagnostic context of another thread.

The parent thread can obtain a reference to its diagnostic context using the cloneStack method. It should communicate this information to its child so that it may inherit the parent's diagnostic context.

The parent's diagnostic context is cloned before being inherited. In other words, once inherited, the two diagnostic contexts can be managed independently.

Parameters
stackThe diagnostic context of the parent thread, will be deleted during call. If NULL, NDC will not be modified.

◆ peek() [1/5]

static LogString log4cxx::NDC::peek ( )
static

Looks at the last diagnostic context at the top of this NDC without removing it.

The returned value is the value that was pushed last. If no context is available, then the empty string "" is returned.

Returns
String The innermost diagnostic context.

◆ peek() [2/5]

static bool log4cxx::NDC::peek ( CFStringRef dst)
static

Gets the current NDC value.

Parameters
dstdestination.
Returns
true if NDC value set.

◆ peek() [3/5]

static bool log4cxx::NDC::peek ( std::basic_string< UniChar > &  dst)
static

Appends the current NDC content to the provided string.

Parameters
dstdestination.
Returns
true if NDC value set.

◆ peek() [4/5]

static bool log4cxx::NDC::peek ( std::string &  buf)
static

Get top value without removing value.

Parameters
bufto which top value is appended.
Returns
true if NDC contained at least one value.

◆ peek() [5/5]

static bool log4cxx::NDC::peek ( std::wstring &  dst)
static

Appends the current NDC content to the provided string.

Parameters
dstdestination.
Returns
true if NDC value set.

◆ pop() [1/5]

static LogString log4cxx::NDC::pop ( )
static

Pop top value off stack.

Returns
top value.

◆ pop() [2/5]

static bool log4cxx::NDC::pop ( CFStringRef dst)
static

Gets and removes the current NDC value.

Parameters
dstdestination.
Returns
true if NDC value set.

◆ pop() [3/5]

static bool log4cxx::NDC::pop ( std::basic_string< UniChar > &  dst)
static

Appends the current NDC content to the provided string and removes the value from the NDC.

Parameters
dstdestination.
Returns
true if NDC value set.

◆ pop() [4/5]

static bool log4cxx::NDC::pop ( std::string &  buf)
static

Pop top value off stack.

Parameters
bufto which top value is appended.
Returns
true if NDC contained at least one value.

◆ pop() [5/5]

static bool log4cxx::NDC::pop ( std::wstring &  dst)
static

Appends the current NDC content to the provided string and removes the value from the NDC.

Parameters
dstdestination.
Returns
true if NDC value set.

◆ push() [1/4]

static void log4cxx::NDC::push ( const CFStringRef message)
static

Push new diagnostic context information for the current thread.

The contents of the message parameter is determined solely by the client.

Parameters
messageThe new diagnostic context information.

◆ push() [2/4]

static void log4cxx::NDC::push ( const std::basic_string< UniChar > &  message)
static

Push new diagnostic context information for the current thread.

The contents of the message parameter is determined solely by the client.

Parameters
messageThe new diagnostic context information.

◆ push() [3/4]

static void log4cxx::NDC::push ( const std::string &  message)
static

Push new diagnostic context information for the current thread.

The contents of the message parameter is determined solely by the client.

Parameters
messageThe new diagnostic context information.

◆ push() [4/4]

static void log4cxx::NDC::push ( const std::wstring &  message)
static

Push new diagnostic context information for the current thread.

The contents of the message parameter is determined solely by the client.

Parameters
messageThe new diagnostic context information.

◆ pushLS()

static void log4cxx::NDC::pushLS ( const LogString message)
static

Push new diagnostic context information for the current thread.

The contents of the message parameter is determined solely by the client.

Parameters
messageThe new diagnostic context information.

◆ remove()

static void log4cxx::NDC::remove ( )
static

Remove the diagnostic context for this thread.

Each thread that created a diagnostic context by calling push should call this method before exiting. Otherwise, the memory used by the thread cannot be reclaimed by the VM.

As this is such an important problem in heavy duty systems and because it is difficult to always guarantee that the remove method is called before exiting a thread, this method has been augmented to lazily remove references to dead threads. In practice, this means that you can be a little sloppy and occasionally forget to call remove before exiting a thread. However, you must call remove sometime. If you never call it, then your application is sure to run out of memory.


The documentation for this class was generated from the following file: