1 /*=========================================================================
4 Module: $RCSfile: gdcmDebug.h,v $
6 Date: $Date: 2006/05/30 08:12:51 $
7 Version: $Revision: 1.55 $
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
164 * @param msg message part
167 #define gdcmMessageBodyMacro(type, obj, msg, adds) \
169 std::ostringstream osmacro; \
170 osmacro << "In " __FILE__ ", line " << __LINE__ \
171 << ", function " << GDCM_FUNCTION << "\n" \
172 << adds << msg << "\n\n"; \
173 gdcm::Debug::SendToOutput(type,osmacro.str(),obj); \
176 // ------------------------------------------------------------------------
179 * \brief Debug : To be used to help bug tracking developer
180 * @param msg message part
183 #define gdcmDebugBodyMacro(obj, msg) {}
184 #define gdcmDebugMacro(msg) {}
185 #define gdcmStaticDebugMacro(msg) {}
187 #define gdcmDebugBodyMacro(obj, msg) \
189 if( Debug::GetDebugFlag() ) \
191 std::string adds=""; \
194 adds = "Last system error was: "; \
195 adds += strerror(errno); \
198 gdcmMessageBodyMacro(gdcm::CMD_DEBUG,obj,msg,adds); \
201 #define gdcmDebugMacro(msg) \
202 gdcmDebugBodyMacro(NULL,msg)
203 #define gdcmStaticDebugMacro(msg) \
204 gdcmDebugBodyMacro(NULL,msg)
207 // ------------------------------------------------------------------------
210 * \brief Log : we display messages when anything is not Dicom Kosher
211 * @param msg message part
213 // No NDEBUG test to always have a return of warnings !!!
214 // -> Rien compris! JPRx
215 #define gdcmLogBodyMacro(obj, msg) \
217 if( Debug::GetLogFlag() ) \
218 gdcmMessageBodyMacro(gdcm::CMD_LOG,obj,msg,""); \
220 #define gdcmLogMacro(msg) \
221 gdcmLogBodyMacro(this,msg)
222 #define gdcmStaticLogMacro(msg) \
223 gdcmLogBodyMacro(NULL,msg)
225 // ------------------------------------------------------------------------
228 * \brief Warning : To be used to warn the user when some oddity occurs
229 * @param msg message part
231 // No NDEBUG test to always have a return of warnings !!!
232 // -> Rien compris! JPRx
233 #define gdcmWarningBodyMacro(obj, msg) \
235 if( Debug::GetWarningFlag() ) \
236 gdcmMessageBodyMacro(gdcm::CMD_WARNING,obj,msg,""); \
238 #define gdcmWarningMacro(msg) \
239 gdcmWarningBodyMacro(this,msg)
240 #define gdcmStaticWarningMacro(msg) \
241 gdcmWarningBodyMacro(NULL,msg)
243 // ------------------------------------------------------------------------
246 * \brief Error : To be used when unecoverabale error occurs
247 * at a 'deep' level. (don't use it if file is not ACR/DICOM!)
248 * @param msg second message part
250 // No NDEBUG test to always have a return of errors !!!
251 // -> Rien compris! JPRx
252 #define gdcmErrorBodyMacro(obj, msg) \
254 gdcmMessageBodyMacro(gdcm::CMD_ERROR,obj,msg,""); \
256 #define gdcmErrorMacro(msg) \
257 gdcmErrorBodyMacro(this,msg)
258 #define gdcmStaticErrorMacro(msg) \
259 gdcmErrorBodyMacro(NULL,msg)
261 // ------------------------------------------------------------------------
264 * \brief Assert : To be used when an *absolutely* impossible error occurs
265 * No function should be allowed to stop the process instead of
266 * warning the caller!
267 * @param arg argument to test
268 * An easy solution to pass also a message is to do:
269 * gdcmAssertMacro( "my message" && 2 < 3 )
271 // No NDEBUG test to always have a return of asserts !!!
272 // -> Rien compris! JPRx
273 #define gdcmAssertBodyMacro(obj, arg) \
277 gdcmMessageBodyMacro(gdcm::CMD_ASSERT,obj,"",""); \
281 #define gdcmAssertMacro(msg) \
282 gdcmAssertBodyMacro(NULL,msg)
283 #define gdcmStaticAssertMacro(msg) \
284 gdcmAssertBodyMacro(NULL,msg)
286 //-----------------------------------------------------------------------------