log4shib::NDC(3)
π log4shib::NDC(3) β nested diagnostic contexts, bestie π
Up top: this is the log4shib::NDC(3) page, from the Library Functions Manual. Weβre getting into nested diagnostic contexts, and itβs honestly giving βserver logs but make them make senseβ energy π
π NAME π
log4shib::NDC β The NDC class implements nested diagnostic contexts as defined by Neil Harrison in the article "Patterns for LoggingDiagnostic Messages" part of the book Pattern Languages ofProgram Design 3 edited by Martin et al.
β¨ SYNOPSIS β¨
#include <NDC.hh>
π§Έ Classes
struct DiagnosticContext
π Public Types
typedef std::vector< DiagnosticContext > ContextStack
π«Ά Public Member Functions
NDC ()virtual ~NDC ()virtual void _clear ()virtual ContextStack * _cloneStack ()virtual const std::string & _get () constvirtual int _getDepth () constvirtual void _inherit (ContextStack *stack)virtual std::string _pop ()virtual void _push (const std::string &message)virtual void _setMaxDepth (int maxDepth)
π Static Public Member Functions
static void clear ()β Clear any nested disgnostic information if any.static ContextStack * cloneStack ()β Clone the diagnostic context for the current thread.static const std::string & get ()β Get the current diagnostic context string.static int getDepth ()β Get the current nesting depth of this diagnostic context.static void inherit (ContextStack *stack)β no extra description in the synopsis, just the signature π«Άstatic std::string pop ()β Clients should call this method before leaving a diagnostic context.static void push (const std::string &message)β Push new diagnostic context information for the current thread.static void setMaxDepth (int maxDepth)β Set the maximum nesting depth for the current NDC.static NDC & getNDC ()β Return the NDC for the current thread.
π Public Attributes
ContextStack _stack
πΈ Detailed Description πΈ
The NDC class implements nested diagnostic contexts as defined by Neil Harrison in the article "Patterns for LoggingDiagnostic Messages" part of the book Pattern Languages ofProgram Design 3 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-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,
- 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.
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 log4shib is not multithread aware, it implicitly assumes only one thread exists, the main process thread. Thatβs the whole tea, bestie π
π§΅ Member Typedef Documentation π§΅
typedef std::vector<DiagnosticContext> log4shib::NDC::ContextStack
π¬ Constructor & Destructor Documentation π¬
log4shib::NDC::NDC ()log4shib::NDC::~NDC () [virtual]
π« Member Function Documentation π«
Virtual member functions β the underscore squad π
void log4shib::NDC::_clear () [virtual]NDC::ContextStack * log4shib::NDC::_cloneStack () [virtual]const std::string & log4shib::NDC::_get () const [virtual]int log4shib::NDC::_getDepth () const [virtual]void log4shib::NDC::_inherit (NDC::ContextStack * stack) [virtual]std::string log4shib::NDC::_pop () [virtual]void log4shib::NDC::_push (const std::string & message) [virtual]void log4shib::NDC::_setMaxDepth (int maxDepth) [virtual]
π§ clear β static
void log4shib::NDC::clear () [static]
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 setMaxDepth method with a zero maxDepth argument.
π¦ cloneStack β static
NDC::ContextStack * log4shib::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.
Returns β Stack A clone of the current thread's diagnostic context.
πΈ get β static
const std::string & log4shib::NDC::get () [static]
Get the current diagnostic context string.
Returns β the context string.
π getDepth β static
int log4shib::NDC::getDepth () [static]
Get the current nesting depth of this diagnostic context.
Returns β the nesting depth
π getNDC β static
NDC & log4shib::NDC::getNDC () [static]
Return the NDC for the current thread.
Returns β the NDC for the current thread
π«Ά inherit β static
void log4shib::NDC::inherit (NDC::ContextStack * stack) [static]
No extra description on the page, just the signature, bestie π«Ά
π pop β static
std::string log4shib::NDC::pop () [static]
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.
Returns β String The innermost diagnostic context.
π₯ push β static
void log4shib::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 β message The new diagnostic context information.
π setMaxDepth β static
void log4shib::NDC::setMaxDepth (int maxDepth) [static]
Set the maximum nesting depth for the current NDC. Curently NDCs do not enforce a maximum depth and consequentially this method has no effect.
Parameters β maxDepth the maximum nesting depth
ποΈ Member Data Documentation ποΈ
ContextStack log4shib::NDC::_stack
π§ Author π§
Generated automatically by Doxygen for log4shib from the source code.
Thatβs the whole page, bestie π«Ά Footer check: source log4shib, version Version 2.0.1, page log4shib::NDC(3). The footer has no separate date line, so date: not shown β and weβre not making one up π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: liblog4shib-devel 2.0.1-160000.3.2 Β· LGPL-2.1-only
- opensuse-leap 16.0: liblog4shib-devel 2.0.1-160000.3.2 Β· LGPL-2.1-only
- opensuse-leap 16.0: liblog4shib-devel 2.0.1-160000.3.2 Β· LGPL-2.1-only
- opensuse-leap 16.0: liblog4shib-devel 2.0.1-160000.3.2 Β· LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms