1 /*=========================================================================
4 Module: $RCSfile: gdcmDebug.h,v $
6 Date: $Date: 2005/11/28 15:20:32 $
7 Version: $Revision: 1.48 $
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 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)
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();
51 class GDCM_EXPORT Debug
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); }
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); }
78 /// \brief This is a global flag that controls if debug are redirected
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); }
87 static void SetOutputFileName (std::string const &filename);
89 static std::ostream &GetOutput ();
91 static void SendToOutput(unsigned int type,std::string const &msg,const CommandManager *mgr = NULL);
94 static bool DebugFlag;
95 static bool WarningFlag;
96 static bool OutputToFile;
98 static std::ofstream OutputFileStream;
99 static std::ostream &StandardStream;
101 static const int LINE_LENGTH;
104 } // end namespace gdcm
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 ...
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++
117 # define __FUNCTION__ __FUNC__
120 # define GDCM_FUNCTION __PRETTY_FUNCTION__
122 # define GDCM_FUNCTION __FUNCTION__
125 # define GDCM_FUNCTION "<unknow>"
126 #endif //GDCM_COMPILER_HAS_FUNCTION
129 * \brief Debug : To be used to help bug tracking developer
130 * @param msg message part
132 #define gdcmMessageBodyMacro(type,obj,msg,adds) \
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); \
142 * \brief Debug : To be used to help bug tracking developer
143 * @param msg message part
146 #define gdcmDebugBodyMacro(obj,msg) {}
147 #define gdcmDebugMacro(msg) {}
148 #define gdcmStaticDebugMacro(msg) {}
150 #define gdcmDebugBodyMacro(obj,msg) \
152 if( Debug::GetDebugFlag() ) \
154 std::string adds=""; \
157 adds = "Last system error was: "; \
158 adds += strerror(errno); \
161 gdcmMessageBodyMacro(gdcm::CMD_DEBUG,obj,msg,adds); \
164 #define gdcmDebugMacro(msg) \
165 gdcmDebugBodyMacro(this,msg)
166 #define gdcmStaticDebugMacro(msg) \
167 gdcmDebugBodyMacro(NULL,msg)
171 * \brief Warning : To be used to warn the user when some oddity occurs
172 * @param msg message part
174 // No NDEBUG test to always have a return of warnings !!!
175 #define gdcmWarningBodyMacro(obj,msg) \
177 if( Debug::GetWarningFlag() ) \
178 gdcmMessageBodyMacro(gdcm::CMD_WARNING,obj,msg,""); \
180 #define gdcmWarningMacro(msg) \
181 gdcmWarningBodyMacro(this,msg)
182 #define gdcmStaticWarningMacro(msg) \
183 gdcmWarningBodyMacro(NULL,msg)
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
190 // No NDEBUG test to always have a return of errors !!!
191 #define gdcmErrorBodyMacro(obj,msg) \
193 gdcmMessageBodyMacro(gdcm::CMD_ERROR,obj,msg,""); \
195 #define gdcmErrorMacro(msg) \
196 gdcmErrorBodyMacro(this,msg)
197 #define gdcmStaticErrorMacro(msg) \
198 gdcmErrorBodyMacro(NULL,msg)
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 )
208 // No NDEBUG test to always have a return of asserts !!!
209 #define gdcmAssertBodyMacro(obj,arg) \
213 gdcmMessageBodyMacro(gdcm::CMD_ASSERT,obj,"",""); \
217 #define gdcmAssertMacro(msg) \
218 gdcmAssertBodyMacro(this,msg)
219 #define gdcmStaticAssertMacro(msg) \
220 gdcmAssertBodyMacro(NULL,msg)
222 //-----------------------------------------------------------------------------