1 /*=========================================================================
4 Module: $RCSfile: gdcmDebug.h,v $
6 Date: $Date: 2005/11/05 13:21:32 $
7 Version: $Revision: 1.47 $
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
55 /// both debug and warning 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 whether
64 /// warning messages are displayed.
65 static void SetWarningFlag (bool flag);
66 static bool GetWarningFlag ();
67 /// \brief Sets the Warning Flag to true
68 static void WarningOn () { SetWarningFlag(true); }
69 /// \brief Sets the Warning Flag to false
70 static void WarningOff () { SetWarningFlag(false); }
72 /// \brief This is a global flag that controls if debug are redirected
74 static void SetDebugToFile (bool flag);
75 static bool GetDebugToFile ();
76 /// \brief Next debug messages will be sent in the debug file
77 static void DebugToFileOn () { SetDebugToFile(true); }
78 /// \brief Next debug messages will be sent in the standard output
79 static void DebugToFileOff () { SetDebugToFile(false); }
81 static void SetDebugFilename (std::string const &filename);
83 static std::ofstream &GetDebugFile ();
86 } // end namespace gdcm
88 // Here we define function this is the only way to be able to pass
89 // stuff with indirection like:
90 // gdcmDebug( "my message:" << i << '\t' );
91 // You cannot use function unless you use vnsprintf ...
93 // __FUNCTION is not always defined by preprocessor
94 // In c++ we should use __PRETTY_FUNCTION__ instead...
95 #ifdef GDCM_COMPILER_HAS_FUNCTION
96 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
97 // which is a lot nice in C++
99 # define __FUNCTION__ __FUNC__
102 # define GDCM_FUNCTION __PRETTY_FUNCTION__
104 # define GDCM_FUNCTION __FUNCTION__
107 # define GDCM_FUNCTION "<unknow>"
108 #endif //GDCM_COMPILER_HAS_FUNCTION
111 * \brief Debug : To be used to help bug tracking developer
112 * @param msg message part
115 #define gdcmDebugMacro(msg) {}
117 #define gdcmDebugMacro(msg) \
119 if( Debug::GetDebugFlag() ) \
121 std::ostringstream osmacro; \
122 osmacro << "Debug: In " __FILE__ ", line " << __LINE__ \
123 << ", function " << GDCM_FUNCTION << '\n'; \
125 osmacro << "Last system error was: " << \
126 strerror(errno) << '\n'; \
127 osmacro << msg << "\n\n"; \
128 if( Debug::GetDebugToFile() ) \
129 Debug::GetDebugFile() << osmacro.str() << std::endl; \
131 std::cerr << osmacro.str() << std::endl; \
137 * \brief Warning : To be used to warn the user when some oddity occurs
138 * @param msg message part
141 #define gdcmWarningMacro(msg) {}
143 #define gdcmWarningMacro(msg) \
145 if( Debug::GetWarningFlag() ) \
147 std::ostringstream osmacro; \
148 osmacro << "Warning: In " __FILE__ ", line " << __LINE__ \
149 << ", function " << GDCM_FUNCTION << "\n" \
151 if( Debug::GetDebugToFile() ) \
152 Debug::GetDebugFile() << osmacro.str() << std::endl; \
154 std::cerr << osmacro.str() << std::endl; \
160 * \brief Error : To be used when unecoverabale error occurs
161 * at a 'deep' level. (don't use it if file is not ACR/DICOM!)
162 * @param msg second message part
165 #define gdcmErrorMacro(msg) {}
167 #define gdcmErrorMacro(msg) \
169 std::ostringstream osmacro; \
170 osmacro << "Error: In " __FILE__ ", line " << __LINE__ \
171 << ", function " << GDCM_FUNCTION << '\n' \
173 if( Debug::GetDebugToFile() ) \
174 Debug::GetDebugFile() << osmacro.str() << std::endl; \
176 std::cerr << osmacro.str() << std::endl; \
181 * \brief Assert : To be used when an *absolutely* impossible error occurs
182 * No function should be allowed to stop the process instead of
183 * warning the caller!
184 * @param arg argument to test
185 * An easy solution to pass also a message is to do:
186 * gdcmAssertMacro( "my message" && 2 < 3 )
189 #define gdcmAssertMacro(arg) {}
191 #define gdcmAssertMacro(arg) \
195 std::ostringstream osmacro; \
196 osmacro << "Assert: In " __FILE__ ", line " << __LINE__ \
197 << ", function " << GDCM_FUNCTION \
199 if( Debug::GetDebugToFile() ) \
200 Debug::GetDebugFile() << osmacro.str() << std::endl; \
202 std::cerr << osmacro.str() << std::endl; \
208 //-----------------------------------------------------------------------------
210 // Define GDCM_LEGACY macro to mark legacy methods where they are
211 // declared in their class.
213 // WARNING : Don't try to use it with 'inline' methods !
217 // // @deprecated Replaced by MyOtherMethod() as of gdcm 2.0.
218 // GDCM_LEGACY(void MyMethod());
219 #if defined(GDCM_LEGACY_REMOVE)
220 // Remove legacy methods completely.
221 # define GDCM_LEGACY(method)
222 #elif defined(GDCM_LEGACY_SILENT) || defined(SWIG)
223 // Provide legacy methods with no warnings.
224 # define GDCM_LEGACY(method) method
226 // Setup compile-time warnings for uses of deprecated methods if
227 // possible on this compiler.
228 # if defined(__GNUC__) && !defined(__INTEL_COMPILER) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
229 #if defined(__APPLE__) && (__GNUC__ == 3) && (__GNUC_MINOR__ == 3)
230 // Seems like there is a bug in APPLE gcc for deprecated attribute and ctor
231 // This is fixed in g++ 4.0 (Tiger)
232 # define GDCM_LEGACY(method) method
234 # define GDCM_LEGACY(method) method __attribute__((deprecated))
236 # elif defined(_MSC_VER) && _MSC_VER >= 1300
237 # define GDCM_LEGACY(method) __declspec(deprecated) method
239 # define GDCM_LEGACY(method) method
243 // Macros to create runtime deprecation warning messages in function
244 // bodies. Example usage:
246 // void MyClass::MyOldMethod()
248 // GDCM_LEGACY_BODY(MyClass::MyOldMethod, 2.0);
251 // void MyClass::MyMethod()
253 // GDCM_LEGACY_REPLACED_BODY(MyClass::MyMethod, 5.0,
254 // MyClass::MyOtherMethod);
256 #if defined(GDCM_LEGACY_REMOVE) || defined(GDCM_LEGACY_SILENT)
257 # define GDCM_LEGACY_BODY(method, version)
258 # define GDCM_LEGACY_REPLACED_BODY(method, version, replace)
260 # define GDCM_LEGACY_BODY(method, version) \
261 gdcmWarningMacro(#method " was deprecated for gdcm" #version " and will be removed in a future version.")
262 # define GDCM_LEGACY_REPLACED_BODY(method, version, replace) \
263 gdcmWarningMacro(#method " was deprecated for gdcm" #version " and will be removed in a future version. Use " #replace " instead.")