1 /*=========================================================================
4 Module: $RCSfile: gdcmDebug.h,v $
6 Date: $Date: 2005/10/27 09:12:20 $
7 Version: $Revision: 1.46 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
22 #include "gdcmCommon.h"
32 //-----------------------------------------------------------------------------
35 * \brief Debug is an object for debugging in program.
36 * It has 2 debugging modes :
37 * - error : for bad library use, seriously wrong DICOM
38 * - debug : for information/debug messages
39 * - warning : for warning about DICOM quality (kosher)
40 * - assert : design by contract implementation. A function should have
41 * proper input and proper output.
42 * (should not happen, not user controlled)
44 * A debugging message is only shown if the flag is on (DebugFlag)
45 * This is static var and can be set at beginning of code:
46 * gdcm::Debug::SetDebugOn();
48 class GDCM_EXPORT Debug
54 /// \brief This is a global flag that controls whether any debug, warning
55 /// messages are displayed.
56 static void SetDebugFlag (bool flag);
57 static bool GetDebugFlag ();
58 /// \brief Sets the Debug Flag to true
59 static void DebugOn () { SetDebugFlag(true); }
60 /// \brief Sets the Debug Flag to false
61 static void DebugOff () { SetDebugFlag(false); }
63 /// \brief This is a global flag that controls if debug are redirected
65 static void SetDebugToFile (bool flag);
66 static bool GetDebugToFile ();
67 /// \brief Next debug messages will be sent in the debug file
68 static void DebugToFileOn () { SetDebugToFile(true); }
69 /// \brief Next debug messages will be sent in the standard output
70 static void DebugToFileOff () { SetDebugToFile(false); }
72 static void SetDebugFilename (std::string const &filename);
74 static std::ofstream &GetDebugFile ();
77 } // end namespace gdcm
79 // Here we define function this is the only way to be able to pass
80 // stuff with indirection like:
81 // gdcmDebug( "my message:" << i << '\t' );
82 // You cannot use function unless you use vnsprintf ...
84 // __FUNCTION is not always defined by preprocessor
85 // In c++ we should use __PRETTY_FUNCTION__ instead...
86 #ifdef GDCM_COMPILER_HAS_FUNCTION
87 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
88 // which is a lot nice in C++
90 # define __FUNCTION__ __FUNC__
93 # define GDCM_FUNCTION __PRETTY_FUNCTION__
95 # define GDCM_FUNCTION __FUNCTION__
98 # define GDCM_FUNCTION "<unknow>"
99 #endif //GDCM_COMPILER_HAS_FUNCTION
102 * \brief Debug : To be used to help bug tracking developer
103 * @param msg message part
106 #define gdcmDebugMacro(msg) {}
108 #define gdcmDebugMacro(msg) \
110 if( Debug::GetDebugFlag() ) \
112 std::ostringstream osmacro; \
113 osmacro << "Debug: In " __FILE__ ", line " << __LINE__ \
114 << ", function " << GDCM_FUNCTION << '\n'; \
116 osmacro << "Last system error was: " << \
117 strerror(errno) << '\n'; \
118 osmacro << msg << "\n\n"; \
119 if( Debug::GetDebugToFile() ) \
120 Debug::GetDebugFile() << osmacro.str() << std::endl; \
122 std::cerr << osmacro.str() << std::endl; \
128 * \brief Warning : To be used to warn the user when some oddity occurs
129 * @param msg message part
132 #define gdcmWarningMacro(msg) {}
134 #define gdcmWarningMacro(msg) \
136 if( Debug::GetDebugFlag() ) \
138 std::ostringstream osmacro; \
139 osmacro << "Warning: In " __FILE__ ", line " << __LINE__ \
140 << ", function " << GDCM_FUNCTION << "\n" \
142 if( Debug::GetDebugToFile() ) \
143 Debug::GetDebugFile() << osmacro.str() << std::endl; \
145 std::cerr << osmacro.str() << std::endl; \
151 * \brief Error : To be used when unecoverabale error occurs
152 * at a 'deep' level. (don't use it if file is not ACR/DICOM!)
153 * @param msg second message part
156 #define gdcmErrorMacro(msg) {}
158 #define gdcmErrorMacro(msg) \
160 std::ostringstream osmacro; \
161 osmacro << "Error: In " __FILE__ ", line " << __LINE__ \
162 << ", function " << GDCM_FUNCTION << '\n' \
164 if( Debug::GetDebugToFile() ) \
165 Debug::GetDebugFile() << osmacro.str() << std::endl; \
167 std::cerr << osmacro.str() << std::endl; \
172 * \brief Assert : To be used when an *absolutely* impossible error occurs
173 * No function should be allowed to stop the process instead of
174 * warning the caller!
175 * @param arg argument to test
176 * An easy solution to pass also a message is to do:
177 * gdcmAssertMacro( "my message" && 2 < 3 )
180 #define gdcmAssertMacro(arg) {}
182 #define gdcmAssertMacro(arg) \
186 std::ostringstream osmacro; \
187 osmacro << "Assert: In " __FILE__ ", line " << __LINE__ \
188 << ", function " << GDCM_FUNCTION \
190 if( Debug::GetDebugToFile() ) \
191 Debug::GetDebugFile() << osmacro.str() << std::endl; \
193 std::cerr << osmacro.str() << std::endl; \
199 //-----------------------------------------------------------------------------
201 // Define GDCM_LEGACY macro to mark legacy methods where they are
202 // declared in their class.
204 // WARNING : Don't try to use it with 'inline' methods !
208 // // @deprecated Replaced by MyOtherMethod() as of gdcm 2.0.
209 // GDCM_LEGACY(void MyMethod());
210 #if defined(GDCM_LEGACY_REMOVE)
211 // Remove legacy methods completely.
212 # define GDCM_LEGACY(method)
213 #elif defined(GDCM_LEGACY_SILENT) || defined(SWIG)
214 // Provide legacy methods with no warnings.
215 # define GDCM_LEGACY(method) method
217 // Setup compile-time warnings for uses of deprecated methods if
218 // possible on this compiler.
219 # if defined(__GNUC__) && !defined(__INTEL_COMPILER) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
220 #if defined(__APPLE__) && (__GNUC__ == 3) && (__GNUC_MINOR__ == 3)
221 // Seems like there is a bug in APPLE gcc for deprecated attribute and ctor
222 // This is fixed in g++ 4.0 (Tiger)
223 # define GDCM_LEGACY(method) method
225 # define GDCM_LEGACY(method) method __attribute__((deprecated))
227 # elif defined(_MSC_VER) && _MSC_VER >= 1300
228 # define GDCM_LEGACY(method) __declspec(deprecated) method
230 # define GDCM_LEGACY(method) method
234 // Macros to create runtime deprecation warning messages in function
235 // bodies. Example usage:
237 // void MyClass::MyOldMethod()
239 // GDCM_LEGACY_BODY(MyClass::MyOldMethod, 2.0);
242 // void MyClass::MyMethod()
244 // GDCM_LEGACY_REPLACED_BODY(MyClass::MyMethod, 5.0,
245 // MyClass::MyOtherMethod);
247 #if defined(GDCM_LEGACY_REMOVE) || defined(GDCM_LEGACY_SILENT)
248 # define GDCM_LEGACY_BODY(method, version)
249 # define GDCM_LEGACY_REPLACED_BODY(method, version, replace)
251 # define GDCM_LEGACY_BODY(method, version) \
252 gdcmWarningMacro(#method " was deprecated for gdcm" #version " and will be removed in a future version.")
253 # define GDCM_LEGACY_REPLACED_BODY(method, version, replace) \
254 gdcmWarningMacro(#method " was deprecated for gdcm" #version " and will be removed in a future version. Use " #replace " instead.")