]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
ENH: Get rid of errno is op is success
[gdcm.git] / src / gdcmDebug.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.h,v $
5   Language:  C++
6   Date:      $Date: 2005/10/26 14:47:06 $
7   Version:   $Revision: 1.44 $
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 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); }
62
63    /// \brief This is a global flag that controls if debug are redirected
64    ///        to a file or not
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); }
71
72    static void SetDebugFilename (std::string const &filename);
73
74    static std::ofstream &GetDebugFile ();
75 };
76
77 } // end namespace gdcm
78
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 ...
83
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++
89 #ifdef __BORLANDC__
90 #  define __FUNCTION__ __FUNC__
91 #endif
92 #ifdef __GNUC__
93 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
94 #else
95 #  define GDCM_FUNCTION __FUNCTION__ 
96 #endif //__GNUC__
97 #else
98 #  define GDCM_FUNCTION "<unknow>"
99 #endif //GDCM_COMPILER_HAS_FUNCTION
100
101 /**
102  * \brief   Debug
103  * @param msg message part
104  */
105 #ifdef NDEBUG
106 #define gdcmDebugMacro(msg) {}
107 #else
108 #define gdcmDebugMacro(msg)                                 \
109 {                                                           \
110    if( Debug::GetDebugFlag() )                              \
111    {                                                        \
112    std::ostringstream osmacro;                              \
113    osmacro << "Debug: In " __FILE__ ", line " << __LINE__   \
114            << ", function " << GDCM_FUNCTION << '\n';       \
115    if( errno )                                              \
116      osmacro  << "Last system error was: " <<               \
117        strerror(errno) << '\n' << msg << "\n\n";            \
118    if( Debug::GetDebugToFile() )                            \
119       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
120    else                                                     \
121       std::cerr << osmacro.str() << std::endl;              \
122    }                                                        \
123 }
124 #endif //NDEBUG
125
126 /**
127  * \brief   Warning
128  * @param msg message part
129  */
130 #ifdef NDEBUG
131 #define gdcmWarningMacro(msg) {}
132 #else
133 #define gdcmWarningMacro(msg)                               \
134 {                                                           \
135    if( Debug::GetDebugFlag() )                              \
136    {                                                        \
137    std::ostringstream osmacro;                              \
138    osmacro << "Warning: In " __FILE__ ", line " << __LINE__ \
139            << ", function " << GDCM_FUNCTION << "\n"        \
140            << msg << "\n\n";                                \
141    if( Debug::GetDebugToFile() )                            \
142       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
143    else                                                     \
144       std::cerr << osmacro.str() << std::endl;              \
145    }                                                        \
146 }
147 #endif //NDEBUG
148
149 /**
150  * \brief   Error 
151  * @param msg second message part 
152  */
153 #ifdef NDEBUG
154 #define gdcmErrorMacro(msg) {}
155 #else
156 #define gdcmErrorMacro(msg)                                 \
157 {                                                           \
158    std::ostringstream osmacro;                              \
159    osmacro << "Error: In " __FILE__ ", line " << __LINE__   \
160            << ", function " << GDCM_FUNCTION << '\n'        \
161            << msg << "\n\n";                                \
162    if( Debug::GetDebugToFile() )                            \
163       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
164    else                                                     \
165       std::cerr << osmacro.str() << std::endl;              \
166 }
167 #endif //NDEBUG
168
169 /**
170  * \brief   Assert 
171  * @param arg argument to test
172  *        An easy solution to pass also a message is to do:
173  *        gdcmAssertMacro( "my message" && 2 < 3 )
174  */
175 #ifdef NDEBUG
176 #define gdcmAssertMacro(arg) {}
177 #else
178 #define gdcmAssertMacro(arg)                                \
179 {                                                           \
180    if( !(arg) )                                             \
181    {                                                        \
182    std::ostringstream osmacro;                              \
183    osmacro << "Assert: In " __FILE__ ", line " << __LINE__  \
184            << ", function " << GDCM_FUNCTION                \
185            << "\n\n";                                       \
186    if( Debug::GetDebugToFile() )                            \
187       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
188    else                                                     \
189       std::cerr << osmacro.str() << std::endl;              \
190    assert ( arg );                                          \
191    }                                                        \
192 }
193 #endif //NDEBUG
194
195 //-----------------------------------------------------------------------------
196 //
197 // Define GDCM_LEGACY macro to mark legacy methods where they are
198 // declared in their class.
199 // 
200 // WARNING : Don't try to use it with 'inline' methods ! 
201 //
202 //Example usage:
203 //
204 //   // @deprecated Replaced by MyOtherMethod() as of gdcm 2.0.
205 //   GDCM_LEGACY(void MyMethod());
206 #if defined(GDCM_LEGACY_REMOVE)
207   // Remove legacy methods completely.
208 # define GDCM_LEGACY(method)
209 #elif defined(GDCM_LEGACY_SILENT) || defined(SWIG)
210   // Provide legacy methods with no warnings.
211 # define GDCM_LEGACY(method) method
212 #else
213   // Setup compile-time warnings for uses of deprecated methods if
214   // possible on this compiler.
215 # if defined(__GNUC__) && !defined(__INTEL_COMPILER) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
216 #if defined(__APPLE__) && (__GNUC__ == 3) && (__GNUC_MINOR__ == 3)
217 // Seems like there is a bug in APPLE gcc for deprecated attribute and ctor
218 // This is fixed in g++ 4.0 (Tiger)
219 #  define GDCM_LEGACY(method) method
220 #else
221 #  define GDCM_LEGACY(method) method __attribute__((deprecated))
222 #endif
223 # elif defined(_MSC_VER) && _MSC_VER >= 1300
224 #  define GDCM_LEGACY(method) __declspec(deprecated) method
225 # else
226 #  define GDCM_LEGACY(method) method
227 # endif
228 #endif
229
230 // Macros to create runtime deprecation warning messages in function
231 // bodies.  Example usage:
232 //
233 //   void MyClass::MyOldMethod()
234 //   {
235 //     GDCM_LEGACY_BODY(MyClass::MyOldMethod, 2.0);
236 //   }
237 //
238 //   void MyClass::MyMethod()
239 //   {
240 //     GDCM_LEGACY_REPLACED_BODY(MyClass::MyMethod, 5.0,
241 //                               MyClass::MyOtherMethod);
242 //   }
243 #if defined(GDCM_LEGACY_REMOVE) || defined(GDCM_LEGACY_SILENT)
244 # define GDCM_LEGACY_BODY(method, version)
245 # define GDCM_LEGACY_REPLACED_BODY(method, version, replace)
246 #else
247 # define GDCM_LEGACY_BODY(method, version) \
248   gdcmWarningMacro(#method " was deprecated for gdcm" #version " and will be removed in a future version.")
249 # define GDCM_LEGACY_REPLACED_BODY(method, version, replace) \
250   gdcmWarningMacro(#method " was deprecated for gdcm" #version " and will be removed in a future version.  Use " #replace " instead.")
251 #endif
252
253 #endif