1 /*=========================================================================
4 Module: $RCSfile: gdcmDebug.h,v $
6 Date: $Date: 2005/01/27 11:55:57 $
7 Version: $Revision: 1.25 $
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 //-----------------------------------------------------------------------------
35 * \brief Debug is an object for debugging in program.
36 * It has 2 debugging modes :
37 * - error : for bad library use, seriously wrong DICOM
38 * - debug : for information/debug messages
39 * - warning : for warning about DICOM quality (kosher)
41 * A debugging message is only show if the flag is on (DebugFlag)
42 * This is static var and can be set at begining of code:
43 * gdcm::Debug::SetDebugOn();
45 class GDCM_EXPORT Debug
51 /// This is a global flag that controls whether any debug, warning
52 /// messages are displayed.
53 static bool GetDebugFlag ();
54 static void SetDebugFlag (bool flag);
55 static void SetDebugOn () { SetDebugFlag(true); };
56 static void SetDebugOff () { SetDebugFlag(false); };
58 /// This is a global flag that controls if debug are redirected
60 static bool GetDebugToFile ();
61 static void SetDebugToFile (bool flag);
62 static void SetDebugToFileOn () { SetDebugToFile(true); };
63 static void SetDebugToFileOff () { SetDebugToFile(false); };
65 /// Set the filename the debug stream should be redirect to
66 /// Settting a filename also set DebugToFile to true
67 static void SetDebugFilename (std::string const& filename);
69 /// Internal use only. Allow us to retrieve the static from anywhere
71 static std::ofstream &GetDebugFile ();
74 } // end namespace gdcm
76 // Here we define function this is the only way to be able to pass
77 // stuff with indirection like:
78 // gdcmDebug( "my message:" << i << '\t' );
79 // You cannot use function unless you use vnsprintf ...
81 // __FUNCTION is not always defined by preprocessor
82 // In c++ we should use __PRETTY_FUNCTION__ instead...
83 #ifdef GDCM_COMPILER_HAS_FUNCTION
84 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
85 // which is a lot nice in C++
87 # define __FUNCTION__ __FUNC__
90 # define GDCM_FUNCTION __PRETTY_FUNCTION__
92 # define GDCM_FUNCTION __FUNCTION__
95 # define GDCM_FUNCTION "<unknow>"
96 #endif //GDCM_COMPILER_HAS_FUNCTION
100 * @param msg message part
103 #define gdcmDebugMacro(msg)
105 #define gdcmDebugMacro(msg) \
107 if( Debug::GetDebugFlag() ) \
109 std::ostringstream osmacro; \
110 osmacro << "Debug: In " __FILE__ ", line " << __LINE__ \
111 << ", function " << GDCM_FUNCTION << '\n' \
112 << "Last system error was: " << strerror(errno) \
113 << '\n' << msg << "\n\n"; \
114 if( Debug::GetDebugToFile() ) \
115 Debug::GetDebugFile() << osmacro.str() << std::endl; \
117 std::cerr << osmacro.str() << std::endl; \
124 * @param msg message part
127 #define gdcmVerboseMacro(msg) \
131 #define gdcmVerboseMacro(msg) \
133 if( Debug::GetDebugFlag() ) \
135 std::ostringstream osmacro; \
136 osmacro << "Verbose: 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; \