DCMTK Version 3.6.9 OFFIS DICOM Toolkit

Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
OFConsole Class Reference

Singleton class which provides thread-safe access to the standard console output and error streams. More...

## Public Member Functions
virtual | ~OFConsole ()
| destructor.
STD_NAMESPACE ostream & | lockCout ()
| acquires a lock on the cout stream and returns a reference to the stream.
void | unlockCout ()
| releases the lock on the cout stream.
STD_NAMESPACE ostream & | getCout ()
| returns a reference to the current cout stream.
STD_NAMESPACE ostream * | setCout (STD_NAMESPACE ostream *newCout=NULL)
| exchanges the cout stream object.
STD_NAMESPACE ostream & | lockCerr ()
| acquires a lock on the cerr stream and returns a reference to the stream.
STD_NAMESPACE ostream & | getCerr ()
| returns a reference to the current cerr stream.
void | unlockCerr ()
| releases the lock on the cerr stream.
STD_NAMESPACE ostream * | setCerr (STD_NAMESPACE ostream *newCerr=NULL)
| exchanges the cerr stream object.
void | join ()
| combines the cerr and cout streams.
void | split ()
| splits combined cerr and cout streams.
OFBool | isJoined ()
| Checks whether cout and cerr are currently combined.

## Static Public Member Functions
static OFConsole & | instance ()
| returns the singleton instance of this class.
static void | mergeStderrStdout ()
| redirect stderr to stdout on file descriptor level
static void | unmergeStderrStdout ()
| remove a redirection of stderr to stdout on file descriptor level

## Private Member Functions
| OFConsole ()
| default constructor.
| OFConsole (const OFConsole &arg)
| private undefined copy constructor
OFConsole & | operator= (const OFConsole &arg)
| private undefined assignment operator

## Private Attributes
STD_NAMESPACE ostream * | currentCout
| pointer to current cout stream, never NULL
STD_NAMESPACE ostream * | currentCerr
| pointer to current cerr stream, never NULL
int | joined
| true if streams are combined, false otherwise
OFMutex | coutMutex
| mutex protecting access to cout
OFMutex | cerrMutex
| mutex protecting access to cerr

## Static Private Attributes
static int | old_stderr
| file descriptor for stderr, if redirected on FILE I/O level

## Detailed Description

Singleton class which provides thread-safe access to the standard console output and error streams.

Allows multiple threads to concurrently create output even if that output is redirected to file or memory. Protection is implemented if the module is compiled with -DWITH_THREADS and is based on Mutexes. Use of the singleton prior to start of main (i.e. from global constructors) is allowed, but any use after the end of main is undefined.

## Constructor & Destructor Documentation

## ◆ OFConsole()

OFConsole::OFConsole | ( |  | ) |
| private

default constructor.

After construction, the cout methods refer to the standard output stream and the cerr methods refer to the standard error stream. If compiled with -DDCMTK_GUI, string streams named COUT and CERR are used instead.

Referenced by OFConsole(), instance(), and operator=().

## Member Function Documentation

## ◆ getCerr()

STD_NAMESPACE ostream & OFConsole::getCerr | ( |  | ) |
| inline

returns a reference to the current cerr stream.

This method neither locks nor unlocks the stream - the called must ensure that the stream is locked and unlocked appropriately.

Returns
  reference to cerr stream

References currentCerr, currentCout, and joined.

## ◆ getCout()

STD_NAMESPACE ostream & OFConsole::getCout | ( |  | ) |
| inline

returns a reference to the current cout stream.

This method neither locks nor unlocks the stream - the called must ensure that the stream is locked and unlocked appropriately.

Returns
  reference to cout stream

References currentCout.

## ◆ instance()

static OFConsole & OFConsole::instance | ( |  | ) |
| static

returns the singleton instance of this class.

May be called before main() but not after end of main

References OFConsole().

## ◆ isJoined()

OFBool OFConsole::isJoined | ( |  | ) |

Checks whether cout and cerr are currently combined.

This method acquires its own locks. Neither cout nor cerr may be locked by the calling thread, otherwise a deadlock may occur.

Returns
  OFTrue if streams are combined, OFFalse otherwise.

## ◆ join()

void OFConsole::join | ( |  | ) |

combines the cerr and cout streams.

After a call to this method, both cout and cerr related methods lock, unlock and return the cout stream. This method acquires its own locks. Neither cout nor cerr may be locked by the calling thread, otherwise a deadlock may occur.

## ◆ lockCerr()

STD_NAMESPACE ostream & OFConsole::lockCerr | ( |  | ) |
| inline

acquires a lock on the cerr stream and returns a reference to the stream.

Returns
  reference to cerr stream

References cerrMutex, coutMutex, currentCerr, currentCout, and joined.

## ◆ lockCout()

STD_NAMESPACE ostream & OFConsole::lockCout | ( |  | ) |
| inline

acquires a lock on the cout stream and returns a reference to the stream.

Returns
  reference to cout stream

References coutMutex, and currentCout.

## ◆ setCerr()

STD_NAMESPACE ostream * OFConsole::setCerr | ( | STD_NAMESPACE ostream * | newCerr = NULL | ) |

exchanges the cerr stream object.

This method acquires its own lock. Cerr must not be locked by the calling thread, otherwise a deadlock may occur. The caller must ensure that the same stream object is not set both as cout and cerr because this might result in a conflict if one thread locks and uses cout, and another one locks and uses cerr. Use the join() method instead, see below.

Parameters

  newCerr | new cerr stream, default: restore the stream that was active upon creation of the console object.

Returns
  pointer to replaced cerr stream.

## ◆ setCout()

STD_NAMESPACE ostream * OFConsole::setCout | ( | STD_NAMESPACE ostream * | newCout = NULL | ) |

exchanges the cout stream object.

This method acquires its own lock. Cout must not be locked by the calling thread, otherwise a deadlock may occur. The caller must ensure that the same stream object is not set both as cout and cerr because this might result in a conflict if one thread locks and uses cout, and another one locks and uses cerr. Use the join() method instead, see below.

Parameters

  newCout | new cout stream, default: restore the stream that was active upon creation of the console object.

Returns
  pointer to replaced cout stream.

## ◆ split()

void OFConsole::split | ( |  | ) |

splits combined cerr and cout streams.

After a call to this method, cout and cerr related methods again lock, unlock and return different cout and cerr objects. This method acquires its own locks. Neither cout nor cerr may be locked by the calling thread, otherwise a deadlock may occur.

## Member Data Documentation

## ◆ coutMutex

OFMutex OFConsole::coutMutex
| private

mutex protecting access to cout

Remarks
  This member is only available if DCMTK is compiled with thread support enabled.

Referenced by lockCerr(), lockCout(), unlockCerr(), and unlockCout().

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

- ofstd/include/dcmtk/ofstd/ofconsol.h

Generated on Sat Mar 1 2025 for DCMTK Version 3.6.9 by Doxygen 1.13.2