00001 /* 00002 * 00003 * Copyright (C) 1999-2010, OFFIS e.V. 00004 * All rights reserved. See COPYRIGHT file for details. 00005 * 00006 * This software and supporting documentation were developed by 00007 * 00008 * OFFIS e.V. 00009 * R&D Division Health 00010 * Escherweg 2 00011 * D-26121 Oldenburg, Germany 00012 * 00013 * 00014 * Module: ofstd 00015 * 00016 * Author: Marco Eichelberg 00017 * 00018 * Purpose: Define general purpose facility for console output 00019 * 00020 * class OFConsole and its global instance, ofConsole, 00021 * provide access to the standard console output and error streams 00022 * in a way that allows multiple threads to concurrently create output 00023 * even if that output is redirected, e. g. to file or memory. 00024 * Protection is implemented if the module is compiled with -DWITH_THREADS 00025 * and is based on Mutexes. 00026 * 00027 * In cases where DCMTK is used for GUI development, the fact that the 00028 * libraries send many error messages to the standard or error streams 00029 * are annoying since these streams are not present in a GUI environment. 00030 * Either the messages just go lost or they even cause the GUI 00031 * application to fail. This file introduces aliases for the standard 00032 * stream handles called COUT and CERR, which are normally only 00033 * preprocessor macros for cout and cerr, respectively. If the 00034 * toolkit is compiled with the flag DCMTK_GUI defined, however, these 00035 * streams are created as OFOStringStream. This will allow a GUI based 00036 * application to extract the messages and either present them to the 00037 * user or store them in a log file. 00038 * 00039 * GUI based applications making use of this feature should periodically 00040 * check and clear these streams in order to avoid increasing consumption 00041 * of heap memory. 00042 * 00043 * Caveat 1: The DCMTK command line tools do not yet support the DCMTK_GUI 00044 * flag, and will most likely exhibit all kinds of undesired behaviour 00045 * if this flag is used. 00046 * 00047 * Caveat 2: The direct use of the COUT and CERR macros is unsafe 00048 * in multithread applications. Use ofConsole instead. 00049 * 00050 * Last Update: $Author: joergr $ 00051 * Update Date: $Date: 2010-10-14 13:15:50 $ 00052 * CVS/RCS Revision: $Revision: 1.21 $ 00053 * Status: $State: Exp $ 00054 * 00055 * CVS/RCS Log at end of file 00056 * 00057 */ 00058 00059 00060 #ifndef OFCONSOL_H 00061 #define OFCONSOL_H 00062 00063 #include "dcmtk/config/osconfig.h" 00064 #include "dcmtk/ofstd/ofstream.h" 00065 #include "dcmtk/ofstd/ofthread.h" 00066 00067 #define INCLUDE_CSTDLIB 00068 #include "dcmtk/ofstd/ofstdinc.h" 00069 00070 00079 class OFConsole 00080 { 00081 public: 00082 00085 virtual ~OFConsole(){ } 00086 00091 STD_NAMESPACE ostream& lockCout() 00092 { 00093 #ifdef WITH_THREADS 00094 coutMutex.lock(); 00095 #endif 00096 return *currentCout; 00097 } 00098 00101 void unlockCout() 00102 { 00103 #ifdef WITH_THREADS 00104 coutMutex.unlock(); 00105 #endif 00106 } 00107 00113 STD_NAMESPACE ostream& getCout() 00114 { 00115 return *currentCout; 00116 } 00117 00129 STD_NAMESPACE ostream *setCout(STD_NAMESPACE ostream *newCout=NULL); 00130 00135 STD_NAMESPACE ostream& lockCerr() 00136 { 00137 #ifdef WITH_THREADS 00138 cerrMutex.lock(); 00139 #endif 00140 if (joined) 00141 { 00142 #ifdef WITH_THREADS 00143 coutMutex.lock(); 00144 #endif 00145 return *currentCout; 00146 } 00147 else return *currentCerr; 00148 } 00149 00155 STD_NAMESPACE ostream& getCerr() 00156 { 00157 if (joined) return *currentCout; 00158 else return *currentCerr; 00159 } 00160 00163 void unlockCerr() 00164 { 00165 #ifdef WITH_THREADS 00166 if (joined) coutMutex.unlock(); 00167 cerrMutex.unlock(); 00168 #endif 00169 } 00170 00182 STD_NAMESPACE ostream *setCerr(STD_NAMESPACE ostream *newCerr=NULL); 00183 00190 void join(); 00191 00198 void split(); 00199 00205 OFBool isJoined(); 00206 00210 static OFConsole& instance(); 00211 00212 private: 00213 00219 OFConsole(); 00220 00222 OFConsole(const OFConsole &arg); 00223 00225 OFConsole& operator=(const OFConsole &arg); 00226 00228 STD_NAMESPACE ostream *currentCout; 00229 00231 STD_NAMESPACE ostream *currentCerr; 00232 00234 int joined; 00235 00236 #ifdef WITH_THREADS 00237 00238 OFMutex coutMutex; 00239 00241 OFMutex cerrMutex; 00242 #endif 00243 00244 // dummy declaration to keep gcc quiet 00245 friend class OFConsoleDummyFriend; 00246 }; 00247 00248 00252 #define ofConsole (OFConsole::instance()) 00253 00254 /* 00255 * definitions for COUT, CERR, CLOG. 00256 * 00257 * NOTE: DIRECT USE OF THESE MACROS IS UNSAFE IN MULTITHREAD APPLICATIONS. 00258 */ 00259 00260 #ifdef DCMTK_GUI 00261 00262 extern OFOStringStream COUT; 00263 extern OFOStringStream CERR; 00264 00265 #else /* DCMTK_GUI */ 00266 00267 #define COUT (ofConsole.getCout()) 00268 #define CERR (ofConsole.getCerr()) 00269 00270 #endif /* DCMTK_GUI */ 00271 00272 #endif /* OFCONSOL_H */ 00273 00274 00275 /* 00276 * 00277 * CVS/RCS Log: 00278 * $Log: ofconsol.h,v $ 00279 * Revision 1.21 2010-10-14 13:15:50 joergr 00280 * Updated copyright header. Added reference to COPYRIGHT file. 00281 * 00282 * Revision 1.20 2010-10-04 14:44:47 joergr 00283 * Replaced "#ifdef _REENTRANT" by "#ifdef WITH_THREADS" where appropriate (i.e. 00284 * in all cases where OFMutex, OFReadWriteLock, etc. are used). 00285 * 00286 * Revision 1.19 2006/08/14 16:42:26 meichel 00287 * Updated all code in module ofstd to correctly compile if the standard 00288 * namespace has not included into the global one with a "using" directive. 00289 * 00290 * Revision 1.18 2005/12/08 16:05:52 meichel 00291 * Changed include path schema for all DCMTK header files 00292 * 00293 * Revision 1.17 2004/01/21 11:50:10 meichel 00294 * Fixed DOS CR/LF in preprocessor directives 00295 * 00296 * Revision 1.16 2004/01/16 10:30:12 joergr 00297 * Removed acknowledgements with e-mail addresses from CVS log. 00298 * 00299 * Revision 1.15 2003/12/17 17:38:39 meichel 00300 * Changed definition of COUT and CERR macros to allow redirection to file. 00301 * 00302 * Revision 1.14 2003/12/05 10:37:41 joergr 00303 * Removed leading underscore characters from preprocessor symbols (reserved 00304 * symbols). Updated copyright date where appropriate. 00305 * 00306 * Revision 1.13 2002/11/27 11:23:05 meichel 00307 * Adapted module ofstd to use of new header file ofstdinc.h 00308 * 00309 * Revision 1.12 2002/05/16 15:56:33 meichel 00310 * Changed ofConsole into singleton implementation that can safely 00311 * be used prior to start of main, i.e. from global constructors 00312 * 00313 * Revision 1.11 2002/05/16 08:16:44 meichel 00314 * changed return type of OFConsole::setCout() and OFConsole::setCerr() 00315 * to pointer instead of reference. 00316 * 00317 * Revision 1.10 2002/05/02 14:05:50 joergr 00318 * Added support for standard and non-standard string streams (which one is 00319 * supported is detected automatically via the configure mechanism). 00320 * 00321 * Revision 1.9 2002/04/16 13:36:02 joergr 00322 * Added configurable support for C++ ANSI standard includes (e.g. streams). 00323 * 00324 * Revision 1.8 2001/06/01 15:51:33 meichel 00325 * Updated copyright header 00326 * 00327 * Revision 1.7 2000/12/13 15:14:25 joergr 00328 * Introduced dummy parameter for "default" constructor of class OFConsole 00329 * to "convince" linker of gcc 2.5.8 (NeXTSTEP) to allocate memory for global 00330 * variable 'ofConsole'. 00331 * 00332 * Revision 1.6 2000/10/10 12:01:21 meichel 00333 * Created/updated doc++ comments 00334 * 00335 * Revision 1.5 2000/09/26 13:46:12 meichel 00336 * Simplified inline code in ofconsol.h, required by Sun CC 2.x 00337 * 00338 * Revision 1.4 2000/06/21 15:47:54 meichel 00339 * Including stdlib.h, required for Sun CC 4.2 00340 * 00341 * Revision 1.3 2000/04/14 15:41:40 meichel 00342 * Added unprotected get methods, required for the cmdata debug facility. 00343 * 00344 * Revision 1.2 2000/04/14 15:16:08 meichel 00345 * Added new class OFConsole and global instance ofConsole which provide 00346 * access to standard output and error streams in a way that allows multiple 00347 * threads to safely create output concurrently. 00348 * 00349 * Revision 1.1 2000/03/03 14:02:47 meichel 00350 * Implemented library support for redirecting error messages into memory 00351 * instead of printing them to stdout/stderr for GUI applications. 00352 * 00353 * 00354 */