forked from trackreco/mkFit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Debug.h
74 lines (56 loc) · 2.08 KB
/
Debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef _debug_
#ifdef DEBUG
#define _debug_
#ifdef dprint
#undef dprint
#undef dprint_np
#undef dcall
#undef dprintf
#undef dprintf_np
#endif
/*
Usage: DEBUG must be defined before this header file is included, typically
#define DEBUG
#include "Debug.h"
This defines macros dprint(), dcall() and dprintf();
dprint(x) is equivalent to std::cout << x << std::endl;
example: dprint("Hits in layer=" << ilayer);
dcall(x) simply calls x
example: dcall(pre_prop_print(ilay, mkfp));
dprintf(x) is equivalent to printf(x)
example: dprintf("Bad label for simtrack %d -- %d\n", itrack, track.label());
All printouts are also controlled by a bool variable "debug"
bool debug = true; is declared as a file global in an anonymous
namespace, and thus can be overridden within any interior scope
as needed, so one could change the global to false and only set
a local to true within certain scopes.
All are protected by a file scope mutex to avoid mixed printouts.
This mutex can also be acquired within a block via dmutex_guard:
if (debug) {
dmutex_guard;
[do complicated stuff]
}
The mutex is not reentrant, so avoid using dprint et al. within a scope
where the mutex has already been acquired, as doing so will deadlock.
*/
#include <mutex>
#define dmutex_guard std::lock_guard<std::mutex> dlock(debug_mutex)
#define dprint(x) if (debug) { dmutex_guard; std::cout << x << std::endl; }
#define dprint_np(n,x) if(debug && n < N_proc) { dmutex_guard; std::cout << n << ": " << x << std::endl; }
#define dcall(x) if (debug) { dmutex_guard; x; }
#define dprintf(...) if (debug) { dmutex_guard; printf(__VA_ARGS__); }
#define dprintf_np(n, ...) if (debug && n < N_proc) { dmutex_guard; std::cout << n << ": "; printf(__VA_ARGS__); }
namespace
{
bool debug = false; // default, can be overridden locally
std::mutex debug_mutex;
struct foo_doo_debug_unused { void foo() { debug = true; } };
}
#else
#define dprint(x) (void(0))
#define dprint_np(n,x) (void(0))
#define dcall(x) (void(0))
#define dprintf(...) (void(0))
#define dprintf_np(n,...) (void(0))
#endif
#endif