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