# gdcmTrace.h

Go to the documentation of this file.

    00001 /*=========================================================================
    00002
    00003   Program: GDCM (Grassroots DICOM). A DICOM library
    00004   Module:  $URL$
    00005
    00006   Copyright (c) 2006-2010 Mathieu Malaterre
    00007   All rights reserved.
    00008   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
    00009
    00010      This software is distributed WITHOUT ANY WARRANTY; without even
    00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    00012      PURPOSE.  See the above copyright notice for more information.
    00013
    00014 =========================================================================*/
    00015 #ifndef GDCMTRACE_H
    00016 #define GDCMTRACE_H
    00017
    00018 #include "gdcmTypes.h"
    00019
    00020 #include <string>
    00021 #include <vector>
    00022 #include <iostream>
    00023 #include <fstream>
    00024 #include <sstream>
    00025 #include <assert.h>
    00026 #include <errno.h>
    00027 #include <string.h> // strerror
    00028
    00029
    00030 namespace gdcm
    00031 {
    00032
    00037 class GDCM_EXPORT Trace
    00038 {
    00039 public :
    00040   Trace();
    00041   ~Trace();
    00042
    00043   static void SetDebug(bool debug); //  { DebugFlag = true; };
    00044   static void DebugOn(); //  { DebugFlag = true; };
    00045   static void DebugOff(); // { DebugFlag = false; };
    00046   static bool GetDebugFlag(); // { DebugFlag = false; };
    00047
    00048   static void SetWarning(bool debug); //  { DebugFlag = true; };
    00049   static void WarningOn(); //  { WarningFlag = true; };
    00050   static void WarningOff(); // { WarningFlag = false; };
    00051   static bool GetWarningFlag();
    00052
    00053   static void SetError(bool debug); //  { DebugFlag = true; };
    00054   static void ErrorOn(); //  { ErrorFlag = true; };
    00055   static void ErrorOff(); // { ErrorFlag = false; };
    00056   static bool GetErrorFlag();
    00057
    00058   static bool GetDebugToFile ();
    00059   static std::ofstream &GetDebugFile ();
    00060
    00061 protected:
    00062 private:
    00063 //  static bool DebugFlag;
    00064 //  static bool WarningFlag;
    00065 //  static bool ErrorFlag;
    00066 };
    00067
    00068 // Here we define function this is the only way to be able to pass
    00069 // stuff with indirection like:
    00070 // gdcmDebug( "my message:" << i << '\t' );
    00071 // You cannot use function unless you use vnsprintf ...
    00072
    00073 // __FUNCTION is not always defined by preprocessor
    00074 // In c++ we should use __PRETTY_FUNCTION__ instead...
    00075 #ifdef GDCM_CXX_HAS_FUNCTION
    00076 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
    00077 // which is a lot nice in C++
    00078 #ifdef __BORLANDC__
    00079 #  define __FUNCTION__ __FUNC__
    00080 #endif
    00081 #ifdef __GNUC__
    00082 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
    00083 #else
    00084 #  define GDCM_FUNCTION __FUNCTION__
    00085 #endif //__GNUC__
    00086 #else
    00087 #  define GDCM_FUNCTION "<unknow>"
    00088 #endif //GDCM_CXX_HAS_FUNCTION
    00089
    00094 #ifdef NDEBUG
    00095 #define gdcmDebugMacro(msg) {}
    00096 #else
    00097 #define gdcmDebugMacro(msg)                                       \
    00098 {                                                                 \
    00099    if( gdcm::Trace::GetDebugFlag() )                              \
    00100    {                                                              \
    00101    std::ostringstream osmacro;                                    \
    00102    osmacro << "Debug: In " __FILE__ ", line " << __LINE__         \
    00103            << ", function " << GDCM_FUNCTION << '\n'              \
    00104            << "Last system error was: " << strerror(errno)        \
    00105            << '\n' << msg << "\n\n";                              \
    00106    if( gdcm::Trace::GetDebugToFile() )                            \
    00107       gdcm::Trace::GetDebugFile() << osmacro.str() << std::endl;  \
    00108    else                                                           \
    00109       std::cerr << osmacro.str() << std::endl;                    \
    00110    }                                                              \
    00111 }
    00112 #endif //NDEBUG
    00113
    00118 #ifdef NDEBUG
    00119 #define gdcmWarningMacro(msg) {}
    00120 #else
    00121 #define gdcmWarningMacro(msg)                                     \
    00122 {                                                                 \
    00123    if( gdcm::Trace::GetWarningFlag() )                            \
    00124    {                                                              \
    00125    std::ostringstream osmacro;                                    \
    00126    osmacro << "Warning: In " __FILE__ ", line " << __LINE__       \
    00127            << ", function " << GDCM_FUNCTION << "\n"              \
    00128            << msg << "\n\n";                                      \
    00129    if( gdcm::Trace::GetDebugToFile() )                            \
    00130       gdcm::Trace::GetDebugFile() << osmacro.str() << std::endl;  \
    00131    else                                                           \
    00132       std::cerr << osmacro.str() << std::endl;                    \
    00133    }                                                              \
    00134 }
    00135 #endif //NDEBUG
    00136
    00142 #ifdef NDEBUG
    00143 #define gdcmErrorMacro(msg) {}
    00144 #else
    00145 #define gdcmErrorMacro(msg)                                       \
    00146 {                                                                 \
    00147    if( gdcm::Trace::GetErrorFlag() )                              \
    00148    {                                                              \
    00149    std::ostringstream osmacro;                                    \
    00150    osmacro << "Error: In " __FILE__ ", line " << __LINE__         \
    00151            << ", function " << GDCM_FUNCTION << '\n'              \
    00152            << msg << "\n\n";                                      \
    00153    if( gdcm::Trace::GetDebugToFile() )                            \
    00154       gdcm::Trace::GetDebugFile() << osmacro.str() << std::endl;  \
    00155    else                                                           \
    00156       std::cerr << osmacro.str() << std::endl;                    \
    00157    }                                                              \
    00158 }
    00159 #endif //NDEBUG
    00160
    00167 #ifdef NDEBUG
    00168 #define gdcmAssertMacro(arg) {}
    00169 #else
    00170 #define gdcmAssertMacro(arg)                                      \
    00171 {                                                                 \
    00172    if( !(arg) )                                                   \
    00173    {                                                              \
    00174    std::ostringstream osmacro;                                    \
    00175    osmacro << "Assert: In " __FILE__ ", line " << __LINE__        \
    00176            << ", function " << GDCM_FUNCTION                      \
    00177            << "\n\n";                                             \
    00178    if( gdcm::Trace::GetDebugToFile() )                            \
    00179       gdcm::Trace::GetDebugFile() << osmacro.str() << std::endl;  \
    00180    else                                                           \
    00181       std::cerr << osmacro.str() << std::endl;                    \
    00182    assert ( arg );                                                \
    00183    }                                                              \
    00184 }
    00185 #endif //NDEBUG
    00186
    00193 #ifdef NDEBUG
    00194 // User asked for release compilation, but still need to report
    00195 // if grave issue.
    00196 #define gdcmAssertAlwaysMacro(arg) \
    00197 {                                                                 \
    00198    if( !(arg) )                                                   \
    00199    {                                                              \
    00200    std::ostringstream osmacro;                                    \
    00201    osmacro << "Assert: In " __FILE__ ", line " << __LINE__        \
    00202            << ", function " << GDCM_FUNCTION                      \
    00203            << "\n\n";                                             \
    00204    throw osmacro.str();                                           \
    00205    }                                                              \
    00206 }
    00207 #else
    00208 // Simply reproduce gdcmAssertMacro behavior:
    00209 #define gdcmAssertAlwaysMacro(arg) gdcmAssertMacro(arg)
    00210 #endif //NDEBUG
    00211
    00212 } // end namespace gdcm
    00213 //-----------------------------------------------------------------------------
    00214 #endif //GDCMTRACE_H