Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gdcmDebug.h

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmDebug.h,v $
00005   Language:  C++
00006   Date:      $Date: 2005/02/09 21:37:45 $
00007   Version:   $Revision: 1.31 $
00008                                                                                 
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012                                                                                 
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
00016                                                                                 
00017 =========================================================================*/
00018 
00019 #ifndef GDCMDEBUG_H
00020 #define GDCMDEBUG_H
00021 
00022 #include "gdcmCommon.h"
00023 
00024 #include <sstream>
00025 #include <fstream>
00026 #include <assert.h>
00027 #include <errno.h>
00028 
00029 namespace gdcm 
00030 {
00031 //-----------------------------------------------------------------------------
00032 
00044 class GDCM_EXPORT Debug
00045 {
00046 public:
00047    Debug();
00048    ~Debug();
00049 
00052    static void SetDebugFlag (bool flag);
00053    static bool GetDebugFlag ();
00055    static void DebugOn  () { SetDebugFlag(true);  };
00057    static void DebugOff () { SetDebugFlag(false); };
00058 
00061    static void SetDebugToFile (bool flag);
00062    static bool GetDebugToFile ();
00064    static void DebugToFileOn  () { SetDebugToFile(true);  };
00066    static void DebugToFileOff () { SetDebugToFile(false); };
00067 
00068    static void SetDebugFilename (std::string const &filename);
00069 
00070    static std::ofstream &GetDebugFile ();
00071 };
00072 
00073 } // end namespace gdcm
00074 
00075 // Here we define function this is the only way to be able to pass
00076 // stuff with indirection like:
00077 // gdcmDebug( "my message:" << i << '\t' ); 
00078 // You cannot use function unless you use vnsprintf ...
00079 
00080 // __FUNCTION is not always defined by preprocessor
00081 // In c++ we should use __PRETTY_FUNCTION__ instead...
00082 #ifdef GDCM_COMPILER_HAS_FUNCTION
00083 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
00084 // which is a lot nice in C++
00085 #ifdef __BORLANDC__
00086 #  define __FUNCTION__ __FUNC__
00087 #endif
00088 #ifdef __GNUC__
00089 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
00090 #else
00091 #  define GDCM_FUNCTION __FUNCTION__ 
00092 #endif //__GNUC__
00093 #else
00094 #  define GDCM_FUNCTION "<unknow>"
00095 #endif //GDCM_COMPILER_HAS_FUNCTION
00096 
00101 #ifdef NDEBUG
00102 #define gdcmDebugMacro(msg)
00103 #else
00104 #define gdcmDebugMacro(msg)                                 \
00105 {                                                           \
00106    if( Debug::GetDebugFlag() )                              \
00107    {                                                        \
00108    std::ostringstream osmacro;                              \
00109    osmacro << "Debug: In " __FILE__ ", line " << __LINE__   \
00110            << ", function " << GDCM_FUNCTION << '\n'        \
00111            << "Last system error was: " << strerror(errno)  \
00112            << '\n' << msg << "\n\n";                        \
00113    if( Debug::GetDebugToFile() )                            \
00114       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
00115    else                                                     \
00116       std::cerr << osmacro.str() << std::endl;              \
00117    }                                                        \
00118 }
00119 #endif //NDEBUG
00120 
00125 #ifdef NDEBUG
00126 #define gdcmWarningMacro(msg)
00127 #else
00128 #define gdcmWarningMacro(msg)                               \
00129 {                                                           \
00130    if( Debug::GetDebugFlag() )                              \
00131    {                                                        \
00132    std::ostringstream osmacro;                              \
00133    osmacro << "Verbose: In " __FILE__ ", line " << __LINE__ \
00134            << ", function " << GDCM_FUNCTION << "\n"        \
00135            << msg << "\n\n";                                \
00136    if( Debug::GetDebugToFile() )                            \
00137       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
00138    else                                                     \
00139       std::cerr << osmacro.str() << std::endl;              \
00140    }                                                        \
00141 }
00142 #endif //NDEBUG
00143 
00148 #ifdef NDEBUG
00149 #define gdcmErrorMacro(msg)
00150 #else
00151 #define gdcmErrorMacro(msg)                                 \
00152 {                                                           \
00153    std::ostringstream osmacro;                              \
00154    osmacro << "Error: In " __FILE__ ", line " << __LINE__   \
00155            << ", function " << GDCM_FUNCTION << '\n'        \
00156            << msg << "\n\n";                                \
00157    if( Debug::GetDebugToFile() )                            \
00158       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
00159    else                                                     \
00160       std::cerr << osmacro.str() << std::endl;              \
00161 }
00162 #endif //NDEBUG
00163 
00170 #ifdef NDEBUG
00171 #define gdcmAssertMacro(arg)
00172 #else
00173 #define gdcmAssertMacro(arg)                                \
00174 {                                                           \
00175    if( !(arg) )                                             \
00176    {                                                        \
00177    std::ostringstream osmacro;                              \
00178    osmacro << "Assert: In " __FILE__ ", line " << __LINE__  \
00179            << ", function " << GDCM_FUNCTION                \
00180            << "\n\n";                                       \
00181    if( Debug::GetDebugToFile() )                            \
00182       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
00183    else                                                     \
00184       std::cerr << osmacro.str() << std::endl;              \
00185    assert ( arg );                                          \
00186    }                                                        \
00187 }
00188 #endif //NDEBUG
00189 
00190 #endif

Generated on Thu Feb 10 22:17:56 2005 for gdcm by doxygen 1.3.6