]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
BUG: Apparently on Borland uses unsigned char to store boolean, all other plateformns...
[gdcm.git] / src / gdcmDebug.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.h,v $
5   Language:  C++
6   Date:      $Date: 2005/02/05 01:37:08 $
7   Version:   $Revision: 1.30 $
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  * 
40  * A debugging message is only show if the flag is on (DebugFlag)
41  * This is static var and can be set at beginning of code:
42  *         gdcm::Debug::SetDebugOn();
43  */
44 class GDCM_EXPORT Debug
45 {
46 public:
47    Debug();
48    ~Debug();
49
50    /// This is a global flag that controls whether any debug, warning
51    /// messages are displayed.
52    static void SetDebugFlag (bool flag);
53    static bool GetDebugFlag ();
54    /// \brief Sets the Debug Flag to true
55    static void DebugOn  () { SetDebugFlag(true);  };
56    /// \brief Sets the Debug Flag to false
57    static void DebugOff () { SetDebugFlag(false); };
58
59    /// This is a global flag that controls if debug are redirected
60    /// to a file or not
61    static void SetDebugToFile (bool flag);
62    static bool GetDebugToFile ();
63    /// \brief Next debug messages will be sent in the debug file
64    static void DebugToFileOn  () { SetDebugToFile(true);  };
65    /// \brief Next debug messages will be sent in the standard output
66    static void DebugToFileOff () { SetDebugToFile(false); };
67
68    static void SetDebugFilename (std::string const &filename);
69
70    static std::ofstream &GetDebugFile ();
71 };
72
73 } // end namespace gdcm
74
75 // Here we define function this is the only way to be able to pass
76 // stuff with indirection like:
77 // gdcmDebug( "my message:" << i << '\t' ); 
78 // You cannot use function unless you use vnsprintf ...
79
80 // __FUNCTION is not always defined by preprocessor
81 // In c++ we should use __PRETTY_FUNCTION__ instead...
82 #ifdef GDCM_COMPILER_HAS_FUNCTION
83 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
84 // which is a lot nice in C++
85 #ifdef __BORLANDC__
86 #  define __FUNCTION__ __FUNC__
87 #endif
88 #ifdef __GNUC__
89 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
90 #else
91 #  define GDCM_FUNCTION __FUNCTION__ 
92 #endif //__GNUC__
93 #else
94 #  define GDCM_FUNCTION "<unknow>"
95 #endif //GDCM_COMPILER_HAS_FUNCTION
96
97 /**
98  * \brief   Debug
99  * @param msg message part
100  */
101 #ifdef NDEBUG
102 #define gdcmDebugMacro(msg)
103 #else
104 #define gdcmDebugMacro(msg)                                 \
105 {                                                           \
106    if( Debug::GetDebugFlag() )                              \
107    {                                                        \
108    std::ostringstream osmacro;                              \
109    osmacro << "Debug: In " __FILE__ ", line " << __LINE__   \
110            << ", function " << GDCM_FUNCTION << '\n'        \
111            << "Last system error was: " << strerror(errno)  \
112            << '\n' << msg << "\n\n";                        \
113    if( Debug::GetDebugToFile() )                            \
114       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
115    else                                                     \
116       std::cerr << osmacro.str() << std::endl;              \
117    }                                                        \
118 }
119 #endif //NDEBUG
120
121 /**
122  * \brief   Verbose 
123  * @param msg message part
124  */
125 #ifdef NDEBUG
126 #define gdcmWarningMacro(msg)                               \
127 {                                                           \
128 }
129 #else
130 #define gdcmWarningMacro(msg)                               \
131 {                                                           \
132    if( Debug::GetDebugFlag() )                              \
133    {                                                        \
134    std::ostringstream osmacro;                              \
135    osmacro << "Verbose: In " __FILE__ ", line " << __LINE__ \
136            << ", function " << GDCM_FUNCTION << "\n"        \
137            << msg << "\n\n";                                \
138    if( Debug::GetDebugToFile() )                            \
139       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
140    else                                                     \
141       std::cerr << osmacro.str() << std::endl;              \
142    }                                                        \
143 }
144 #endif //NDEBUG
145
146 /**
147  * \brief   Error 
148  * @param msg second message part 
149  */
150 #ifdef NDEBUG
151 #define gdcmErrorMacro(msg)
152 #else
153 #define gdcmErrorMacro(msg)                                 \
154 {                                                           \
155    std::ostringstream osmacro;                              \
156    osmacro << "Error: In " __FILE__ ", line " << __LINE__   \
157            << ", function " << GDCM_FUNCTION << '\n'        \
158            << msg << "\n\n";                                \
159    if( Debug::GetDebugToFile() )                            \
160       Debug::GetDebugFile() << osmacro.str() << std::endl;  \
161    else                                                     \
162       std::cerr << osmacro.str() << std::endl;              \
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