aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
debug.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) et Christophe GONZALES(@AMU)
4  * (@AMU) info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #ifndef GUM_DEBUG_H
23 #define GUM_DEBUG_H
24 
25 #ifndef DOXYGEN_SHOULD_SKIP_THIS
26 
27 // WARNING : Do not include this file directlty : instead include
28 // <agrum/config.h>
29 
30 # include <algorithm>
31 # include <iomanip>
32 # include <iostream>
33 # include <sstream>
34 # include <string>
35 # include <vector>
36 
37 
38 # ifdef GUM_DEBUG_MODE
39 # ifndef GUM_TRACE_ON
40 # define GUM_TRACE_ON // in DEBUG MODE we force TRACE to be ON
41 # else // GUM_TRACE_ON on mode debug add TRACE_CONSTRUCTION_ON (tracing
42 // construction/destruction of object)
43 # define GUM_DEEP_TRACE_ON
44 # endif // GUM_TRACE_ON
45 
46 # define GUM_ASSERT(condition)
47  do {
48  if (!(condition)) {
49  std::cout << std::endl
50  << __FILE__ << ":" << __LINE__ << " [aGrUM] assert ("
51  << #condition << ") failed" << std::endl;
52  std::abort();
53  }
54  } while (0)
55 
56 # define GUM_DEBUG_ONLY(x)
57  { x }
58 
59 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
60 // DEFINITION OF GUM_CONSTRUCTOR
61 # define GUM_CONSTRUCTOR_BASIC(x)
62  {
63  gum::__debug__::inc_creation__(#x,
64  __FILE__,
65  __LINE__,
66  "constructor of",
67  (void*)this,
68  sizeof(x));
69  }
70 # define GUM_CONSTRUCTOR(x) GUM_CONSTRUCTOR_BASIC(x)
71 
72 // FOR EXPANSION OF MACRO IN ARGS OF GUM_DESTRUCTOR, WE NEED TO USE A 2-LEVEL
73 // DEFINITION OF GUM_DESTRUCTOR
74 # define GUM_DESTRUCTOR_BASIC(x)
75  {
76  gum::__debug__::inc_deletion__(#x,
77  __FILE__,
78  __LINE__,
79  "destructor of",
80  (void*)this);
81  }
82 # define GUM_DESTRUCTOR(x) GUM_DESTRUCTOR_BASIC(x)
83 
84 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONS_CPY, WE NEED TO USE A 2-LEVEL
85 // DEFINITION OF GUM_CONS_CPY
86 # define GUM_CONS_CPY_BASIC(x)
87  {
88  gum::__debug__::inc_creation__(#x,
89  __FILE__,
90  __LINE__,
91  "copy constructor of",
92  (void*)this,
93  sizeof(x));
94  }
95 # define GUM_CONS_CPY(x) GUM_CONS_CPY_BASIC(x)
96 
97 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONS_MOV, WE NEED TO USE A 2-LEVEL
98 // DEFINITION OF GUM_CONS_MOV
99 # define GUM_CONS_MOV_BASIC(x)
100  {
101  gum::__debug__::inc_creation__(#x,
102  __FILE__,
103  __LINE__,
104  "move constructor of",
105  (void*)this,
106  sizeof(x));
107  }
108 # define GUM_CONS_MOV(x) GUM_CONS_MOV_BASIC(x)
109 
110 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
111 // DEFINITION OF GUM_CONSTRUCTOR
112 # define GUM_OP_CPY_BASIC(x)
113  {
114  gum::__debug__::show_trace__(#x,
115  __FILE__,
116  __LINE__,
117  "copy operator of",
118  (void*)this);
119  }
120 # define GUM_OP_CPY(x) GUM_OP_CPY_BASIC(x)
121 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
122 // DEFINITION OF GUM_CONSTRUCTOR
123 # define GUM_OP_MOV_BASIC(x)
124  {
125  gum::__debug__::show_trace__(#x,
126  __FILE__,
127  __LINE__,
128  "move operator of",
129  (void*)this);
130  }
131 # define GUM_OP_MOV(x) GUM_OP_MOV_BASIC(x)
132 /////////////////////////////////////////////////////////////
133 # else // GUM_DEBUG_MODE
134 /////////////////////////////////////////////////////////////
135 # define GUM_ASSERT(condition) ((void)0)
136 # define GUM_CONSTRUCTOR(x) ((void)0)
137 # define GUM_DESTRUCTOR(x) ((void)0)
138 # define GUM_CONS_CPY(x) ((void)0)
139 # define GUM_CONS_MOV(x) ((void)0)
140 # define GUM_OP_CPY(x) ((void)0)
141 # define GUM_OP_MOV(x) ((void)0)
142 
143 # define GUM_DEBUG_ONLY(x)
144 /////////////////////////////////////////////////////////////
145 # endif // GUM_DEBUG_MODE
146 
147 # ifdef GUM_TRACE_ON
148 # define GUM__PRINT(file, line, msg)
149  {
150  std::string ff(file);
151  std::cout << file << ":" << line << " [GUM] " << msg << std::endl;
152  }
153 
154 # define GUM_CHECKPOINT
155  GUM__PRINT(__FILE__, __LINE__, "******** checkpoint ********")
156 # define GUM_TRACE(msg) GUM__PRINT(__FILE__, __LINE__, msg)
157 # define GUM_TRACE_VAR(var)
158  GUM__PRINT(__FILE__, __LINE__, "<" << #var << ">: " << var)
159 
160 # define GUM_TRACE_NEWLINE
161  { std::cout << std::endl; }
162 # else // GUM_TRACE_ON
163 # define GUM__PRINT(line, file, x)
164 # define GUM_CHECKPOINT
165 # define GUM_TRACE(msg)
166 # define GUM_TRACE_VAR(var)
167 # define GUM_TRACE_NEWLINE
168 # endif // GUM_TRACE_ON
169 
170 namespace gum {
171 
172 # ifdef GUM_DEBUG_MODE
173 
174  namespace __debug__ {
175 
176  std::string getFile__(const char* f);
177 
178  void show_trace__(const char* zeKey,
179  const char* zeFile,
180  long zeLine,
181  const char* zeMsg,
182  const void* zePtr);
183  void inc_creation__(const char* zeKey,
184  const char* zeFile,
185  long zeLine,
186  const char* zeMsg,
187  const void* zePtr,
188  int zeSize = -1);
189  void inc_deletion__(const char* zeKey,
190  const char* zeFile,
191  long zeLine,
192  const char* zeMsg,
193  const void* zePtr);
194  void dec_creation__(const char* zeKey,
195  const char* zeFile,
196  long zeLine,
197  const char* zeMsg,
198  const void* zePtr);
199  void dumpObjects__();
200  void atexit__();
201 
202  } // namespace __debug__
203 
204 # endif // GUM_DEBUG_MODE
205 
206  // ===========================================================================
207  // === A CLASS USED FOR MAKING VALGRIND HAPPY IN DEBUG MODE ===
208  // ===========================================================================
209 
210  class Debug: public std::string {
211  public:
212  Debug(const std::string& str) : std::string(str) {}
213 
214  Debug(const char* const str) : std::string(str) {}
215  };
216 
217 } /* namespace gum */
218 
219 #endif // DOXYGEN_SHOULD_SKIP_THIS
220 
221 #endif // GUM_DEBUG_H