]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
c135290d7f4bb347717b4b30e7f3b6b7abf3fd0d
[gdcm.git] / src / gdcmDebug.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.h,v $
5   Language:  C++
6   Date:      $Date: 2005/01/13 22:39:15 $
7   Version:   $Revision: 1.23 $
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  * \ingroup Debug
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  * 
41  * A debugging message is only show if the flag is on (DebugFlag)
42  * This is static var and can be set at begining of code:
43  *         gdcm::Debug::SetDebugOn();
44  */
45 class GDCM_EXPORT Debug
46 {
47 public:
48    /// This is a global flag that controls whether any debug, warning
49    /// messages are displayed.
50    static bool GetDebugFlag ();
51    static void SetDebugFlag (bool flag);
52    static void SetDebugOn  () { SetDebugFlag(true); };
53    static void SetDebugOff () { SetDebugFlag(false); };
54
55    /// This is a global flag that controls if debug are redirected
56    /// to a file or not
57    static bool GetDebugToFile ();
58    static void SetDebugToFile (bool flag);
59    static void SetDebugToFileOn  () { SetDebugToFile(true); };
60    static void SetDebugToFileOff () { SetDebugToFile(false); };
61
62    /// Set the filename the debug stream should be redirect to
63    /// Settting a filename also set DebugToFile to true
64    static void SetDebugFilename (std::string const& filename);
65
66    /// Internal use only. Allow us to retrieve the static from anywhere
67    /// in gdcm code
68    static std::ofstream & GetDebugFile ();
69 };
70
71 } // end namespace gdcm
72
73 // Here we define function this is the only way to be able to pass
74 // stuff with indirection like:
75 // gdcmDebug( "my message:" << i << '\t' ); 
76 // You cannot use function unless you use vnsprintf ...
77
78 // __FUNCTION is not always defined by preprocessor
79 // In c++ we should use __PRETTY_FUNCTION__ instead...
80 #ifdef GDCM_COMPILER_HAS_FUNCTION
81 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
82 // which is a lot nice in C++
83 #ifdef __BORLANDC__
84 #  define __FUNCTION__ __FUNC__
85 #endif
86 #ifdef __GNUC__
87 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
88 #else
89 #  define GDCM_FUNCTION __FUNCTION__ 
90 #endif //__GNUC__
91 #else
92 #  define GDCM_FUNCTION "<unknow>"
93 #endif //GDCM_COMPILER_HAS_FUNCTION
94
95 /**
96  * \brief   Debug
97  * @param msg message part
98  */
99 #ifdef NDEBUG
100 #define gdcmDebugMacro(msg)
101 #else
102 #define gdcmDebugMacro(msg)                                 \
103 {                                                           \
104    if( Debug::GetDebugFlag() )                              \
105    {                                                        \
106    std::ostringstream osmacro;                              \
107    osmacro << "Debug: In " __FILE__ ", line " << __LINE__   \
108            << ", function " << GDCM_FUNCTION << '\n'        \
109            << "Last system error was: " << strerror(errno)  \
110            << '\n' << msg << "\n\n";                        \
111    if( Debug::GetDebugToFile() )                            \
112       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
113    else                                                     \
114       std::cerr << osmacro.str() << std::endl;              \
115    }                                                        \
116 }
117 #endif //NDEBUG
118
119 /**
120  * \brief   Verbose 
121  * @param msg message part
122  */
123 #ifdef NDEBUG
124 #define gdcmVerboseMacro(msg)
125 #else
126 #define gdcmVerboseMacro(msg)                               \
127 {                                                           \
128    if( Debug::GetDebugFlag() )                              \
129    {                                                        \
130    std::ostringstream osmacro;                              \
131    osmacro << "Verbose: In " __FILE__ ", line " << __LINE__ \
132            << ", function " << GDCM_FUNCTION << "\n"        \
133            << msg << "\n\n";                                \
134    if( Debug::GetDebugToFile() )                            \
135       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
136    else                                                     \
137       std::cerr << osmacro.str() << std::endl;              \
138    }                                                        \
139 }
140 #endif //NDEBUG
141
142 /**
143  * \brief   Error 
144  * @param msg second message part 
145  */
146 #ifdef NDEBUG
147 #define gdcmErrorMacro(msg)
148 #else
149 #define gdcmErrorMacro(msg)                                 \
150 {                                                           \
151    if( Debug::GetDebugFlag() )                              \
152    {                                                        \
153    std::ostringstream osmacro;                              \
154    osmacro << "Error: In " __FILE__ ", line " << __LINE__   \
155            << ", function " << GDCM_FUNCTION << '\n'        \
156            << msg << "\n\n";                                \
157    if( Debug::GetDebugToFile() )                            \
158       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
159    else                                                     \
160       std::cerr << osmacro.str() << std::endl;              \
161    exit(1);                                                 \
162    }                                                        \
163 }
164 #endif //NDEBUG
165
166 /**
167  * \brief   Assert 
168  * @param arg argument to test
169  *        An easy solution to pass also a message is to do:
170  *        gdcmAssertMacro( "my message" && 2 < 3 )
171  */
172 #ifdef NDEBUG
173 #define gdcmAssertMacro(arg)
174 #else
175 #define gdcmAssertMacro(arg)                                \
176 {                                                           \
177    if( !(arg) )                                             \
178    {                                                        \
179    std::ostringstream osmacro;                              \
180    osmacro << "Assert: In " __FILE__ ", line " << __LINE__  \
181            << ", function " << GDCM_FUNCTION                \
182            << "\n\n";                                       \
183    if( Debug::GetDebugToFile() )                            \
184       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
185    else                                                     \
186       std::cerr << osmacro.str() << std::endl;              \
187    assert ( arg );                                          \
188    }                                                        \
189 }
190 #endif //NDEBUG
191
192 #endif