1 /*=========================================================================
4 Module: $RCSfile: gdcmDebug.h,v $
6 Date: $Date: 2005/09/02 07:10:03 $
7 Version: $Revision: 1.42 $
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"
31 //-----------------------------------------------------------------------------
34 * \brief Debug is an object for debugging in program.
35 * It has 2 debugging modes :
36 * - error : for bad library use, seriously wrong DICOM
37 * - debug : for information/debug messages
38 * - warning : for warning about DICOM quality (kosher)
39 * - assert : design by contract implementation. A function should have
40 * proper input and proper output.
41 * (should not happen, not user controlled)
43 * A debugging message is only shown if the flag is on (DebugFlag)
44 * This is static var and can be set at beginning of code:
45 * gdcm::Debug::SetDebugOn();
47 class GDCM_EXPORT Debug
53 /// \brief This is a global flag that controls whether any debug, warning
54 /// messages are displayed.
55 static void SetDebugFlag (bool flag);
56 static bool GetDebugFlag ();
57 /// \brief Sets the Debug Flag to true
58 static void DebugOn () { SetDebugFlag(true); }
59 /// \brief Sets the Debug Flag to false
60 static void DebugOff () { SetDebugFlag(false); }
62 /// \brief This is a global flag that controls if debug are redirected
64 static void SetDebugToFile (bool flag);
65 static bool GetDebugToFile ();
66 /// \brief Next debug messages will be sent in the debug file
67 static void DebugToFileOn () { SetDebugToFile(true); }
68 /// \brief Next debug messages will be sent in the standard output
69 static void DebugToFileOff () { SetDebugToFile(false); }
71 static void SetDebugFilename (std::string const &filename);
73 static std::ofstream &GetDebugFile ();
76 } // end namespace gdcm
78 // Here we define function this is the only way to be able to pass
79 // stuff with indirection like:
80 // gdcmDebug( "my message:" << i << '\t' );
81 // You cannot use function unless you use vnsprintf ...
83 // __FUNCTION is not always defined by preprocessor
84 // In c++ we should use __PRETTY_FUNCTION__ instead...
85 #ifdef GDCM_COMPILER_HAS_FUNCTION
86 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
87 // which is a lot nice in C++
89 # define __FUNCTION__ __FUNC__
92 # define GDCM_FUNCTION __PRETTY_FUNCTION__
94 # define GDCM_FUNCTION __FUNCTION__
97 # define GDCM_FUNCTION "<unknow>"
98 #endif //GDCM_COMPILER_HAS_FUNCTION
102 * @param msg message part
105 #define gdcmDebugMacro(msg) {}
107 #define gdcmDebugMacro(msg) \
109 if( Debug::GetDebugFlag() ) \
111 std::ostringstream osmacro; \
112 osmacro << "Debug: In " __FILE__ ", line " << __LINE__ \
113 << ", function " << GDCM_FUNCTION << '\n' \
114 << "Last system error was: " << strerror(errno) \
115 << '\n' << msg << "\n\n"; \
116 if( Debug::GetDebugToFile() ) \
117 Debug::GetDebugFile() << osmacro.str() << std::endl; \
119 std::cerr << osmacro.str() << std::endl; \
126 * @param msg message part
129 #define gdcmWarningMacro(msg) {}
131 #define gdcmWarningMacro(msg) \
133 if( Debug::GetDebugFlag() ) \
135 std::ostringstream osmacro; \
136 osmacro << "Warning: In " __FILE__ ", line " << __LINE__ \
137 << ", function " << GDCM_FUNCTION << "\n" \
139 if( Debug::GetDebugToFile() ) \
140 Debug::GetDebugFile() << osmacro.str() << std::endl; \
142 std::cerr << osmacro.str() << std::endl; \
149 * @param msg second message part
152 #define gdcmErrorMacro(msg) {}
154 #define gdcmErrorMacro(msg) \
156 std::ostringstream osmacro; \
157 osmacro << "Error: In " __FILE__ ", line " << __LINE__ \
158 << ", function " << GDCM_FUNCTION << '\n' \
160 if( Debug::GetDebugToFile() ) \
161 Debug::GetDebugFile() << osmacro.str() << std::endl; \
163 std::cerr << osmacro.str() << std::endl; \
169 * @param arg argument to test
170 * An easy solution to pass also a message is to do:
171 * gdcmAssertMacro( "my message" && 2 < 3 )
174 #define gdcmAssertMacro(arg) {}
176 #define gdcmAssertMacro(arg) \
180 std::ostringstream osmacro; \
181 osmacro << "Assert: In " __FILE__ ", line " << __LINE__ \
182 << ", function " << GDCM_FUNCTION \
184 if( Debug::GetDebugToFile() ) \
185 Debug::GetDebugFile() << osmacro.str() << std::endl; \
187 std::cerr << osmacro.str() << std::endl; \
193 //-----------------------------------------------------------------------------
195 // Define GDCM_LEGACY macro to mark legacy methods where they are
196 // declared in their class.
198 // WARNING : Don't try to use it with 'inline' methods !
202 // // @deprecated Replaced by MyOtherMethod() as of gdcm 2.0.
203 // GDCM_LEGACY(void MyMethod());
204 #if defined(GDCM_LEGACY_REMOVE)
205 // Remove legacy methods completely.
206 # define GDCM_LEGACY(method)
207 #elif defined(GDCM_LEGACY_SILENT) || defined(SWIG)
208 // Provide legacy methods with no warnings.
209 # define GDCM_LEGACY(method) method
211 // Setup compile-time warnings for uses of deprecated methods if
212 // possible on this compiler.
213 # if defined(__GNUC__) && !defined(__INTEL_COMPILER) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
214 #if defined(__APPLE__) && (__GNUC__ == 3) && (__GNUC_MINOR__ == 3)
215 // Seems like there is a bug in APPLE gcc for deprecated attribute and ctor
216 // This is fixed in g++ 4.0 (Tiger)
217 # define GDCM_LEGACY(method) method
219 # define GDCM_LEGACY(method) method __attribute__((deprecated))
221 # elif defined(_MSC_VER) && _MSC_VER >= 1300
222 # define GDCM_LEGACY(method) __declspec(deprecated) method
224 # define GDCM_LEGACY(method) method
228 // Macros to create runtime deprecation warning messages in function
229 // bodies. Example usage:
231 // void MyClass::MyOldMethod()
233 // GDCM_LEGACY_BODY(MyClass::MyOldMethod, 2.0);
236 // void MyClass::MyMethod()
238 // GDCM_LEGACY_REPLACED_BODY(MyClass::MyMethod, 5.0,
239 // MyClass::MyOtherMethod);
241 #if defined(GDCM_LEGACY_REMOVE) || defined(GDCM_LEGACY_SILENT)
242 # define GDCM_LEGACY_BODY(method, version)
243 # define GDCM_LEGACY_REPLACED_BODY(method, version, replace)
245 # define GDCM_LEGACY_BODY(method, version) \
246 gdcmWarningMacro(#method " was deprecated for gdcm" #version " and will be removed in a future version.")
247 # define GDCM_LEGACY_REPLACED_BODY(method, version, replace) \
248 gdcmWarningMacro(#method " was deprecated for gdcm" #version " and will be removed in a future version. Use " #replace " instead.")