1 /*=========================================================================
4 Module: $RCSfile: gdcmDebug.h,v $
6 Date: $Date: 2005/06/21 14:05:06 $
7 Version: $Revision: 1.35 $
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"
31 //-----------------------------------------------------------------------------
34 * \brief Debug is an object for debugging in program.
35 * It has 2 debugging modes :
36 * - error : for bad library use, seriously wrong DICOM
37 * - debug : for information/debug messages
38 * - warning : for warning about DICOM quality (kosher)
39 * - assert : design by contract implementation. A function should have
40 * proper input and proper output.
41 * (should not happen, not user controlled)
43 * A debugging message is only show if the flag is on (DebugFlag)
44 * This is static var and can be set at beginning of code:
45 * gdcm::Debug::SetDebugOn();
47 class GDCM_EXPORT Debug
53 /// \brief This is a global flag that controls whether any debug, warning
54 /// messages are displayed.
55 static void SetDebugFlag (bool flag);
56 static bool GetDebugFlag ();
57 /// \brief Sets the Debug Flag to true
58 static void DebugOn () { SetDebugFlag(true); };
59 /// \brief Sets the Debug Flag to false
60 static void DebugOff () { SetDebugFlag(false); };
62 /// \brief This is a global flag that controls if debug are redirected
64 static void SetDebugToFile (bool flag);
65 static bool GetDebugToFile ();
66 /// \brief Next debug messages will be sent in the debug file
67 static void DebugToFileOn () { SetDebugToFile(true); };
68 /// \brief Next debug messages will be sent in the standard output
69 static void DebugToFileOff () { SetDebugToFile(false); };
71 static void SetDebugFilename (std::string const &filename);
73 static std::ofstream &GetDebugFile ();
76 } // end namespace gdcm
78 // Here we define function this is the only way to be able to pass
79 // stuff with indirection like:
80 // gdcmDebug( "my message:" << i << '\t' );
81 // You cannot use function unless you use vnsprintf ...
83 // __FUNCTION is not always defined by preprocessor
84 // In c++ we should use __PRETTY_FUNCTION__ instead...
85 #ifdef GDCM_COMPILER_HAS_FUNCTION
86 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
87 // which is a lot nice in C++
89 # define __FUNCTION__ __FUNC__
92 # define GDCM_FUNCTION __PRETTY_FUNCTION__
94 # define GDCM_FUNCTION __FUNCTION__
97 # define GDCM_FUNCTION "<unknow>"
98 #endif //GDCM_COMPILER_HAS_FUNCTION
102 * @param msg message part
105 #define gdcmDebugMacro(msg) {}
107 #define gdcmDebugMacro(msg) \
109 if( Debug::GetDebugFlag() ) \
111 std::ostringstream osmacro; \
112 osmacro << "Debug: In " __FILE__ ", line " << __LINE__ \
113 << ", function " << GDCM_FUNCTION << '\n' \
114 << "Last system error was: " << strerror(errno) \
115 << '\n' << msg << "\n\n"; \
116 if( Debug::GetDebugToFile() ) \
117 Debug::GetDebugFile() << osmacro.str() << std::endl; \
119 std::cerr << osmacro.str() << std::endl; \
126 * @param msg message part
129 #define gdcmWarningMacro(msg) {}
131 #define gdcmWarningMacro(msg) \
133 if( Debug::GetDebugFlag() ) \
135 std::ostringstream osmacro; \
136 osmacro << "Warning: In " __FILE__ ", line " << __LINE__ \
137 << ", function " << GDCM_FUNCTION << "\n" \
139 if( Debug::GetDebugToFile() ) \
140 Debug::GetDebugFile() << osmacro.str() << std::endl; \
142 std::cerr << osmacro.str() << std::endl; \
149 * @param msg second message part
152 #define gdcmErrorMacro(msg) {}
154 #define gdcmErrorMacro(msg) \
156 std::ostringstream osmacro; \
157 osmacro << "Error: In " __FILE__ ", line " << __LINE__ \
158 << ", function " << GDCM_FUNCTION << '\n' \
160 if( Debug::GetDebugToFile() ) \
161 Debug::GetDebugFile() << osmacro.str() << std::endl; \
163 std::cerr << osmacro.str() << std::endl; \
169 * @param arg argument to test
170 * An easy solution to pass also a message is to do:
171 * gdcmAssertMacro( "my message" && 2 < 3 )
174 #define gdcmAssertMacro(arg) {}
176 #define gdcmAssertMacro(arg) \
180 std::ostringstream osmacro; \
181 osmacro << "Assert: In " __FILE__ ", line " << __LINE__ \
182 << ", function " << GDCM_FUNCTION \
184 if( Debug::GetDebugToFile() ) \
185 Debug::GetDebugFile() << osmacro.str() << std::endl; \
187 std::cerr << osmacro.str() << std::endl; \