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