1 /*=========================================================================
4 Module: $RCSfile: gdcmDebug.h,v $
6 Date: $Date: 2006/01/03 14:28:53 $
7 Version: $Revision: 1.53 $
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.
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.
17 =========================================================================*/
22 #include "gdcmCommon.h"
23 #include "gdcmCommand.h"
33 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
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)
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::Debug::SetDebugOn();
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
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)
66 * Setting ON Debug leads to set ON Warning (but not Log)
67 * Setting ON Log leads to set ON Warning (but not Debug)
70 class GDCM_EXPORT Debug
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); }
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); }
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); }
107 /// \brief This is a global flag that controls if debug are redirected
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); }
116 static void SetOutputFileName (std::string const &filename);
118 static std::ostream &GetOutput ();
120 static void SendToOutput(unsigned int type,std::string const &msg,
121 const Base *object = NULL);
124 static bool WarningFlag;
126 static bool DebugFlag;
128 static bool OutputToFile;
130 static std::ofstream OutputFileStream;
131 static std::ostream &StandardStream;
133 static const int LINE_LENGTH;
136 } // end namespace gdcm
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 ...
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++
149 # define __FUNCTION__ __FUNC__
152 # define GDCM_FUNCTION __PRETTY_FUNCTION__
154 # define GDCM_FUNCTION __FUNCTION__
157 # define GDCM_FUNCTION "<unknow>"
158 #endif //GDCM_COMPILER_HAS_FUNCTION
161 * \brief Debug : To be used to help bug tracking developer
162 * @param msg message part
164 #define gdcmMessageBodyMacro(type, obj, msg, adds) \
166 std::ostringstream osmacro; \
167 osmacro << "In " __FILE__ ", line " << __LINE__ \
168 << ", function " << GDCM_FUNCTION << "\n" \
169 << adds << msg << "\n\n"; \
170 gdcm::Debug::SendToOutput(type,osmacro.str(),obj); \
173 // ------------------------------------------------------------------------
176 * \brief Debug : To be used to help bug tracking developer
177 * @param msg message part
180 #define gdcmDebugBodyMacro(obj, msg) {}
181 #define gdcmDebugMacro(msg) {}
182 #define gdcmStaticDebugMacro(msg) {}
184 #define gdcmDebugBodyMacro(obj, msg) \
186 if( Debug::GetDebugFlag() ) \
188 std::string adds=""; \
191 adds = "Last system error was: "; \
192 adds += strerror(errno); \
195 gdcmMessageBodyMacro(gdcm::CMD_DEBUG,obj,msg,adds); \
198 #define gdcmDebugMacro(msg) \
199 gdcmDebugBodyMacro(NULL,msg)
200 #define gdcmStaticDebugMacro(msg) \
201 gdcmDebugBodyMacro(NULL,msg)
204 // ------------------------------------------------------------------------
207 * \brief Log : we display messages when anything is not Dicom Kosher
208 * @param msg message part
210 // No NDEBUG test to always have a return of warnings !!!
211 // -> Rien compris! JPRx
212 #define gdcmLogBodyMacro(obj, msg) \
214 if( Debug::GetLogFlag() ) \
215 gdcmMessageBodyMacro(gdcm::CMD_LOG,obj,msg,""); \
217 #define gdcmLogMacro(msg) \
218 gdcmLogBodyMacro(this,msg)
219 #define gdcmStaticLogMacro(msg) \
220 gdcmLogBodyMacro(NULL,msg)
222 // ------------------------------------------------------------------------
225 * \brief Warning : To be used to warn the user when some oddity occurs
226 * @param msg message part
228 // No NDEBUG test to always have a return of warnings !!!
229 // -> Rien compris! JPRx
230 #define gdcmWarningBodyMacro(obj, msg) \
232 if( Debug::GetWarningFlag() ) \
233 gdcmMessageBodyMacro(gdcm::CMD_WARNING,obj,msg,""); \
235 #define gdcmWarningMacro(msg) \
236 gdcmWarningBodyMacro(this,msg)
237 #define gdcmStaticWarningMacro(msg) \
238 gdcmWarningBodyMacro(NULL,msg)
240 // ------------------------------------------------------------------------
243 * \brief Error : To be used when unecoverabale error occurs
244 * at a 'deep' level. (don't use it if file is not ACR/DICOM!)
245 * @param msg second message part
247 // No NDEBUG test to always have a return of errors !!!
248 // -> Rien compris! JPRx
249 #define gdcmErrorBodyMacro(obj, msg) \
251 gdcmMessageBodyMacro(gdcm::CMD_ERROR,obj,msg,""); \
253 #define gdcmErrorMacro(msg) \
254 gdcmErrorBodyMacro(this,msg)
255 #define gdcmStaticErrorMacro(msg) \
256 gdcmErrorBodyMacro(NULL,msg)
258 // ------------------------------------------------------------------------
261 * \brief Assert : To be used when an *absolutely* impossible error occurs
262 * No function should be allowed to stop the process instead of
263 * warning the caller!
264 * @param arg argument to test
265 * An easy solution to pass also a message is to do:
266 * gdcmAssertMacro( "my message" && 2 < 3 )
268 // No NDEBUG test to always have a return of asserts !!!
269 // -> Rien compris! JPRx
270 #define gdcmAssertBodyMacro(obj, arg) \
274 gdcmMessageBodyMacro(gdcm::CMD_ASSERT,obj,"",""); \
278 #define gdcmAssertMacro(msg) \
279 gdcmAssertBodyMacro(NULL,msg)
280 #define gdcmStaticAssertMacro(msg) \
281 gdcmAssertBodyMacro(NULL,msg)
283 //-----------------------------------------------------------------------------