]> Creatis software - gdcm.git/blob - src/gdcmDebug.h
ENH: update comments. Rename variable with more complexe name to avoid collision.
[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 20:31:36 $
7   Version:   $Revision: 1.14 $
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 #ifndef __GNUC__
61 #  define __FUNCTION__ "<unknow>"
62 #endif
63
64 /**
65  * \brief   Debug
66  * @param msg message part
67  */
68 #define gdcmDebugMacro(msg)                                \
69 {                                                          \
70    if( gdcm::Debug::GetDebugFlag() )                       \
71    {                                                       \
72    std::ostringstream osmacro;                             \
73    osmacro << "Debug: In " __FILE__ ", line " << __LINE__  \
74            << ", function " << __FUNCTION__ << '\n'        \
75            << msg << "\n\n";                               \
76    std::cerr << osmacro.str() << std::endl;                \
77    }                                                       \
78 }
79
80 /**
81  * \brief   Verbose 
82  * @param msg message part
83  */
84 #define gdcmVerboseMacro(msg)                               \
85 {                                                           \
86    if( gdcm::Debug::GetDebugFlag() )                        \
87    {                                                        \
88    std::ostringstream osmacro;                              \
89    osmacro << "Verbose: In " __FILE__ ", line " << __LINE__ \
90            << ", function " << __FUNCTION__ << "\n"         \
91            << msg << "\n\n";                                \
92    std::cerr << osmacro.str() << std::endl;                 \
93    }                                                        \
94 }
95
96 /**
97  * \brief   Error 
98  * @param msg second message part 
99  */
100 #define gdcmErrorMacro(msg)                               \
101 {                                                         \
102    if( gdcm::Debug::GetDebugFlag() )                      \
103    {                                                      \
104    std::ostringstream osmacro;                            \
105    osmacro << "Error: In " __FILE__ ", line " << __LINE__ \
106            << ", function " << __FUNCTION__ << '\n'       \
107            << msg << "\n\n";                              \
108    std::cerr << osmacro.str() << std::endl;               \
109    exit(1);                                               \
110    }                                                      \
111 }
112
113 /**
114  * \brief   Assert 
115  * @param arg argument to test
116  *        An easy solution to pass also a message is to do:
117  *        gdcmAssertMacro( "my message" && 2 < 3 )
118  */
119 #define gdcmAssertMacro(arg)                               \
120 {                                                          \
121    if( arg )                                               \
122    {                                                       \
123    std::ostringstream osmacro;                             \
124    osmacro << "Assert: In " __FILE__ ", line " << __LINE__ \
125            << ", function " << __FUNCTION__                \
126            << "\n\n";                                      \
127    std::cerr << osmacro.str() << std::endl;                \
128    assert ( arg );                                         \
129    }                                                       \
130 }
131
132 #endif