]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
BUG: map<>::mapped_type is not part of the STL. Should fix issue on VS* compiler
[gdcm.git] / src / gdcmDebug.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.h,v $
5   Language:  C++
6   Date:      $Date: 2007/08/22 16:14:03 $
7   Version:   $Revision: 1.58 $
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_NAME_SPACE
32 {
33 //-----------------------------------------------------------------------------
34 class CommandManager;
35
36 //-----------------------------------------------------------------------------
37 /**
38  * \brief Debug is an object for warning/logging/tracing programs.
39  * It has the following modes :
40  *  - error : for bad library use, seriously wrong DICOM
41  *  - debug : for information/debug messages
42  *  - warning : Warns the user when some oddity occured.
43  *  - log     : we display messages when anything is not Dicom Kosher
44  *  - debug : we help developper to trace, at a glance, the execution
45  *  - assert : design by contract implementation. A function should have 
46  *             proper input and proper output. 
47  *             (should not happen, not user controlled)
48  * 
49  * A debugging message is only shown if the flag is on (DebugFlag)
50  * This is static var and can be set at beginning of code:
51  *         GDCM_NAME_SPACE::Debug::SetDebugOn();
52  *
53  * Warning  : Warns the user when some oddity occured, and we used an heuristics
54  *            to bypass the trouble.
55  *            e.g.  : SamplesPerPixel missing, we assume it's a grey level image
56  *            e.g   : Pixel Representation missing : we assume 'unsigned'
57  *            (we hope our assumption is OK, but user may disagree.)
58  * Log      : we display messages when anything is not Dicom Kosher 
59  *            e.g. : non even length field
60  *            e.g  : file is declared as Explicit VR, but a DataElement
61  *                 is Implicit
62  *            e.g  : a file holds an illegal group (0x0005, ...)    
63  * Debug : We help developper to trace, at a glance, the execution.
64  *         (before refining with a debugging tool)
65  *
66  * Setting ON Debug leads to set ON Warning (but not Log)
67  * Setting ON Log   leads to set ON Warning (but not Debug)
68  */
69
70 class GDCM_EXPORT Debug
71 {
72 public:
73    Debug();
74    ~Debug();
75
76    /// \brief This is a global flag that controls whether 
77    ///        both debug and warning messages are displayed. 
78    ///        (used to warn user when file contains some oddity)
79    static void SetDebugFlag (bool flag);
80    /// \brief   Gets the debug flag value
81    static bool GetDebugFlag () {return DebugFlag; }
82    /// \brief Sets the Debug Flag to true
83    static void DebugOn  () { SetDebugFlag(true);  }
84    /// \brief Sets the Debug Flag to false
85    static void DebugOff () { SetDebugFlag(false); }
86    
87    /// \brief This is a global flag that controls whether 
88    ///        log messages are displayed.
89    static void SetLogFlag (bool flag);
90    /// \brief   Gets the Log flag value
91    static bool GetLogFlag () {return LogFlag; }
92    /// \brief Sets the Log Flag to true
93    static void LogOn  () { SetLogFlag(true);  }
94    /// \brief Sets the Log Flag to false
95    static void LogOff () { SetLogFlag(false); } 
96    
97    /// \brief This is a global flag that controls whether 
98    ///        warning messages are displayed.
99    static void SetWarningFlag (bool flag);
100    /// \brief   Gets the warning flag value
101    static bool GetWarningFlag () {return WarningFlag; }
102    /// \brief Sets the Warning Flag to true
103    static void WarningOn  () { SetWarningFlag(true);  }
104    /// \brief Sets the Warning Flag to false
105    static void WarningOff () { SetWarningFlag(false); }      
106
107    /// \brief This is a global flag that controls if debug are redirected
108    ///        to a file or not
109    static void SetOutputToFile (bool flag);
110    static bool GetOutputToFile ();
111    /// \brief Next debug messages will be sent in the debug file
112    static void OutputToFileOn  () { SetOutputToFile(true);  }
113    /// \brief Next debug messages will be sent in the standard output
114    static void OutputToFileOff () { SetOutputToFile(false); }
115
116    static void SetOutputFileName (std::string const &filename);
117
118    static std::ostream &GetOutput ();
119
120    static void SendToOutput(unsigned int type,std::string const &msg,
121                             const Base *object = NULL);
122
123 private:
124    static bool WarningFlag;
125    static bool LogFlag;
126    static bool DebugFlag;
127
128    static bool OutputToFile;
129
130    static std::ofstream OutputFileStream;
131    static std::ostream &StandardStream;
132
133    static const int LINE_LENGTH;
134 };
135
136 } // end namespace gdcm
137
138 // Here we define function this is the only way to be able to pass
139 // stuff with indirection like:
140 // gdcmDebug( "my message:" << i << '\t' ); 
141 // You cannot use function unless you use vnsprintf ...
142
143 // __FUNCTION is not always defined by preprocessor
144 // In c++ we should use __PRETTY_FUNCTION__ instead...
145 #ifdef GDCM_COMPILER_HAS_FUNCTION
146 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
147 // which is a lot nice in C++
148 #ifdef __BORLANDC__
149 #  define __FUNCTION__ __FUNC__
150 #endif
151 #ifdef __GNUC__
152 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
153 #else
154 #  define GDCM_FUNCTION __FUNCTION__ 
155 #endif //__GNUC__
156 #else
157 #  define GDCM_FUNCTION "<unknow>"
158 #endif //GDCM_COMPILER_HAS_FUNCTION
159
160 /**
161  * \brief   Debug : To be used to help bug tracking developer
162  * @param type type 
163  * @param obj obj
164  * @param msg message part 
165  * @param adds adds
166  */
167 #define gdcmMessageBodyMacro(type, obj, msg, adds)             \
168 {                                                              \
169    std::ostringstream osmacro;                                 \
170    osmacro << "In " __FILE__ ", line " << __LINE__             \
171            << ", function " << GDCM_FUNCTION << "\n"           \
172            << adds << msg << "\n\n";                           \
173    GDCM_NAME_SPACE::Debug::SendToOutput(type,osmacro.str(),obj);\
174 }
175
176 // ------------------------------------------------------------------------
177
178 /**
179  * \brief Debug : To be used to help bug tracking developer
180  * @param obj current instance or NULL
181  * @param msg message part
182  */
183 #ifdef NDEBUG
184 #define gdcmDebugBodyMacro(obj, msg) {}
185 #define gdcmDebugMacro(msg) {}
186 #define gdcmStaticDebugMacro(msg) {}
187 #else
188 #define gdcmDebugBodyMacro(obj, msg)                           \
189 {                                                              \
190    if( Debug::GetDebugFlag() )                                 \
191    {                                                           \
192       std::string adds="";                                     \
193       if( errno )                                              \
194       {                                                        \
195          adds = "Last system error was: ";                     \
196          adds += strerror(errno);                              \
197          adds += "\n";                                         \
198       }                                                        \
199       gdcmMessageBodyMacro(GDCM_NAME_SPACE::CMD_DEBUG,obj,msg,adds);\
200    }                                                           \
201 }
202 #define gdcmDebugMacro(msg)                                    \
203    gdcmDebugBodyMacro(NULL,msg)
204 #define gdcmStaticDebugMacro(msg)                              \
205    gdcmDebugBodyMacro(NULL,msg)
206 #endif //NDEBUG
207
208 // ------------------------------------------------------------------------
209
210 /**
211  * \brief Log : we display messages when anything is not Dicom Kosher
212  * @param obj current instance or NULL
213  * @param msg message part
214  */
215 // No NDEBUG test to always have a return of warnings !!!
216 // -> Rien compris! JPRx
217 #define gdcmLogBodyMacro(obj, msg)                         \
218 {                                                          \
219    if( Debug::GetLogFlag() )                               \
220       gdcmMessageBodyMacro(GDCM_NAME_SPACE::CMD_LOG,obj,msg,"");\
221 }
222 #define gdcmLogMacro(msg)                                  \
223    gdcmLogBodyMacro(this,msg)
224 #define gdcmStaticLogMacro(msg)                            \
225    gdcmLogBodyMacro(NULL,msg)
226    
227 // ------------------------------------------------------------------------
228
229 /**
230  * \brief Warning : To be used to warn the user when some oddity occurs
231  * @param obj current instance or NULL
232  * @param msg message part
233  */
234 // No NDEBUG test to always have a return of warnings !!!
235 // -> Rien compris! JPRx
236 #define gdcmWarningBodyMacro(obj, msg)                         \
237 {                                                              \
238    if( Debug::GetWarningFlag() )                               \
239       gdcmMessageBodyMacro(GDCM_NAME_SPACE::CMD_WARNING,obj,msg,"");\
240 }
241 #define gdcmWarningMacro(msg)                                  \
242    gdcmWarningBodyMacro(this,msg)
243 #define gdcmStaticWarningMacro(msg)                            \
244    gdcmWarningBodyMacro(NULL,msg)
245
246 // ------------------------------------------------------------------------
247
248 /**
249  * \brief   Error : To be used when unecoverabale error occurs
250  *          at a 'deep' level. (don't use it if file is not ACR/DICOM!)
251  * @param obj current instance or NULL 
252  * @param msg second message part 
253  */
254 // No NDEBUG test to always have a return of errors !!!
255 // -> Rien compris! JPRx
256 #define gdcmErrorBodyMacro(obj, msg)                           \
257 {                                                              \
258    gdcmMessageBodyMacro(GDCM_NAME_SPACE::CMD_ERROR,obj,msg,"");\
259 }
260 #define gdcmErrorMacro(msg)                                    \
261    gdcmErrorBodyMacro(this,msg)
262 #define gdcmStaticErrorMacro(msg)                              \
263    gdcmErrorBodyMacro(NULL,msg)
264
265 // ------------------------------------------------------------------------
266
267 /**
268  * \brief Assert : To be used when an *absolutely* impossible error occurs
269  *        No function should be allowed to stop the process instead of
270  *        warning the caller!
271  * @param obj current instance or NULL
272  * @param arg argument to test
273  *        An easy solution to pass also a message is to do:
274  *        gdcmAssertMacro( "my message" && 2 < 3 )
275  */
276 // No NDEBUG test to always have a return of asserts !!!
277 // -> Rien compris! JPRx
278 #define gdcmAssertBodyMacro(obj, arg)                          \
279 {                                                              \
280    if( !(arg) )                                                \
281    {                                                           \
282       gdcmMessageBodyMacro(GDCM_NAME_SPACE::CMD_ASSERT,obj,"","");\
283       assert ( arg );                                          \
284    }                                                           \
285 }
286 #define gdcmAssertMacro(msg)                                   \
287    gdcmAssertBodyMacro(NULL,msg)
288 #define gdcmStaticAssertMacro(msg)                             \
289    gdcmAssertBodyMacro(NULL,msg)
290
291 //-----------------------------------------------------------------------------
292 #endif