aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
debug.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
42#ifndef GUM_DEBUG_H
43#define GUM_DEBUG_H
44
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
46
47// WARNING : Do not include this file directlty : instead include
48// <agrum/config.h>
49
50# include <algorithm>
51# include <iomanip>
52# include <iostream>
53# include <sstream>
54# include <string>
55# include <vector>
56
57
58# ifdef GUM_DEBUG_MODE
59# ifndef GUM_TRACE_ON
60# define GUM_TRACE_ON // in DEBUG MODE we force TRACE to be ON
61# else // GUM_TRACE_ON on mode debug add TRACE_CONSTRUCTION_ON (tracing
62// construction/destruction of object)
63# define GUM_DEEP_TRACE_ON
64# endif // GUM_TRACE_ON
65
66# define GUM_ASSERT(condition) \
67 { \
68 do { \
69 if (!(condition)) { \
70 std::cout << std::endl \
71 << __FILE__ << ":" << __LINE__ << " [aGrUM] assert (" << #condition \
72 << ") failed" << std::endl; \
73 std::abort(); \
74 } \
75 } while (0); \
76 }
77
78# define GUM_DEBUG_ONLY(x) {x}
79
80// FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
81// DEFINITION OF GUM_CONSTRUCTOR
82# define GUM_CONSTRUCTOR_BASIC(x) \
83 { \
84 gum::__debug__::_inc_creation_(#x, \
85 __FILE__, \
86 __LINE__, \
87 "constructor of", \
88 (void*)this, \
89 sizeof(x)); \
90 }
91# define GUM_CONSTRUCTOR(x) GUM_CONSTRUCTOR_BASIC(x);
92
93// FOR EXPANSION OF MACRO IN ARGS OF GUM_DESTRUCTOR, WE NEED TO USE A 2-LEVEL
94// DEFINITION OF GUM_DESTRUCTOR
95# define GUM_DESTRUCTOR_BASIC(x) \
96 { gum::__debug__::_inc_deletion_(#x, __FILE__, __LINE__, "destructor of", (void*)this); }
97# define GUM_DESTRUCTOR(x) GUM_DESTRUCTOR_BASIC(x);
98
99// FOR EXPANSION OF MACRO IN ARGS OF GUM_CONS_CPY, WE NEED TO USE A 2-LEVEL
100// DEFINITION OF GUM_CONS_CPY
101# define GUM_CONS_CPY_BASIC(x) \
102 { \
103 gum::__debug__::_inc_creation_(#x, \
104 __FILE__, \
105 __LINE__, \
106 "copy constructor of", \
107 (void*)this, \
108 sizeof(x)); \
109 }
110# define GUM_CONS_CPY(x) GUM_CONS_CPY_BASIC(x);
111
112// FOR EXPANSION OF MACRO IN ARGS OF GUM_CONS_MOV, WE NEED TO USE A 2-LEVEL
113// DEFINITION OF GUM_CONS_MOV
114# define GUM_CONS_MOV_BASIC(x) \
115 { \
116 gum::__debug__::_inc_creation_(#x, \
117 __FILE__, \
118 __LINE__, \
119 "move constructor of", \
120 (void*)this, \
121 sizeof(x)); \
122 }
123# define GUM_CONS_MOV(x) GUM_CONS_MOV_BASIC(x)
124
125// FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
126// DEFINITION OF GUM_CONSTRUCTOR
127# define GUM_OP_CPY_BASIC(x) \
128 { gum::__debug__::_show_trace_(#x, __FILE__, __LINE__, "copy operator of", (void*)this); }
129# define GUM_OP_CPY(x) GUM_OP_CPY_BASIC(x)
130// FOR EXPANSION OF MACRO IN ARGS OF GUM_CONSTRUCTOR, WE NEED TO USE A 2-LEVEL
131// DEFINITION OF GUM_CONSTRUCTOR
132# define GUM_OP_MOV_BASIC(x) \
133 { gum::__debug__::_show_trace_(#x, __FILE__, __LINE__, "move operator of", (void*)this); }
134# define GUM_OP_MOV(x) GUM_OP_MOV_BASIC(x)
136# else // GUM_DEBUG_MODE
138# define GUM_ASSERT(condition)
139# define GUM_CONSTRUCTOR(x) ;
140# define GUM_DESTRUCTOR(x) ;
141# define GUM_CONS_CPY(x) ;
142# define GUM_CONS_MOV(x) ;
143# define GUM_OP_CPY(x) ;
144# define GUM_OP_MOV(x) ;
145
146# define GUM_DEBUG_ONLY(x)
148# endif // GUM_DEBUG_MODE
149
150# ifdef GUM_TRACE_ON
151# define _GUM_PRINT(file, line, msg) \
152 { \
153 std::string ff(file); \
154 std::cout << file << ":" << line << " [GUM] " << msg << std::endl; \
155 }
156
157# define GUM_CHECKPOINT _GUM_PRINT(__FILE__, __LINE__, "******** checkpoint ********")
158# define GUM_TRACE(msg) _GUM_PRINT(__FILE__, __LINE__, msg)
159# define GUM_TRACE_VAR(var) _GUM_PRINT(__FILE__, __LINE__, "<" << #var << ">: " << var)
160
161# define GUM_TRACE_NEWLINE \
162 { std::cout << std::endl; }
163# else // GUM_TRACE_ON
164# define _GUM_PRINT(line, file, x)
165# define GUM_CHECKPOINT
166# define GUM_TRACE(msg)
167# define GUM_TRACE_VAR(var)
168# define GUM_TRACE_NEWLINE
169# endif // GUM_TRACE_ON
170
171namespace gum {
172
173# ifdef GUM_DEBUG_MODE
174
175 namespace __debug__ {
176
177 std::string _getFile_(const char* f);
178
179 void _show_trace_(const char* zeKey,
180 const char* zeFile,
181 long zeLine,
182 const char* zeMsg,
183 const void* zePtr);
184 void _inc_creation_(const char* zeKey,
185 const char* zeFile,
186 long zeLine,
187 const char* zeMsg,
188 const void* zePtr,
189 int zeSize = -1);
190 void _inc_deletion_(const char* zeKey,
191 const char* zeFile,
192 long zeLine,
193 const char* zeMsg,
194 const void* zePtr);
195 void _dec_creation_(const char* zeKey,
196 const char* zeFile,
197 long zeLine,
198 const char* zeMsg,
199 const void* zePtr);
200 void _dumpObjects_();
201 void _atexit_();
202
203 } // namespace __debug__
204
205# endif // GUM_DEBUG_MODE
206
207 // ===========================================================================
208 // === A CLASS USED FOR MAKING VALGRIND HAPPY IN DEBUG MODE ===
209 // ===========================================================================
210
211 class Debug: public std::string {
212 using std::string::string;
213 };
214
215} /* namespace gum */
216
217#endif // DOXYGEN_SHOULD_SKIP_THIS
218
219#endif // GUM_DEBUG_H
void _atexit_()
Used for debug purpose.
Internal namespace for aGrUM debugging tools.
Definition agrum.h:52
gum is the global namespace for all aGrUM entities
Definition agrum.h:46