GDCM
2.2.3
|
00001 /*========================================================================= 00002 00003 Program: GDCM (Grassroots DICOM). A DICOM library 00004 00005 Copyright (c) 2006-2011 Mathieu Malaterre 00006 All rights reserved. 00007 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details. 00008 00009 This software is distributed WITHOUT ANY WARRANTY; without even 00010 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00011 PURPOSE. See the above copyright notice for more information. 00012 00013 =========================================================================*/ 00014 #ifndef GDCMTRACE_H 00015 #define GDCMTRACE_H 00016 00017 #include "gdcmTypes.h" 00018 #include "gdcmSystem.h" 00019 00020 #include <iosfwd> 00021 #include <cassert> 00022 00023 namespace gdcm 00024 { 00025 00041 class GDCM_EXPORT Trace 00042 { 00043 public : 00044 Trace(); 00045 ~Trace(); 00046 00049 static void SetStream(std::ostream &os); 00050 static std::ostream &GetStream(); 00051 00053 static void SetDebugStream(std::ostream &os); 00054 static std::ostream &GetDebugStream(); 00055 00057 static void SetWarningStream(std::ostream &os); 00058 static std::ostream &GetWarningStream(); 00059 00061 static void SetErrorStream(std::ostream &os); 00062 static std::ostream &GetErrorStream(); 00063 00066 static void SetStreamToFile( const char *filename ); 00067 00069 static void SetDebug(bool debug); 00070 static void DebugOn(); 00071 static void DebugOff(); 00072 static bool GetDebugFlag(); 00073 00075 static void SetWarning(bool debug); 00076 static void WarningOn(); 00077 static void WarningOff(); 00078 static bool GetWarningFlag(); 00079 00081 static void SetError(bool debug); 00082 static void ErrorOn(); 00083 static void ErrorOff(); 00084 static bool GetErrorFlag(); 00085 00086 protected: 00087 private: 00088 }; 00089 00090 // Here we define function this is the only way to be able to pass 00091 // stuff with indirection like: 00092 // gdcmDebug( "my message:" << i << '\t' ); 00093 // You cannot use function unless you use vnsprintf ... 00094 00095 // __FUNCTION is not always defined by preprocessor 00096 // In c++ we should use __PRETTY_FUNCTION__ instead... 00097 #ifdef GDCM_CXX_HAS_FUNCTION 00098 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__ 00099 // which is a lot nice in C++ 00100 #ifdef __BORLANDC__ 00101 # define __FUNCTION__ __FUNC__ 00102 #endif 00103 #ifdef __GNUC__ 00104 # define GDCM_FUNCTION __PRETTY_FUNCTION__ 00105 #else 00106 # define GDCM_FUNCTION __FUNCTION__ 00107 #endif //__GNUC__ 00108 #else 00109 # define GDCM_FUNCTION "<unknow>" 00110 #endif //GDCM_CXX_HAS_FUNCTION 00111 00116 #if defined(NDEBUG) && !defined(GDCM_ALWAYS_TRACE_MACRO) 00117 #define gdcmDebugMacro(msg) {} 00118 #else 00119 #define gdcmDebugMacro(msg) \ 00120 { \ 00121 if( gdcm::Trace::GetDebugFlag() ) \ 00122 { \ 00123 std::ostringstream osmacro; \ 00124 osmacro << "Debug: In " __FILE__ ", line " << __LINE__ \ 00125 << ", function " << GDCM_FUNCTION << '\n' \ 00126 << "Last system error was: " \ 00127 << gdcm::System::GetLastSystemError() << '\n' << msg; \ 00128 std::ostream &_os = gdcm::Trace::GetDebugStream(); \ 00129 _os << osmacro.str() << "\n\n" << std::endl; \ 00130 } \ 00131 } 00132 #endif //NDEBUG 00133 00138 #if defined(NDEBUG) && !defined(GDCM_ALWAYS_TRACE_MACRO) 00139 #define gdcmWarningMacro(msg) {} 00140 #else 00141 #define gdcmWarningMacro(msg) \ 00142 { \ 00143 if( gdcm::Trace::GetWarningFlag() ) \ 00144 { \ 00145 std::ostringstream osmacro; \ 00146 osmacro << "Warning: In " __FILE__ ", line " << __LINE__ \ 00147 << ", function " << GDCM_FUNCTION << "\n" \ 00148 << msg << "\n\n"; \ 00149 std::ostream &_os = gdcm::Trace::GetWarningStream(); \ 00150 _os << osmacro.str() << std::endl; \ 00151 } \ 00152 } 00153 #endif //NDEBUG 00154 00160 #if defined(NDEBUG) && !defined(GDCM_ALWAYS_TRACE_MACRO) 00161 #define gdcmErrorMacro(msg) {} 00162 #else 00163 #define gdcmErrorMacro(msg) \ 00164 { \ 00165 if( gdcm::Trace::GetErrorFlag() ) \ 00166 { \ 00167 std::ostringstream osmacro; \ 00168 osmacro << "Error: In " __FILE__ ", line " << __LINE__ \ 00169 << ", function " << GDCM_FUNCTION << '\n' \ 00170 << msg << "\n\n"; \ 00171 std::ostream &_os = gdcm::Trace::GetErrorStream(); \ 00172 _os << osmacro.str() << std::endl; \ 00173 } \ 00174 } 00175 #endif //NDEBUG 00176 00183 #if defined(NDEBUG) && !defined(GDCM_ALWAYS_TRACE_MACRO) 00184 #define gdcmAssertMacro(arg) {} 00185 #else 00186 #define gdcmAssertMacro(arg) \ 00187 { \ 00188 if( !(arg) ) \ 00189 { \ 00190 std::ostringstream osmacro; \ 00191 osmacro << "Assert: In " __FILE__ ", line " << __LINE__ \ 00192 << ", function " << GDCM_FUNCTION \ 00193 << "\n\n"; \ 00194 std::ostream &_os = gdcm::Trace::GetErrorStream(); \ 00195 _os << osmacro.str() << std::endl; \ 00196 assert ( arg ); \ 00197 } \ 00198 } 00199 #endif //NDEBUG 00200 00207 #if defined(NDEBUG) && !defined(GDCM_ALWAYS_TRACE_MACRO) 00208 // User asked for release compilation, but still need to report 00209 // if grave issue. 00210 #define gdcmAssertAlwaysMacro(arg) \ 00211 { \ 00212 if( !(arg) ) \ 00213 { \ 00214 std::ostringstream osmacro; \ 00215 osmacro << "Assert: In " __FILE__ ", line " << __LINE__ \ 00216 << ", function " << GDCM_FUNCTION \ 00217 << "\n\n"; \ 00218 throw osmacro.str(); \ 00219 } \ 00220 } 00221 #else 00222 // Simply reproduce gdcmAssertMacro behavior: 00223 #define gdcmAssertAlwaysMacro(arg) gdcmAssertMacro(arg) 00224 #endif //NDEBUG 00225 00226 } // end namespace gdcm 00227 //----------------------------------------------------------------------------- 00228 #endif //GDCMTRACE_H