]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
* src/*.cxx : first parss to normalize file organisation
[gdcm.git] / src / gdcmDebug.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.h,v $
5   Language:  C++
6   Date:      $Date: 2005/02/01 10:29:54 $
7   Version:   $Revision: 1.27 $
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 beginning of code:
43  *         gdcm::Debug::SetDebugOn();
44  */
45 class GDCM_EXPORT Debug
46 {
47 public:
48    Debug();
49    ~Debug();
50
51    /// This is a global flag that controls whether any debug, warning
52    /// messages are displayed.
53    static void SetDebugFlag (bool flag);
54    static bool GetDebugFlag ();
55    static void DebugOn  () { SetDebugFlag(true);  };
56    static void DebugOff () { SetDebugFlag(false); };
57
58    /// This is a global flag that controls if debug are redirected
59    /// to a file or not
60    static void SetDebugToFile (bool flag);
61    static bool GetDebugToFile ();
62    static void DebugToFileOn  () { SetDebugToFile(true);  };
63    static void DebugToFileOff () { SetDebugToFile(false); };
64
65    /// Set the filename the debug stream should be redirect to
66    /// Settting a filename also set DebugToFile to true
67    static void SetDebugFilename (std::string const& filename);
68
69    /// Internal use only. Allow us to retrieve the static from anywhere
70    /// in gdcm code
71    static std::ofstream &GetDebugFile ();
72 };
73
74 } // end namespace gdcm
75
76 // Here we define function this is the only way to be able to pass
77 // stuff with indirection like:
78 // gdcmDebug( "my message:" << i << '\t' ); 
79 // You cannot use function unless you use vnsprintf ...
80
81 // __FUNCTION is not always defined by preprocessor
82 // In c++ we should use __PRETTY_FUNCTION__ instead...
83 #ifdef GDCM_COMPILER_HAS_FUNCTION
84 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
85 // which is a lot nice in C++
86 #ifdef __BORLANDC__
87 #  define __FUNCTION__ __FUNC__
88 #endif
89 #ifdef __GNUC__
90 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
91 #else
92 #  define GDCM_FUNCTION __FUNCTION__ 
93 #endif //__GNUC__
94 #else
95 #  define GDCM_FUNCTION "<unknow>"
96 #endif //GDCM_COMPILER_HAS_FUNCTION
97
98 /**
99  * \brief   Debug
100  * @param msg message part
101  */
102 #ifdef NDEBUG
103 #define gdcmDebugMacro(msg)
104 #else
105 #define gdcmDebugMacro(msg)                                 \
106 {                                                           \
107    if( Debug::GetDebugFlag() )                              \
108    {                                                        \
109    std::ostringstream osmacro;                              \
110    osmacro << "Debug: In " __FILE__ ", line " << __LINE__   \
111            << ", function " << GDCM_FUNCTION << '\n'        \
112            << "Last system error was: " << strerror(errno)  \
113            << '\n' << msg << "\n\n";                        \
114    if( Debug::GetDebugToFile() )                            \
115       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
116    else                                                     \
117       std::cerr << osmacro.str() << std::endl;              \
118    }                                                        \
119 }
120 #endif //NDEBUG
121
122 /**
123  * \brief   Verbose 
124  * @param msg message part
125  */
126 #ifdef NDEBUG
127 #define gdcmVerboseMacro(msg)                               \
128 {                                                           \
129 }
130 #else
131 #define gdcmVerboseMacro(msg)                               \
132 {                                                           \
133    if( Debug::GetDebugFlag() )                              \
134    {                                                        \
135    std::ostringstream osmacro;                              \
136    osmacro << "Verbose: 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 #endif