]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
Flags Debug and Warning may be used separately.
[gdcm.git] / src / gdcmDebug.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.h,v $
5   Language:  C++
6   Date:      $Date: 2005/11/05 13:21:32 $
7   Version:   $Revision: 1.47 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #ifndef GDCMDEBUG_H
20 #define GDCMDEBUG_H
21
22 #include "gdcmCommon.h"
23
24 #include <iostream>
25 #include <sstream>
26 #include <fstream>
27 #include <assert.h>
28 #include <errno.h>
29
30 namespace gdcm 
31 {
32 //-----------------------------------------------------------------------------
33
34 /**
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)
43  * 
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();
47  */
48 class GDCM_EXPORT Debug
49 {
50 public:
51    Debug();
52    ~Debug();
53
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); }
62    
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); }   
71
72    /// \brief This is a global flag that controls if debug are redirected
73    ///        to a file or not
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); }
80
81    static void SetDebugFilename (std::string const &filename);
82
83    static std::ofstream &GetDebugFile ();
84 };
85
86 } // end namespace gdcm
87
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 ...
92
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++
98 #ifdef __BORLANDC__
99 #  define __FUNCTION__ __FUNC__
100 #endif
101 #ifdef __GNUC__
102 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
103 #else
104 #  define GDCM_FUNCTION __FUNCTION__ 
105 #endif //__GNUC__
106 #else
107 #  define GDCM_FUNCTION "<unknow>"
108 #endif //GDCM_COMPILER_HAS_FUNCTION
109
110 /**
111  * \brief   Debug : To be used to help bug tracking developer
112  * @param msg message part
113  */
114 #ifdef NDEBUG
115 #define gdcmDebugMacro(msg) {}
116 #else
117 #define gdcmDebugMacro(msg)                                 \
118 {                                                           \
119    if( Debug::GetDebugFlag() )                              \
120    {                                                        \
121    std::ostringstream osmacro;                              \
122    osmacro << "Debug: In " __FILE__ ", line " << __LINE__   \
123            << ", function " << GDCM_FUNCTION << '\n';       \
124    if( errno )                                              \
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;  \
130    else                                                     \
131       std::cerr << osmacro.str() << std::endl;              \
132    }                                                        \
133 }
134 #endif //NDEBUG
135
136 /**
137  * \brief   Warning : To be used to warn the user when some oddity occurs
138  * @param msg message part
139  */
140 #ifdef NDEBUG
141 #define gdcmWarningMacro(msg) {}
142 #else
143 #define gdcmWarningMacro(msg)                               \
144 {                                                           \
145    if( Debug::GetWarningFlag() )                            \
146    {                                                        \
147    std::ostringstream osmacro;                              \
148    osmacro << "Warning: In " __FILE__ ", line " << __LINE__ \
149            << ", function " << GDCM_FUNCTION << "\n"        \
150            << msg << "\n\n";                                \
151    if( Debug::GetDebugToFile() )                            \
152       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
153    else                                                     \
154       std::cerr << osmacro.str() << std::endl;              \
155    }                                                        \
156 }
157 #endif //NDEBUG
158
159 /**
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 
163  */
164 #ifdef NDEBUG
165 #define gdcmErrorMacro(msg) {}
166 #else
167 #define gdcmErrorMacro(msg)                                 \
168 {                                                           \
169    std::ostringstream osmacro;                              \
170    osmacro << "Error: In " __FILE__ ", line " << __LINE__   \
171            << ", function " << GDCM_FUNCTION << '\n'        \
172            << msg << "\n\n";                                \
173    if( Debug::GetDebugToFile() )                            \
174       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
175    else                                                     \
176       std::cerr << osmacro.str() << std::endl;              \
177 }
178 #endif //NDEBUG
179
180 /**
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 )
187  */
188 #ifdef NDEBUG
189 #define gdcmAssertMacro(arg) {}
190 #else
191 #define gdcmAssertMacro(arg)                                \
192 {                                                           \
193    if( !(arg) )                                             \
194    {                                                        \
195    std::ostringstream osmacro;                              \
196    osmacro << "Assert: In " __FILE__ ", line " << __LINE__  \
197            << ", function " << GDCM_FUNCTION                \
198            << "\n\n";                                       \
199    if( Debug::GetDebugToFile() )                            \
200       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
201    else                                                     \
202       std::cerr << osmacro.str() << std::endl;              \
203    assert ( arg );                                          \
204    }                                                        \
205 }
206 #endif //NDEBUG
207
208 //-----------------------------------------------------------------------------
209 //
210 // Define GDCM_LEGACY macro to mark legacy methods where they are
211 // declared in their class.
212 // 
213 // WARNING : Don't try to use it with 'inline' methods ! 
214 //
215 //Example usage:
216 //
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
225 #else
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
233 #else
234 #  define GDCM_LEGACY(method) method __attribute__((deprecated))
235 #endif
236 # elif defined(_MSC_VER) && _MSC_VER >= 1300
237 #  define GDCM_LEGACY(method) __declspec(deprecated) method
238 # else
239 #  define GDCM_LEGACY(method) method
240 # endif
241 #endif
242
243 // Macros to create runtime deprecation warning messages in function
244 // bodies.  Example usage:
245 //
246 //   void MyClass::MyOldMethod()
247 //   {
248 //     GDCM_LEGACY_BODY(MyClass::MyOldMethod, 2.0);
249 //   }
250 //
251 //   void MyClass::MyMethod()
252 //   {
253 //     GDCM_LEGACY_REPLACED_BODY(MyClass::MyMethod, 5.0,
254 //                               MyClass::MyOtherMethod);
255 //   }
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)
259 #else
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.")
264 #endif
265
266 #endif