]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
ENH: Adding a deprecation mechanism to gdcm via two MACRO and two config options
[gdcm.git] / src / gdcmDebug.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.h,v $
5   Language:  C++
6   Date:      $Date: 2005/07/19 14:43:48 $
7   Version:   $Revision: 1.36 $
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 <sstream>
25 #include <fstream>
26 #include <assert.h>
27 #include <errno.h>
28
29 namespace gdcm 
30 {
31 //-----------------------------------------------------------------------------
32
33 /**
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)
42  * 
43  * A debugging message is only show if the flag is on (DebugFlag)
44  * This is static var and can be set at beginning of code:
45  *         gdcm::Debug::SetDebugOn();
46  */
47 class GDCM_EXPORT Debug
48 {
49 public:
50    Debug();
51    ~Debug();
52
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); };
61
62    /// \brief This is a global flag that controls if debug are redirected
63    ///        to a file or not
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); };
70
71    static void SetDebugFilename (std::string const &filename);
72
73    static std::ofstream &GetDebugFile ();
74 };
75
76 } // end namespace gdcm
77
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 ...
82
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++
88 #ifdef __BORLANDC__
89 #  define __FUNCTION__ __FUNC__
90 #endif
91 #ifdef __GNUC__
92 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
93 #else
94 #  define GDCM_FUNCTION __FUNCTION__ 
95 #endif //__GNUC__
96 #else
97 #  define GDCM_FUNCTION "<unknow>"
98 #endif //GDCM_COMPILER_HAS_FUNCTION
99
100 /**
101  * \brief   Debug
102  * @param msg message part
103  */
104 #ifdef NDEBUG
105 #define gdcmDebugMacro(msg) {}
106 #else
107 #define gdcmDebugMacro(msg)                                 \
108 {                                                           \
109    if( Debug::GetDebugFlag() )                              \
110    {                                                        \
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;  \
118    else                                                     \
119       std::cerr << osmacro.str() << std::endl;              \
120    }                                                        \
121 }
122 #endif //NDEBUG
123
124 /**
125  * \brief   Warning
126  * @param msg message part
127  */
128 #ifdef NDEBUG
129 #define gdcmWarningMacro(msg) {}
130 #else
131 #define gdcmWarningMacro(msg)                               \
132 {                                                           \
133    if( Debug::GetDebugFlag() )                              \
134    {                                                        \
135    std::ostringstream osmacro;                              \
136    osmacro << "Warning: In " __FILE__ ", line " << __LINE__ \
137            << ", function " << GDCM_FUNCTION << "\n"        \
138            << msg << "\n\n";                                \
139    if( Debug::GetDebugToFile() )                            \
140       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
141    else                                                     \
142       std::cerr << osmacro.str() << std::endl;              \
143    }                                                        \
144 }
145 #endif //NDEBUG
146
147 /**
148  * \brief   Error 
149  * @param msg second message part 
150  */
151 #ifdef NDEBUG
152 #define gdcmErrorMacro(msg) {}
153 #else
154 #define gdcmErrorMacro(msg)                                 \
155 {                                                           \
156    std::ostringstream osmacro;                              \
157    osmacro << "Error: In " __FILE__ ", line " << __LINE__   \
158            << ", function " << GDCM_FUNCTION << '\n'        \
159            << msg << "\n\n";                                \
160    if( Debug::GetDebugToFile() )                            \
161       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
162    else                                                     \
163       std::cerr << osmacro.str() << std::endl;              \
164 }
165 #endif //NDEBUG
166
167 /**
168  * \brief   Assert 
169  * @param arg argument to test
170  *        An easy solution to pass also a message is to do:
171  *        gdcmAssertMacro( "my message" && 2 < 3 )
172  */
173 #ifdef NDEBUG
174 #define gdcmAssertMacro(arg) {}
175 #else
176 #define gdcmAssertMacro(arg)                                \
177 {                                                           \
178    if( !(arg) )                                             \
179    {                                                        \
180    std::ostringstream osmacro;                              \
181    osmacro << "Assert: In " __FILE__ ", line " << __LINE__  \
182            << ", function " << GDCM_FUNCTION                \
183            << "\n\n";                                       \
184    if( Debug::GetDebugToFile() )                            \
185       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
186    else                                                     \
187       std::cerr << osmacro.str() << std::endl;              \
188    assert ( arg );                                          \
189    }                                                        \
190 }
191 #endif //NDEBUG
192
193 //-----------------------------------------------------------------------------
194 //
195 // Define GDCM_LEGACY macro to mark legacy methods where they are
196 // declared in their class.  Example usage:
197 //
198 //   // @deprecated Replaced by MyOtherMethod() as of gdcm 2.0.
199 //   GDCM_LEGACY(void MyMethod());
200 #if defined(GDCM_LEGACY_REMOVE)
201   // Remove legacy methods completely.
202 # define GDCM_LEGACY(method)
203 #elif defined(GDCM_LEGACY_SILENT)
204   // Provide legacy methods with no warnings.
205 # define GDCM_LEGACY(method) method
206 #else
207   // Setup compile-time warnings for uses of deprecated methods if
208   // possible on this compiler.
209 # if defined(__GNUC__) && !defined(__INTEL_COMPILER) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
210 #  define GDCM_LEGACY(method) method __attribute__((deprecated))
211 # elif defined(_MSC_VER) && _MSC_VER >= 1300
212 #  define GDCM_LEGACY(method) __declspec(deprecated) method
213 # else
214 #  define GDCM_LEGACY(method) method
215 # endif
216 #endif
217
218 // Macros to create runtime deprecation warning messages in function
219 // bodies.  Example usage:
220 //
221 //   void MyClass::MyOldMethod()
222 //   {
223 //     GDCM_LEGACY_BODY(MyClass::MyOldMethod, 2.0);
224 //   }
225 //
226 //   void MyClass::MyMethod()
227 //   {
228 //     GDCM_LEGACY_REPLACED_BODY(MyClass::MyMethod, 5.0,
229 //                               MyClass::MyOtherMethod);
230 //   }
231 #if defined(GDCM_LEGACY_REMOVE) || defined(GDCM_LEGACY_SILENT)
232 # define GDCM_LEGACY_BODY(method, version)
233 # define GDCM_LEGACY_REPLACED_BODY(method, version, replace)
234 #else
235 # define GDCM_LEGACY_BODY(method, version) \
236   gdcmWarningMacro(#method " was deprecated for gdcm" #version " and will be removed in a future version.")
237 # define GDCM_LEGACY_REPLACED_BODY(method, version, replace) \
238   gdcmWarningMacro(#method " was deprecated for gdcm" #version " and will be removed in a future version.  Use " #replace " instead.")
239 #endif
240
241 #endif