aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
debug.h
Go to the documentation of this file.
1 
23 #ifndef GUM_DEBUG_H
24 #define GUM_DEBUG_H
25 
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 
28 // WARNING : Do not include this file directlty : instead include
29 // <agrum/config.h>
30 
31 # include <algorithm>
32 # include <iomanip>
33 # include <iostream>
34 # include <sstream>
35 # include <string>
36 # include <vector>
37 
38 
39 # ifdef GUM_DEBUG_MODE
40 # ifndef GUM_TRACE_ON
41 # define GUM_TRACE_ON // in DEBUG MODE we force TRACE to be ON
42 # else // GUM_TRACE_ON on mode debug add TRACE_CONSTRUCTION_ON (tracing
43 // construction/destruction of object)
44 # define GUM_DEEP_TRACE_ON
45 # endif // GUM_TRACE_ON
46 
47 # define GUM_ASSERT(condition) \
48  do { \
49  if (!(condition)) { \
50  std::cout << std::endl \
51  << __FILE__ << ":" << __LINE__ << " [aGrUM] assert (" \
52  << #condition << ") failed" << std::endl; \
53  std::abort(); \
54  } \
55  } while (0)
56 
57 # define GUM_DEBUG_ONLY(x) \
58  { x }
59 
60 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
61 // DEFINITION OF GUM_CONSTRUCTOR
62 # define GUM_CONSTRUCTOR_BASIC(x) \
63  { \
64  gum::__debug__::inc_creation__( \
65  #x, __FILE__, __LINE__, "constructor of", (void*)this, sizeof(x)); \
66  }
67 # define GUM_CONSTRUCTOR(x) GUM_CONSTRUCTOR_BASIC(x)
68 
69 // FOR EXPANSION OF MACRO IN ARGS OF GUM_DESTRUCTOR, WE NEED TO USE A 2-LEVEL
70 // DEFINITION OF GUM_DESTRUCTOR
71 # define GUM_DESTRUCTOR_BASIC(x) \
72  { \
73  gum::__debug__::inc_deletion__( \
74  #x, __FILE__, __LINE__, "destructor of", (void*)this); \
75  }
76 # define GUM_DESTRUCTOR(x) GUM_DESTRUCTOR_BASIC(x)
77 
78 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONS_CPY, WE NEED TO USE A 2-LEVEL
79 // DEFINITION OF GUM_CONS_CPY
80 # define GUM_CONS_CPY_BASIC(x) \
81  { \
82  gum::__debug__::inc_creation__(#x, \
83  __FILE__, \
84  __LINE__, \
85  "copy constructor of", \
86  (void*)this, \
87  sizeof(x)); \
88  }
89 # define GUM_CONS_CPY(x) GUM_CONS_CPY_BASIC(x)
90 
91 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONS_MOV, WE NEED TO USE A 2-LEVEL
92 // DEFINITION OF GUM_CONS_MOV
93 # define GUM_CONS_MOV_BASIC(x) \
94  { \
95  gum::__debug__::inc_creation__(#x, \
96  __FILE__, \
97  __LINE__, \
98  "move constructor of", \
99  (void*)this, \
100  sizeof(x)); \
101  }
102 # define GUM_CONS_MOV(x) GUM_CONS_MOV_BASIC(x)
103 
104 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
105 // DEFINITION OF GUM_CONSTRUCTOR
106 # define GUM_OP_CPY_BASIC(x) \
107  { \
108  gum::__debug__::show_trace__( \
109  #x, __FILE__, __LINE__, "copy operator of", (void*)this); \
110  }
111 # define GUM_OP_CPY(x) GUM_OP_CPY_BASIC(x)
112 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
113 // DEFINITION OF GUM_CONSTRUCTOR
114 # define GUM_OP_MOV_BASIC(x) \
115  { \
116  gum::__debug__::show_trace__( \
117  #x, __FILE__, __LINE__, "move operator of", (void*)this); \
118  }
119 # define GUM_OP_MOV(x) GUM_OP_MOV_BASIC(x)
120 # else // GUM_DEBUG_MODE
122 # define GUM_ASSERT(condition) ((void)0)
124 # define GUM_CONSTRUCTOR(x) ((void)0)
125 # define GUM_DESTRUCTOR(x) ((void)0)
126 # define GUM_CONS_CPY(x) ((void)0)
127 # define GUM_CONS_MOV(x) ((void)0)
128 # define GUM_OP_CPY(x) ((void)0)
129 # define GUM_OP_MOV(x) ((void)0)
130 
131 # define GUM_DEBUG_ONLY(x)
132 # endif // GUM_DEBUG_MODE
134 
135 # ifdef GUM_TRACE_ON
136 # define GUM__PRINT(file, line, msg) \
137  { \
138  std::string ff(file); \
139  std::cout << file << ":" << line << " [GUM] " << msg << std::endl; \
140  }
141 
142 # define GUM_CHECKPOINT \
143  GUM__PRINT(__FILE__, __LINE__, "******** checkpoint ********")
144 # define GUM_TRACE(msg) GUM__PRINT(__FILE__, __LINE__, msg)
145 # define GUM_TRACE_VAR(var) \
146  GUM__PRINT(__FILE__, __LINE__, "<" << #var << ">: " << var)
147 
148 # define GUM_TRACE_NEWLINE \
149  { std::cout << std::endl; }
150 # else // GUM_TRACE_ON
151 # define GUM__PRINT(line, file, x)
152 # define GUM_CHECKPOINT
153 # define GUM_TRACE(msg)
154 # define GUM_TRACE_VAR(var)
155 # define GUM_TRACE_NEWLINE
156 # endif // GUM_TRACE_ON
157 
158 namespace gum {
159 
160 # ifdef GUM_DEBUG_MODE
161 
162  namespace __debug__ {
163 
164  std::string getFile__(const char* f);
165 
166  void show_trace__(const char* zeKey,
167  const char* zeFile,
168  long zeLine,
169  const char* zeMsg,
170  const void* zePtr);
171  void inc_creation__(const char* zeKey,
172  const char* zeFile,
173  long zeLine,
174  const char* zeMsg,
175  const void* zePtr,
176  int zeSize = -1);
177  void inc_deletion__(const char* zeKey,
178  const char* zeFile,
179  long zeLine,
180  const char* zeMsg,
181  const void* zePtr);
182  void dec_creation__(const char* zeKey,
183  const char* zeFile,
184  long zeLine,
185  const char* zeMsg,
186  const void* zePtr);
187  void dumpObjects__();
188  void atexit__();
189 
190  } // namespace __debug__
191 
192 # endif // GUM_DEBUG_MODE
193 
194  // ===========================================================================
195  // === A CLASS USED FOR MAKING VALGRIND HAPPY IN DEBUG MODE ===
196  // ===========================================================================
197 
198  class Debug: public std::string {
199  public:
200  Debug(const std::string& str) : std::string(str) {}
201 
202  Debug(const char* const str) : std::string(str) {}
203  };
204 
205 } /* namespace gum */
206 
207 #endif // DOXYGEN_SHOULD_SKIP_THIS
208 
209 #endif // GUM_DEBUG_H
STL namespace.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Definition: agrum.h:25
void atexit__()
Used for debug purpose.
Definition: utils_misc.cpp:50