aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
debug.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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  {
48  do {
49  if (!(condition)) {
50  std::cout << std::endl
51  << __FILE__ << ":" << __LINE__ << " [aGrUM] assert (" << #condition
52  << ") failed" << std::endl;
53  std::abort();
54  }
55  } while (0);
56  }
57 
58 # define GUM_DEBUG_ONLY(x)
59  { x }
60 
61 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
62 // DEFINITION OF GUM_CONSTRUCTOR
63 # define GUM_CONSTRUCTOR_BASIC(x)
64  {
65  gum::__debug__::_inc_creation_(#x,
66  __FILE__,
67  __LINE__,
68  "constructor of",
69  (void*)this,
70  sizeof(x));
71  }
72 # define GUM_CONSTRUCTOR(x) GUM_CONSTRUCTOR_BASIC(x);
73 
74 // FOR EXPANSION OF MACRO IN ARGS OF GUM_DESTRUCTOR, WE NEED TO USE A 2-LEVEL
75 // DEFINITION OF GUM_DESTRUCTOR
76 # define GUM_DESTRUCTOR_BASIC(x)
77  { gum::__debug__::_inc_deletion_(#x, __FILE__, __LINE__, "destructor of", (void*)this); }
78 # define GUM_DESTRUCTOR(x) GUM_DESTRUCTOR_BASIC(x);
79 
80 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONS_CPY, WE NEED TO USE A 2-LEVEL
81 // DEFINITION OF GUM_CONS_CPY
82 # define GUM_CONS_CPY_BASIC(x)
83  {
84  gum::__debug__::_inc_creation_(#x,
85  __FILE__,
86  __LINE__,
87  "copy constructor of",
88  (void*)this,
89  sizeof(x));
90  }
91 # define GUM_CONS_CPY(x) GUM_CONS_CPY_BASIC(x);
92 
93 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONS_MOV, WE NEED TO USE A 2-LEVEL
94 // DEFINITION OF GUM_CONS_MOV
95 # define GUM_CONS_MOV_BASIC(x)
96  {
97  gum::__debug__::_inc_creation_(#x,
98  __FILE__,
99  __LINE__,
100  "move constructor of",
101  (void*)this,
102  sizeof(x));
103  }
104 # define GUM_CONS_MOV(x) GUM_CONS_MOV_BASIC(x)
105 
106 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
107 // DEFINITION OF GUM_CONSTRUCTOR
108 # define GUM_OP_CPY_BASIC(x)
109  { gum::__debug__::_show_trace_(#x, __FILE__, __LINE__, "copy operator of", (void*)this); }
110 # define GUM_OP_CPY(x) GUM_OP_CPY_BASIC(x)
111 // FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
112 // DEFINITION OF GUM_CONSTRUCTOR
113 # define GUM_OP_MOV_BASIC(x)
114  { gum::__debug__::_show_trace_(#x, __FILE__, __LINE__, "move operator of", (void*)this); }
115 # define GUM_OP_MOV(x) GUM_OP_MOV_BASIC(x)
116 /////////////////////////////////////////////////////////////
117 # else // GUM_DEBUG_MODE
118 /////////////////////////////////////////////////////////////
119 # define GUM_ASSERT(condition)
120 # define GUM_CONSTRUCTOR(x) ;
121 # define GUM_DESTRUCTOR(x) ;
122 # define GUM_CONS_CPY(x) ;
123 # define GUM_CONS_MOV(x)
124 # define GUM_OP_CPY(x)
125 # define GUM_OP_MOV(x)
126 
127 # define GUM_DEBUG_ONLY(x)
128 /////////////////////////////////////////////////////////////
129 # endif // GUM_DEBUG_MODE
130 
131 # ifdef GUM_TRACE_ON
132 # define _GUM_PRINT(file, line, msg)
133  {
134  std::string ff(file);
135  std::cout << file << ":" << line << " [GUM] " << msg << std::endl;
136  }
137 
138 # define GUM_CHECKPOINT _GUM_PRINT(__FILE__, __LINE__, "******** checkpoint ********")
139 # define GUM_TRACE(msg) _GUM_PRINT(__FILE__, __LINE__, msg)
140 # define GUM_TRACE_VAR(var) _GUM_PRINT(__FILE__, __LINE__, "<" << # var << ">: " << var)
141 
142 # define GUM_TRACE_NEWLINE
143  { std::cout << std::endl; }
144 # else // GUM_TRACE_ON
145 # define _GUM_PRINT(line, file, x)
146 # define GUM_CHECKPOINT
147 # define GUM_TRACE(msg)
148 # define GUM_TRACE_VAR(var)
149 # define GUM_TRACE_NEWLINE
150 # endif // GUM_TRACE_ON
151 
152 namespace gum {
153 
154 # ifdef GUM_DEBUG_MODE
155 
156  namespace __debug__ {
157 
158  std::string _getFile_(const char* f);
159 
160  void _show_trace_(const char* zeKey,
161  const char* zeFile,
162  long zeLine,
163  const char* zeMsg,
164  const void* zePtr);
165  void _inc_creation_(const char* zeKey,
166  const char* zeFile,
167  long zeLine,
168  const char* zeMsg,
169  const void* zePtr,
170  int zeSize = -1);
171  void _inc_deletion_(const char* zeKey,
172  const char* zeFile,
173  long zeLine,
174  const char* zeMsg,
175  const void* zePtr);
176  void _dec_creation_(const char* zeKey,
177  const char* zeFile,
178  long zeLine,
179  const char* zeMsg,
180  const void* zePtr);
181  void _dumpObjects_();
182  void _atexit_();
183 
184  } // namespace __debug__
185 
186 # endif // GUM_DEBUG_MODE
187 
188  // ===========================================================================
189  // === A CLASS USED FOR MAKING VALGRIND HAPPY IN DEBUG MODE ===
190  // ===========================================================================
191 
192  class Debug: public std::string {
193  public:
194  Debug(const std::string& str) : std::string(str) {}
195 
196  Debug(const char* const str) : std::string(str) {}
197  };
198 
199 } /* namespace gum */
200 
201 #endif // DOXYGEN_SHOULD_SKIP_THIS
202 
203 #endif // GUM_DEBUG_H