]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
* Fix bug -- sorry
[gdcm.git] / src / gdcmDebug.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.h,v $
5   Language:  C++
6   Date:      $Date: 2005/11/28 15:30:03 $
7   Version:   $Revision: 1.49 $
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 #include "gdcmCommand.h"
24
25 #include <iostream>
26 #include <sstream>
27 #include <fstream>
28 #include <assert.h>
29 #include <errno.h>
30
31 namespace gdcm 
32 {
33 //-----------------------------------------------------------------------------
34 class CommandManager;
35
36 //-----------------------------------------------------------------------------
37 /**
38  * \brief Debug is an object for debugging in program.
39  * It has 2 debugging modes :
40  *  - error : for bad library use, seriously wrong DICOM
41  *  - debug : for information/debug messages
42  *  - warning : for warning about DICOM quality (kosher)
43  *  - assert : design by contract implementation. A function should have 
44  *             proper input and proper output. 
45  *             (should not happen, not user controlled)
46  * 
47  * A debugging message is only shown if the flag is on (DebugFlag)
48  * This is static var and can be set at beginning of code:
49  *         gdcm::Debug::SetDebugOn();
50  */
51 class GDCM_EXPORT Debug
52 {
53 public:
54    Debug();
55    ~Debug();
56
57    /// \brief This is a global flag that controls whether 
58    ///        both debug and warning messages are displayed. 
59    ///        (used to warn user when file contains some oddity)
60    static void SetDebugFlag (bool flag);
61    /// \brief   Gets the debug flag value
62    static bool GetDebugFlag () {return DebugFlag;}
63    /// \brief Sets the Debug Flag to true
64    static void DebugOn  () { SetDebugFlag(true);  }
65    /// \brief Sets the Debug Flag to false
66    static void DebugOff () { SetDebugFlag(false); }
67    
68    /// \brief This is a global flag that controls whether 
69    ///        warning messages are displayed.
70    static void SetWarningFlag (bool flag);
71    /// \brief   Gets the warning flag value
72    static bool GetWarningFlag () {return WarningFlag;}
73    /// \brief Sets the Warning Flag to true
74    static void WarningOn  () { SetWarningFlag(true);  }
75    /// \brief Sets the Warning Flag to false
76    static void WarningOff () { SetWarningFlag(false); }   
77
78    /// \brief This is a global flag that controls if debug are redirected
79    ///        to a file or not
80    static void SetOutputToFile (bool flag);
81    static bool GetOutputToFile ();
82    /// \brief Next debug messages will be sent in the debug file
83    static void OutputToFileOn  () { SetOutputToFile(true);  }
84    /// \brief Next debug messages will be sent in the standard output
85    static void OutputToFileOff () { SetOutputToFile(false); }
86
87    static void SetOutputFileName (std::string const &filename);
88
89    static std::ostream &GetOutput ();
90
91    static void SendToOutput(unsigned int type,std::string const &msg,const CommandManager *mgr = NULL);
92
93 private:
94    static bool DebugFlag;
95    static bool WarningFlag;
96    static bool OutputToFile;
97
98    static std::ofstream OutputFileStream;
99    static std::ostream &StandardStream;
100
101    static const int LINE_LENGTH;
102 };
103
104 } // end namespace gdcm
105
106 // Here we define function this is the only way to be able to pass
107 // stuff with indirection like:
108 // gdcmDebug( "my message:" << i << '\t' ); 
109 // You cannot use function unless you use vnsprintf ...
110
111 // __FUNCTION is not always defined by preprocessor
112 // In c++ we should use __PRETTY_FUNCTION__ instead...
113 #ifdef GDCM_COMPILER_HAS_FUNCTION
114 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
115 // which is a lot nice in C++
116 #ifdef __BORLANDC__
117 #  define __FUNCTION__ __FUNC__
118 #endif
119 #ifdef __GNUC__
120 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
121 #else
122 #  define GDCM_FUNCTION __FUNCTION__ 
123 #endif //__GNUC__
124 #else
125 #  define GDCM_FUNCTION "<unknow>"
126 #endif //GDCM_COMPILER_HAS_FUNCTION
127
128 /**
129  * \brief   Debug : To be used to help bug tracking developer
130  * @param msg message part
131  */
132 #define gdcmMessageBodyMacro(type,obj,msg,adds)                \
133 {                                                              \
134    std::ostringstream osmacro;                                 \
135    osmacro << "In " __FILE__ ", line " << __LINE__             \
136            << ", function " << GDCM_FUNCTION << "\n"           \
137            << adds << msg << "\n\n";                           \
138    gdcm::Debug::SendToOutput(type,osmacro.str(),obj);          \
139 }
140
141 /**
142  * \brief Debug : To be used to help bug tracking developer
143  * @param msg message part
144  */
145 #ifdef NDEBUG
146 #define gdcmDebugBodyMacro(obj,msg) {}
147 #define gdcmDebugMacro(msg) {}
148 #define gdcmStaticDebugMacro(msg) {}
149 #else
150 #define gdcmDebugBodyMacro(obj,msg)                            \
151 {                                                              \
152    if( Debug::GetDebugFlag() )                                 \
153    {                                                           \
154       std::string adds="";                                     \
155       if( errno )                                              \
156       {                                                        \
157          adds = "Last system error was: ";                     \
158          adds += strerror(errno);                              \
159          adds += "\n";                                         \
160       }                                                        \
161       gdcmMessageBodyMacro(gdcm::CMD_DEBUG,obj,msg,adds);      \
162    }                                                           \
163 }
164 #define gdcmDebugMacro(msg)                                    \
165    gdcmDebugBodyMacro(this,msg)
166 #define gdcmStaticDebugMacro(msg)                              \
167    gdcmDebugBodyMacro(NULL,msg)
168 #endif //NDEBUG
169
170 /**
171  * \brief Warning : To be used to warn the user when some oddity occurs
172  * @param msg message part
173  */
174 // No NDEBUG test to always have a return of warnings !!!
175 #define gdcmWarningBodyMacro(obj,msg)                          \
176 {                                                              \
177    if( Debug::GetWarningFlag() )                               \
178       gdcmMessageBodyMacro(gdcm::CMD_WARNING,obj,msg,"");      \
179 }
180 #define gdcmWarningMacro(msg)                                  \
181    gdcmWarningBodyMacro(this,msg)
182 #define gdcmStaticWarningMacro(msg)                            \
183    gdcmWarningBodyMacro(NULL,msg)
184
185 /**
186  * \brief   Error : To be used when unecoverabale error occurs
187  *          at a 'deep' level. (don't use it if file is not ACR/DICOM!)
188  * @param msg second message part 
189  */
190 // No NDEBUG test to always have a return of errors !!!
191 #define gdcmErrorBodyMacro(obj,msg)                            \
192 {                                                              \
193    gdcmMessageBodyMacro(gdcm::CMD_ERROR,obj,msg,"");           \
194 }
195 #define gdcmErrorMacro(msg)                                    \
196    gdcmErrorBodyMacro(this,msg)
197 #define gdcmStaticErrorMacro(msg)                              \
198    gdcmErrorBodyMacro(NULL,msg)
199
200 /**
201  * \brief Assert : To be used when an *absolutely* impossible error occurs
202  *        No function should be allowed to stop the process instead of
203  *        warning the caller!
204  * @param arg argument to test
205  *        An easy solution to pass also a message is to do:
206  *        gdcmAssertMacro( "my message" && 2 < 3 )
207  */
208 // No NDEBUG test to always have a return of asserts !!!
209 #define gdcmAssertBodyMacro(obj,arg)                           \
210 {                                                              \
211    if( !(arg) )                                                \
212    {                                                           \
213       gdcmMessageBodyMacro(gdcm::CMD_ASSERT,obj,"","");        \
214       assert ( arg );                                          \
215    }                                                           \
216 }
217 #define gdcmAssertMacro(msg)                                   \
218    gdcmAssertBodyMacro(NULL,msg)
219 #define gdcmStaticAssertMacro(msg)                             \
220    gdcmAssertBodyMacro(NULL,msg)
221
222 //-----------------------------------------------------------------------------
223 #endif