]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
Two things at once: start removing hack for old ostrstream which was really bad ...
[gdcm.git] / src / gdcmDebug.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.h,v $
5   Language:  C++
6   Date:      $Date: 2005/01/08 23:14:05 $
7   Version:   $Revision: 1.18 $
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 __GNUC__
67 #  define GDCM_FUNCTION __PRETTY_FUNCTION__
68 #else
69 #  define GDCM_FUNCTION __FUNCTION__ 
70 #endif //__GNUC__
71 #else
72 #  define GDCM_FUNCTION "<unknow>"
73 #endif //GDCM_COMPILER_HAS_FUNCTION
74
75 /**
76  * \brief   Debug
77  * @param msg message part
78  */
79 #define gdcmDebugMacro(msg)                                \
80 {                                                          \
81    if( gdcm::Debug::GetDebugFlag() )                       \
82    {                                                       \
83    std::ostringstream osmacro;                             \
84    osmacro << "Debug: In " __FILE__ ", line " << __LINE__  \
85            << ", function " << GDCM_FUNCTION << '\n'       \
86            << msg << "\n\n";                               \
87    std::cerr << osmacro.str() << std::endl;                \
88    }                                                       \
89 }
90
91 /**
92  * \brief   Verbose 
93  * @param msg message part
94  */
95 #define gdcmVerboseMacro(msg)                               \
96 {                                                           \
97    if( gdcm::Debug::GetDebugFlag() )                        \
98    {                                                        \
99    std::ostringstream osmacro;                              \
100    osmacro << "Verbose: In " __FILE__ ", line " << __LINE__ \
101            << ", function " << GDCM_FUNCTION << "\n"        \
102            << msg << "\n\n";                                \
103    std::cerr << osmacro.str() << std::endl;                 \
104    }                                                        \
105 }
106
107 /**
108  * \brief   Error 
109  * @param msg second message part 
110  */
111 #define gdcmErrorMacro(msg)                               \
112 {                                                         \
113    if( gdcm::Debug::GetDebugFlag() )                      \
114    {                                                      \
115    std::ostringstream osmacro;                            \
116    osmacro << "Error: In " __FILE__ ", line " << __LINE__ \
117            << ", function " << GDCM_FUNCTION << '\n'      \
118            << msg << "\n\n";                              \
119    std::cerr << osmacro.str() << std::endl;               \
120    exit(1);                                               \
121    }                                                      \
122 }
123
124 /**
125  * \brief   Assert 
126  * @param arg argument to test
127  *        An easy solution to pass also a message is to do:
128  *        gdcmAssertMacro( "my message" && 2 < 3 )
129  */
130 #define gdcmAssertMacro(arg)                               \
131 {                                                          \
132    if( !(arg) )                                              \
133    {                                                       \
134    std::ostringstream osmacro;                             \
135    osmacro << "Assert: In " __FILE__ ", line " << __LINE__ \
136            << ", function " << GDCM_FUNCTION               \
137            << "\n\n";                                      \
138    std::cerr << osmacro.str() << std::endl;                \
139    assert ( arg );                                         \
140    }                                                       \
141 }
142
143 #endif