MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
debug.h
Go to the documentation of this file.
1// Copyright (C) 2024 The m-AIA AUTHORS
2//
3// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
4//
5// SPDX-License-Identifier: LGPL-3.0-only
6
7#ifndef DEBUG_H
8#define DEBUG_H
9
10#include <cstdlib>
11#include <iostream>
12#include <sstream>
13#include "INCLUDE/maiamacro.h"
14#include "INCLUDE/maiatypes.h"
15#include "IO/infoout.h"
16#include "UTIL/timer.h"
17#include "globalvariables.h"
18
19#define __FUNCTION_LOCATION__ std::string(__FILE__) + ": " + std::string(__FUNCTION__)
20
21#ifdef MAIA_PROFILING
22// Define profiling macros
23#define DEBUG_DISPLAY_ON
24#define DEBUG_DISPLAY_OFF
25#define SET_DEBUG_LEVEL(a) MDebug::setLevelNotEnabled(a)
26#define DEBUG(a, b)
27#define PROFILE(id, a, b) \
28 do { \
29 if(b == MAIA_DEBUG_TRACE_IN) { \
30 Profile::s_functionTimings[id].in(); \
31 } else if(b == MAIA_DEBUG_TRACE_OUT) { \
32 Profile::s_functionTimings[id].out(); \
33 } \
34 } while(false)
35
36// Note: this needs to be without braces/do-while, using this will cause the Tracer object to directly go out of of
37// scope again
38// Note: to enable profiling of all TRACE-instrumented functions set the last argument to 'true'
39#define TRACE() \
40 static MInt timingId = Profile::getTimingId(FUN_); \
41 maia::debug::Tracer USE_ONLY_ONCE_PER_METHOD(FUN_, LOC_, timingId, false)
42// Use TRACE_PROFILE instead of TRACE if only certain functions should be profiled
43#define TRACE_PROFILE() \
44 static MInt timingId = Profile::getTimingId(FUN_); \
45 maia::debug::Tracer USE_ONLY_ONCE_PER_METHOD(FUN_, LOC_, timingId, true)
46
47#elif defined MAIA_DEBUG_FUNCTION
48// Define debugging macros
49#define DEBUG_DISPLAY_ON MDebug::displayOn();
50#define DEBUG_DISPLAY_OFF MDebug::displayOff();
51#define SET_DEBUG_LEVEL(a) MDebug::setLevel(a);
52#define DEBUG(a, b) \
53 do { \
54 std::ostringstream message; \
55 message << a; \
56 MDebug::maiaprint(message.str(), b); \
57 } while(false)
58#define PROFILE(id, a, b)
59#define TRACE() maia::debug::Tracer USE_ONLY_ONCE_PER_METHOD(FUN_, LOC_)
60#define TRACE_PROFILE() TRACE()
61
62#else
63// Reset all profiling/debugging macros
64#define DEBUG_DISPLAY_ON
65#define DEBUG_DISPLAY_OFF
66#define SET_DEBUG_LEVEL(a) MDebug::setLevelNotEnabled(a)
67#define DEBUG(a, b)
68#define PROFILE(id, a, b)
69#define TRACE()
70#define TRACE_PROFILE()
71#endif
72
73
74// Additional debugging macros
75#ifdef MAIA_DEBUG_FUNCTION
76#define DOUT(a) \
77 do { \
78 std::cerr << "DEBUG: VALUE: '" << #a << "' = '" << a << "' at " << AT_ << std::endl; \
79 } while(false)
80
81#define DOUT_IF(condition, a) \
82 do { \
83 if(condition) { \
84 std::cerr << "DEBUG: VALUE: '" << #a << "' = '" << a << "' at " << AT_ << std::endl; \
85 } \
86 } while(false)
87
88#define DLOC() \
89 do { \
90 std::cerr << "DEBUG: DOMAIN: " << globalDomainId() << "; LOCATION: " << AT_ << std::endl; \
91 } while(false)
92#define DMSG(a) \
93 do { \
94 std::cerr << "DEBUG: " << globalDomainId() << "; MESSAGE: '" << a << "' at " << AT_ << std::endl; \
95 } while(false)
96#else
97// Define empty macros with do-while clause to enfore a semicolon after the
98// macro
99#define DOUT(a) \
100 do { \
101 } while(false)
102#define DOUT_IF(condition, a) \
103 do { \
104 } while(false)
105#define DLOC() \
106 do { \
107 } while(false)
108#define DMSG(a) \
109 do { \
110 } while(false)
111#endif
112
114using MDebugLevel = enum {
115 MAIA_DEBUG_ASSERTION = 1,
116 MAIA_DEBUG_IO = 2,
117 MAIA_DEBUG_ALLOCATION = 4,
118 MAIA_DEBUG_TRACE = 8,
119 MAIA_DEBUG_TRACE_IN = 16,
120 MAIA_DEBUG_TRACE_OUT = 32,
121 MAIA_DEBUG_LEVEL1 = 64,
122 MAIA_DEBUG_LEVEL2 = 128,
123 MAIA_DEBUG_LEVEL3 = 256,
124 MAIA_DEBUG_LEVEL4 = 512,
125 MAIA_DEBUG_LEVEL5 = 1024,
126 MAIA_DEBUG_USER1 = 2048,
127 MAIA_DEBUG_USER2 = 4096,
128 MAIA_DEBUG_USER3 = 8192,
129 MAIA_DEBUG_USER4 = 16384,
130 MAIA_DEBUG_USER5 = 32768
131};
132
134
180class MDebug {
181 public:
183
184 static void maiaerror(MString message, MDebugLevel debugLevel);
185 static void maiaprint(const MString& message, MDebugLevel debugLevel);
186 static void setLevel(MInt debugLevel);
187 static void setLevelNotEnabled(const MInt debugLevel);
188 static void displayOn();
189 static void displayOff();
190
191 static const MInt m_minLevel = MAIA_DEBUG_ASSERTION;
192 static const MInt m_maxLevel = MAIA_DEBUG_USER5;
193
194 private:
197 static std::basic_string<char> m_traceSpaces;
198};
199
200inline void MDebug::displayOn() { m_debugOn = true; }
201
202inline void MDebug::displayOff() { m_debugOn = false; }
203
204inline void MDebug::setLevel(MInt debugLevel) {
205 displayOn();
206 m_debugLevel = debugLevel;
207}
208
209inline void MDebug::setLevelNotEnabled(const MInt debugLevel) {
210 if(debugLevel != 0) {
211 cerr0 << "WARNING: trying to set debug level " << debugLevel << " but MAIA_DEBUG_FUNCTION is not enabled."
212 << std::endl;
213 }
214}
215
216inline void MDebug::maiaprint(const MString& message, MDebugLevel debugLevel) {
217 // This function seperates the debug levels and
218 // displays the messages which have the right debug level.
219 if(m_debugOn) {
220 auto a = (MUint)m_maxLevel;
221 auto b = (MUint)m_debugLevel;
222 ldiv_t adiv;
223 do {
224 adiv = ldiv(b, a);
225 if(adiv.quot == 1 && a == (MUint)debugLevel) {
226 if(debugLevel == MAIA_DEBUG_TRACE_OUT) {
227 m_traceSpaces.erase(0, 2);
228 }
229 m_log << m_traceSpaces << "MDebug: "
230 << "[" << globalDomainId() << "]:" << message << std::endl;
231 if(debugLevel == MAIA_DEBUG_TRACE_IN) {
232 m_traceSpaces += " ";
233 }
234 }
235 if(b >= a) {
236 b -= a;
237 }
238 a = a / 2;
239 } while(adiv.rem != 0); // repeat as long as there
240 // is a rest and a level
241 }
242}
243
244#if(defined(MAIA_DEBUG_FUNCTION) || defined(MAIA_PROFILING))
246namespace maia {
247namespace debug {
248struct Tracer {
249 Tracer(const MString& fun, [[maybe_unused]] const MString& loc, const MInt timingId, const MBool profile = false)
250 : m_timingId(timingId), m_fun(fun), m_profile(profile) {
251#ifdef MAIA_PROFILING
252 if(profile) {
253 PROFILE(timingId, fun, MAIA_DEBUG_TRACE_IN);
254 } else {
255 DEBUG(fun + " entry (" + loc + ")", MAIA_DEBUG_TRACE_IN);
256 }
257#else
258 DEBUG(fun + " entry (" + loc + ")", MAIA_DEBUG_TRACE_IN);
259#endif
260 }
262#ifdef MAIA_PROFILING
263 if(m_profile) {
264 PROFILE(m_timingId, m_fun, MAIA_DEBUG_TRACE_OUT);
265 } else {
266 DEBUG(m_fun << " return", MAIA_DEBUG_TRACE_OUT);
267 }
268#else
269 DEBUG(m_fun << " return", MAIA_DEBUG_TRACE_OUT);
270#endif
271 }
272
273 private:
274 const MInt m_timingId = -1;
275 const MString m_fun{};
276 const MBool m_profile = false;
277};
278} // namespace debug
279} // namespace maia
280#endif
281
282#endif // DEBUG_H
The DEBUG class for the maia.
Definition: debug.h:180
static void setLevelNotEnabled(const MInt debugLevel)
Definition: debug.h:209
static MBool m_debugOn
Definition: debug.h:196
static MInt m_debugLevel
Definition: debug.h:195
static const MInt m_minLevel
Definition: debug.h:191
static void maiaerror(MString message, MDebugLevel debugLevel)
static MString debugMessage
Definition: debug.h:182
static std::basic_string< char > m_traceSpaces
Definition: debug.h:197
static const MInt m_maxLevel
Definition: debug.h:192
static void maiaprint(const MString &message, MDebugLevel debugLevel)
Definition: debug.h:216
static void displayOn()
Definition: debug.h:200
static void displayOff()
Definition: debug.h:202
static void setLevel(MInt debugLevel)
Definition: debug.h:204
enum { MAIA_DEBUG_ASSERTION=1, MAIA_DEBUG_IO=2, MAIA_DEBUG_ALLOCATION=4, MAIA_DEBUG_TRACE=8, MAIA_DEBUG_TRACE_IN=16, MAIA_DEBUG_TRACE_OUT=32, MAIA_DEBUG_LEVEL1=64, MAIA_DEBUG_LEVEL2=128, MAIA_DEBUG_LEVEL3=256, MAIA_DEBUG_LEVEL4=512, MAIA_DEBUG_LEVEL5=1024, MAIA_DEBUG_USER1=2048, MAIA_DEBUG_USER2=4096, MAIA_DEBUG_USER3=8192, MAIA_DEBUG_USER4=16384, MAIA_DEBUG_USER5=32768 } MDebugLevel
This enum holds the error levels of the DEBUG class.
Definition: debug.h:131
const MString const MString & message
Definition: functions.h:37
MInt globalDomainId()
Return global domain id.
InfoOutFile m_log
std::ostream cerr0
int32_t MInt
Definition: maiatypes.h:62
uint32_t MUint
Definition: maiatypes.h:63
std::basic_string< char > MString
Definition: maiatypes.h:55
bool MBool
Definition: maiatypes.h:58
Namespace for auxiliary functions/classes.
Definition: contexttypes.h:19
Tracer(const MString &fun, const MString &loc, const MInt timingId, const MBool profile=false)
Definition: debug.h:249
const MInt m_timingId
Definition: debug.h:274
const MBool m_profile
Definition: debug.h:276
const MString m_fun
Definition: debug.h:275