]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
ENH: gdcmDebugMacro is now different from gdcmVerboseMacro as now it also display...
[gdcm.git] / src / gdcmDebug.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.h,v $
5   Language:  C++
6   Date:      $Date: 2005/01/12 21:53:32 $
7   Version:   $Revision: 1.20 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #ifndef GDCMDEBUG_H
20 #define GDCMDEBUG_H
21
22 #include "gdcmCommon.h"
23
24 #include <sstream>
25 #include <assert.h>
26 #include <errno.h>
27
28 namespace gdcm 
29 {
30 //-----------------------------------------------------------------------------
31
32 /**
33  * \ingroup Debug
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  * 
40  * A debugging message is only show if the flag is on (DebugFlag)
41  * This is static var and can be set at begining of code:
42  *         gdcm::Debug::SetDebugOn();
43  */
44 class GDCM_EXPORT Debug
45 {
46 public:
47    /// This is a global flag that controls whether any debug, warning
48    /// messages are displayed.
49    static int  GetDebugFlag ();
50    static void SetDebugFlag (int flag);
51    static void SetDebugOn  () { SetDebugFlag(1); };
52    static void SetDebugOff () { SetDebugFlag(0); };
53 };
54
55 } // end namespace gdcm
56
57 // Here we define function this is the only way to be able to pass
58 // stuff with indirection like:
59 // gdcmDebug( "my message:" << i << '\t' ); 
60 // You cannot use function unless you use vnsprintf ...
61
62 // __FUNCTION is not always defined by preprocessor
63 // In c++ we should use __PRETTY_FUNCTION__ instead...
64 #ifdef GDCM_COMPILER_HAS_FUNCTION
65 // Handle particular case for GNU C++ which also defines __PRETTY_FUNCTION__
66 // which is a lot nice in C++
67 #ifdef __BORLANDC__
68 #  define __FUNCTION__ __FUNC__
69 #endif
70 #ifdef __GNUC__
71 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
72 #else
73 #  define GDCM_FUNCTION __FUNCTION__ 
74 #endif //__GNUC__
75 #else
76 #  define GDCM_FUNCTION "<unknow>"
77 #endif //GDCM_COMPILER_HAS_FUNCTION
78
79 /**
80  * \brief   Debug
81  * @param msg message part
82  */
83 #define gdcmDebugMacro(msg)                                \
84 {                                                          \
85    if( gdcm::Debug::GetDebugFlag() )                       \
86    {                                                       \
87    std::ostringstream osmacro;                             \
88    osmacro << "Debug: In " __FILE__ ", line " << __LINE__  \
89            << ", function " << GDCM_FUNCTION << '\n'       \
90            << "Last system error was: " << strerror(errno) \
91            << '\n' << msg << "\n\n";                       \
92    std::cerr << osmacro.str() << std::endl;                \
93    }                                                       \
94 }
95
96 /**
97  * \brief   Verbose 
98  * @param msg message part
99  */
100 #define gdcmVerboseMacro(msg)                               \
101 {                                                           \
102    if( gdcm::Debug::GetDebugFlag() )                        \
103    {                                                        \
104    std::ostringstream osmacro;                              \
105    osmacro << "Verbose: In " __FILE__ ", line " << __LINE__ \
106            << ", function " << GDCM_FUNCTION << "\n"        \
107            << msg << "\n\n";                                \
108    std::cerr << osmacro.str() << std::endl;                 \
109    }                                                        \
110 }
111
112 /**
113  * \brief   Error 
114  * @param msg second message part 
115  */
116 #define gdcmErrorMacro(msg)                               \
117 {                                                         \
118    if( gdcm::Debug::GetDebugFlag() )                      \
119    {                                                      \
120    std::ostringstream osmacro;                            \
121    osmacro << "Error: In " __FILE__ ", line " << __LINE__ \
122            << ", function " << GDCM_FUNCTION << '\n'      \
123            << msg << "\n\n";                              \
124    std::cerr << osmacro.str() << std::endl;               \
125    exit(1);                                               \
126    }                                                      \
127 }
128
129 /**
130  * \brief   Assert 
131  * @param arg argument to test
132  *        An easy solution to pass also a message is to do:
133  *        gdcmAssertMacro( "my message" && 2 < 3 )
134  */
135 #define gdcmAssertMacro(arg)                               \
136 {                                                          \
137    if( !(arg) )                                              \
138    {                                                       \
139    std::ostringstream osmacro;                             \
140    osmacro << "Assert: In " __FILE__ ", line " << __LINE__ \
141            << ", function " << GDCM_FUNCTION               \
142            << "\n\n";                                      \
143    std::cerr << osmacro.str() << std::endl;                \
144    assert ( arg );                                         \
145    }                                                       \
146 }
147
148 #endif