]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
ENH: update gdcmDebug after Benoit comments
[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 19:20:38 $
7   Version:   $Revision: 1.13 $
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
33  *  - debug : for debugging messages
34  * 
35  * A debugging message has a level of priority and is 
36  * Shown only when the debug level is higher than the 
37  * message level.
38  */
39 class GDCM_EXPORT Debug
40 {
41 public:
42    /// This is a global flag that controls whether any debug, warning
43    /// messages are displayed.
44    static int  GetDebugFlag ();
45    static void SetDebugFlag (int flag);
46    static void SetDebugOn  () { SetDebugFlag(1); };
47    static void SetDebugOff () { SetDebugFlag(0); };
48 };
49
50 } // end namespace gdcm
51
52 // Here we define function this is the only way to be able to pass
53 // stuff with indirection like:
54 // gdcmDebug( "my message:" << i << '\t' ); 
55 // You cannot use function unless you use vnsprintf ...
56
57 // __FUNCTION is not always defined by preprocessor
58 #ifndef __FUNCTION__
59 #  define __FUNCTION__ ""
60 #endif
61
62 /**
63  * \brief   Verbose 
64  * @param level level
65  * @param msg1 first message part
66  * @param msg2 second message part 
67  */
68 #define gdcmDebugMacro(x)                                  \
69 {                                                          \
70    if( gdcm::Debug::GetDebugFlag() )                       \
71    {                                                       \
72    std::ostringstream osmacro;                             \
73    osmacro << "Debug: In " __FILE__ ", line " << __LINE__  \
74       << ", function " << __FUNCTION__ << '\n'             \
75       << x << "\n\n";                                      \
76    std::cerr << osmacro.str() << std::endl;                \
77    }                                                       \
78 }
79
80 /**
81  * \brief   Verbose 
82  * @param level level
83  * @param msg1 first message part
84  * @param msg2 second message part 
85  */
86 #define gdcmVerboseMacro(x)                                 \
87 {                                                           \
88    if( gdcm::Debug::GetDebugFlag() )                        \
89    {                                                        \
90    std::ostringstream osmacro;                              \
91    osmacro << "Verbose: In " __FILE__ ", line " << __LINE__ \
92       << ", function " __FUNCTION__ "\n"                    \
93       << x << "\n\n";                                       \
94    std::cerr << osmacro.str() << std::endl;                 \
95    }                                                        \
96 }
97
98 /**
99  * \brief   Error 
100  * @param test test
101  * @param msg1 first message part
102  * @param msg2 second message part 
103  */
104 #define gdcmErrorMacro(x)                                 \
105 {                                                         \
106    if( gdcm::Debug::GetDebugFlag() )                      \
107    {                                                      \
108    std::ostringstream osmacro;                            \
109    osmacro << "Error: In " __FILE__ ", line " << __LINE__ \
110       << ", function " << __FUNCTION__ << '\n'            \
111       << x << "\n\n";                                     \
112    std::cerr << osmacro.str() << std::endl;               \
113    exit(1);                                               \
114    }                                                      \
115 }
116
117 /**
118  * \brief   Assert 
119  * @param level level 
120  * @param test test
121  * @param msg1 first message part
122  * @param msg2 second message part
123  */
124 #define gdcmAssertMacro(x)                                 \
125 {                                                          \
126    if( x )                                                 \
127    {                                                       \
128    std::ostringstream osmacro;                             \
129    osmacro << "Assert: In " __FILE__ ", line " << __LINE__ \
130                       << ", function " << __FUNCTION__     \
131                       << "\n\n";                           \
132    std::cerr << osmacro.str() << std::endl;                \
133    assert ( x );                                           \
134    }                                                       \
135 }
136
137 #endif