DCMTK: dcmtk::log4cplus::NDC Class Reference

DCMTK Version 3.6.9 OFFIS DICOM Toolkit

Loading...
Searching...
No Matches
Public Member Functions | List of all members
dcmtk::log4cplus::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...

## Public Member Functions
void | clear ()
| Clear any nested diagnostic information if any.
DiagnosticContextStack | cloneStack () const
| Clone the diagnostic context for the current thread.
void | inherit (const DiagnosticContextStack &stack)
| Inherit the diagnostic context of another thread.
log4cplus::tstring const & | get () const
| Used when printing the diagnostic context.
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 | pop_void ()
| Same as pop() but without the return value.
log4cplus::tstring const & | peek () const
| Looks at the last diagnostic context at the top of this NDC without removing it.
void | push (const log4cplus::tstring &message)
| Push new diagnostic context information for the current thread.
void | remove ()
| Remove the diagnostic context for this thread.
void | setMaxDepth (size_t maxDepth)
| Set maximum depth of this diagnostic context.

## 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`, `getDepth` and `setMaxDepth` affect the NDC of the current thread only. NDCs of other threads remain unaffected.

For example, a server 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 `getNDC().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 `getNDC().pop()`.

- When exiting a thread make sure to call `NDC.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. Use of the `NDCContextCreator` class can automate this process and make your code exception-safe.

If configured to do so, `log4cplus::PatternLayout` and `log4cplus::TTCCLayout` instances automatically retrieve the nested diagnostic context for the current thread 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 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.

A thread may inherit the nested diagnostic context of another (possibly parent) thread using the `inherit` method. A thread may obtain a copy of its NDC with the `cloneStack` method and pass the reference to any other thread, in particular to a child.

## Member Function Documentation

## ◆ clear()

void dcmtk::log4cplus::NDC::clear | ( |  | ) |

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.

This method is equivalent to calling the `setMaxDepth` method with a zero `maxDepth` argument.

## ◆ cloneStack()

DiagnosticContextStack dcmtk::log4cplus::NDC::cloneStack | ( |  | ) | const

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.

## ◆ getDepth()

size_t dcmtk::log4cplus::NDC::getDepth | ( |  | ) | const

Get the current nesting depth of this diagnostic context.

See also
  setMaxDepth

## ◆ inherit()

void dcmtk::log4cplus::NDC::inherit | ( | const DiagnosticContextStack & | stack | ) |

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

  stack | The diagnostic context of the parent thread.

## ◆ peek()

log4cplus::tstring const & dcmtk::log4cplus::NDC::peek | ( |  | ) | const

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.

## ◆ pop()

log4cplus::tstring dcmtk::log4cplus::NDC::pop | ( |  | ) |

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. If each call to push() is paired with a call to pop() (even in presence of thrown exceptions), the last pop() call frees the memory used by NDC for this thread. Otherwise, remove() must be called at the end of the thread to free the memory used by NDC for the thread.

Returns
  String The innermost diagnostic context.

See also
  NDCContextCreator, remove(), push()

## ◆ push()

void dcmtk::log4cplus::NDC::push | ( | const log4cplus::tstring & | message | ) |

Push new diagnostic context information for the current thread.

The contents of the `message` parameter is determined solely by the client. Each call to push() should be paired with a call to pop().

Parameters

  message | The new diagnostic context information.

See also
  NDCContextCreator, pop(), remove()

References push().

Referenced by push().

## ◆ remove()

void dcmtk::log4cplus::NDC::remove | ( |  | ) |

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. It is possible to omit this call if and only if each push() call is always paired with a pop() call (even in presence of thrown exceptions). Then the memory used by NDC will be returned by the last pop() call and a call to remove() will be no-op.

## ◆ setMaxDepth()

void dcmtk::log4cplus::NDC::setMaxDepth | ( | size_t | maxDepth | ) |

Set maximum depth of this diagnostic context.

If the current depth is smaller or equal to `maxDepth`, then no action is taken.

This method is a convenient alternative to multiple `pop` calls. Moreover, it is often the case that at the end of complex call sequences, the depth of the NDC is unpredictable. The `setMaxDepth` method circumvents this problem.

For example, the combination

       void foo() {
          size_t depth = NDC.getDepth();

          ... complex sequence of calls

          NDC.setMaxDepth(depth);
       }

ensures that between the entry and exit of foo the depth of the diagnostic stack is conserved.

Note: Use of the `NDCContextCreator` class will solve this particular problem.

See also
  getDepth

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

- oflog/include/dcmtk/oflog/ndc.h

Generated on Wed Apr 30 2025 for DCMTK Version 3.6.9 by Doxygen 1.13.2