#include <NDC.hh>
Public Types | |
typedef std::vector< DiagnosticContext > | ContextStack |
Public Methods | |
NDC () | |
virtual | ~NDC () |
virtual void | _clear () |
virtual ContextStack * | _cloneStack () |
virtual const std::string & | _get () const |
virtual int | _getDepth () const |
virtual void | _inherit (ContextStack *stack) |
virtual std::string | _pop () |
virtual void | _push (const std::string &message) |
virtual void | _setMaxDepth (int maxDepth) |
Static Public Methods | |
void | clear () |
Clear any nested disgnostic information if any. More... | |
ContextStack * | cloneStack () |
Clone the diagnostic context for the current thread. More... | |
const std::string & | get () |
Get the current diagnostic context string. More... | |
int | getDepth () |
Get the current nesting depth of this diagnostic context. More... | |
void | inherit (ContextStack *stack) |
std::string | pop () |
Clients should call this method before leaving a diagnostic context. More... | |
void | push (const std::string &message) |
Push new diagnostic context information for the current thread. More... | |
void | setMaxDepth (int maxDepth) |
Set the maximum nesting depth for the current NDC. More... | |
NDC & | getNDC () |
Return the NDC for the current thread. More... | |
Public Attributes | |
ContextStack | _stack |
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-simulatanously.
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
, getDepth
and setMaxDepth
affect the NDC of the current thread only. NDCs of other threads remain unaffected.
To build an NDC one uses the push
operation. Simply put,
NDC.push
. As a side effect, if there is no nested diagnostic context for the current thread, this method will create it.
NDC.pop
.
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.
Custom Layouts may include the nested diagnostic context for the current thread in log messages, without any user intervention. Hence, even if a server is serving multiple clients simultaneously, the logs emanating from the same code (belonging to the same category) can still be distinguished because each client request will have a different NDC tag.
Unfortunately, unlike Java, C++ does not have platform independent multithreading support. Therefore, currently log4cpp is not multithread aware, it implicitly assumes only one thread exists, the main process thread.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Clear any nested disgnostic information if any. This method is useful in cases where the same thread can be potentially used over and over in different unrelated contexts.
This method is equivalent to calling the |
|
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
|
|
Get the current diagnostic context string.
|
|
Get the current nesting depth of this diagnostic context.
|
|
Return the NDC for the current thread.
|
|
|
|
Clients should call this method before leaving a diagnostic context.
The returned value is the value that was pushed last. If no context is available, then the empty string "" is returned.
|
|
Push new diagnostic context information for the current thread.
The contents of the
|
|
Set the maximum nesting depth for the current NDC. Curently NDCs do not enforce a maximum depth and consequentially this method has no effect.
|
|
|