ofconsol.h

00001 /*
00002  *
00003  *  Copyright (C) 1999-2005, OFFIS
00004  *
00005  *  This software and supporting documentation were developed by
00006  *
00007  *    Kuratorium OFFIS e.V.
00008  *    Healthcare Information and Communication Systems
00009  *    Escherweg 2
00010  *    D-26121 Oldenburg, Germany
00011  *
00012  *  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  AND OFFIS MAKES NO  WARRANTY
00013  *  REGARDING  THE  SOFTWARE,  ITS  PERFORMANCE,  ITS  MERCHANTABILITY  OR
00014  *  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES  OR
00015  *  ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
00016  *  PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
00017  *
00018  *  Module:  ofstd
00019  *
00020  *  Author:  Marco Eichelberg
00021  *
00022  *  Purpose: Define general purpose facility for console output
00023  *
00024  *  class OFConsole and its global instance, ofConsole,
00025  *  provide access to the standard console output and error streams
00026  *  in a way that allows multiple threads to concurrently create output
00027  *  even if that output is redirected, e. g. to file or memory.
00028  *  Protection is implemented if the module is compiled with -D_REENTRANT
00029  *  and is based on Mutexes.
00030  *
00031  *  In cases where DCMTK is used for GUI development, the fact that the
00032  *  libraries send many error messages to the standard or error streams
00033  *  are annoying since these streams are not present in a GUI environment.
00034  *  Either the messages just go lost or they even cause the GUI
00035  *  application to fail.  This file introduces aliases for the standard
00036  *  stream handles called COUT and CERR, which are normally only
00037  *  preprocessor macros for cout and cerr, respectively. If the
00038  *  toolkit is compiled with the flag DCMTK_GUI defined, however, these
00039  *  streams are created as OFOStringStream. This will allow a GUI based
00040  *  application to extract the messages and either present them to the
00041  *  user or store them in a log file.
00042  *
00043  *  GUI based applications making use of this feature should periodically
00044  *  check and clear these streams in order to avoid increasing consumption
00045  *  of heap memory.
00046  *
00047  *  Caveat 1: The DCMTK command line tools do not yet support the DCMTK_GUI
00048  *  flag, and will most likely exhibit all kinds of undesired behaviour
00049  *  if this flag is used.
00050  *
00051  *  Caveat 2: The direct use of the COUT and CERR macros is unsafe
00052  *  in multithread applications. Use ofConsole instead.
00053  *
00054  *  Last Update:      $Author: meichel $
00055  *  Update Date:      $Date: 2005/12/08 16:05:52 $
00056  *  CVS/RCS Revision: $Revision: 1.18 $
00057  *  Status:           $State: Exp $
00058  *
00059  *  CVS/RCS Log at end of file
00060  *
00061  */
00062 
00063 
00064 #ifndef OFCONSOL_H
00065 #define OFCONSOL_H
00066 
00067 #include "dcmtk/config/osconfig.h"
00068 #include "dcmtk/ofstd/ofstream.h"
00069 #include "dcmtk/ofstd/ofthread.h"
00070 
00071 #define INCLUDE_CSTDLIB
00072 #include "dcmtk/ofstd/ofstdinc.h"
00073 
00074 
00083 class OFConsole
00084 {
00085 public:
00086 
00089   virtual ~OFConsole(){ }
00090 
00095   ostream& lockCout()
00096   {
00097 #ifdef _REENTRANT
00098     coutMutex.lock();
00099 #endif
00100     return *currentCout;
00101   }
00102 
00105   void unlockCout()
00106   {
00107 #ifdef _REENTRANT
00108     coutMutex.unlock();
00109 #endif
00110   }
00111 
00117   ostream& getCout()
00118   {
00119     return *currentCout;
00120   }
00121 
00133   ostream *setCout(ostream *newCout=NULL);
00134 
00139   ostream& lockCerr()
00140   {
00141 #ifdef _REENTRANT
00142     cerrMutex.lock();
00143 #endif
00144     if (joined)
00145     {
00146 #ifdef _REENTRANT
00147       coutMutex.lock();
00148 #endif
00149       return *currentCout;
00150     }
00151     else return *currentCerr;
00152   }
00153 
00159   ostream& getCerr()
00160   {
00161     if (joined) return *currentCout;
00162     else return *currentCerr;
00163   }
00164 
00167   void unlockCerr()
00168   {
00169 #ifdef _REENTRANT
00170     if (joined) coutMutex.unlock();
00171     cerrMutex.unlock();
00172 #endif
00173   }
00174 
00186   ostream *setCerr(ostream *newCerr=NULL);
00187 
00194   void join();
00195 
00202   void split();
00203 
00209   OFBool isJoined();
00210 
00214   static OFConsole& instance();
00215 
00216 private:
00217 
00223   OFConsole();
00224 
00226   OFConsole(const OFConsole &arg);
00227 
00229   OFConsole& operator=(const OFConsole &arg);
00230 
00232   ostream *currentCout;
00233 
00235   ostream *currentCerr;
00236 
00238   int joined;
00239 
00240 #ifdef _REENTRANT
00241 
00242   OFMutex coutMutex;
00243 
00245   OFMutex cerrMutex;
00246 #endif
00247 
00248   // dummy declaration to keep gcc quiet
00249   friend class OFConsoleDummyFriend;
00250 };
00251 
00252 
00256 #define ofConsole (OFConsole::instance())
00257 
00258 /*
00259  * definitions for COUT, CERR, CLOG.
00260  *
00261  * NOTE: DIRECT USE OF THESE MACROS IS UNSAFE IN MULTITHREAD APPLICATIONS.
00262  */
00263 
00264 #ifdef DCMTK_GUI
00265 
00266 extern OFOStringStream COUT;
00267 extern OFOStringStream CERR;
00268 
00269 #else /* DCMTK_GUI */
00270 
00271 #define COUT (ofConsole.getCout())
00272 #define CERR (ofConsole.getCerr())
00273 
00274 #endif /* DCMTK_GUI */
00275 
00276 #endif /* OFCONSOL_H */
00277 
00278 
00279 /*
00280  *
00281  * CVS/RCS Log:
00282  * $Log: ofconsol.h,v $
00283  * Revision 1.18  2005/12/08 16:05:52  meichel
00284  * Changed include path schema for all DCMTK header files
00285  *
00286  * Revision 1.17  2004/01/21 11:50:10  meichel
00287  * Fixed DOS CR/LF in preprocessor directives
00288  *
00289  * Revision 1.16  2004/01/16 10:30:12  joergr
00290  * Removed acknowledgements with e-mail addresses from CVS log.
00291  *
00292  * Revision 1.15  2003/12/17 17:38:39  meichel
00293  * Changed definition of COUT and CERR macros to allow redirection to file.
00294  *
00295  * Revision 1.14  2003/12/05 10:37:41  joergr
00296  * Removed leading underscore characters from preprocessor symbols (reserved
00297  * symbols). Updated copyright date where appropriate.
00298  *
00299  * Revision 1.13  2002/11/27 11:23:05  meichel
00300  * Adapted module ofstd to use of new header file ofstdinc.h
00301  *
00302  * Revision 1.12  2002/05/16 15:56:33  meichel
00303  * Changed ofConsole into singleton implementation that can safely
00304  *   be used prior to start of main, i.e. from global constructors
00305  *
00306  * Revision 1.11  2002/05/16 08:16:44  meichel
00307  * changed return type of OFConsole::setCout() and OFConsole::setCerr()
00308  *   to pointer instead of reference.
00309  *
00310  * Revision 1.10  2002/05/02 14:05:50  joergr
00311  * Added support for standard and non-standard string streams (which one is
00312  * supported is detected automatically via the configure mechanism).
00313  *
00314  * Revision 1.9  2002/04/16 13:36:02  joergr
00315  * Added configurable support for C++ ANSI standard includes (e.g. streams).
00316  *
00317  * Revision 1.8  2001/06/01 15:51:33  meichel
00318  * Updated copyright header
00319  *
00320  * Revision 1.7  2000/12/13 15:14:25  joergr
00321  * Introduced dummy parameter for "default" constructor of class OFConsole
00322  * to "convince" linker of gcc 2.5.8 (NeXTSTEP) to allocate memory for global
00323  * variable 'ofConsole'.
00324  *
00325  * Revision 1.6  2000/10/10 12:01:21  meichel
00326  * Created/updated doc++ comments
00327  *
00328  * Revision 1.5  2000/09/26 13:46:12  meichel
00329  * Simplified inline code in ofconsol.h, required by Sun CC 2.x
00330  *
00331  * Revision 1.4  2000/06/21 15:47:54  meichel
00332  * Including stdlib.h, required for Sun CC 4.2
00333  *
00334  * Revision 1.3  2000/04/14 15:41:40  meichel
00335  * Added unprotected get methods, required for the cmdata debug facility.
00336  *
00337  * Revision 1.2  2000/04/14 15:16:08  meichel
00338  * Added new class OFConsole and global instance ofConsole which provide
00339  *   access to standard output and error streams in a way that allows multiple
00340  *   threads to safely create output concurrently.
00341  *
00342  * Revision 1.1  2000/03/03 14:02:47  meichel
00343  * Implemented library support for redirecting error messages into memory
00344  *   instead of printing them to stdout/stderr for GUI applications.
00345  *
00346  *
00347  */


Generated on 20 Dec 2005 for OFFIS DCMTK Version 3.5.4 by Doxygen 1.4.5