CMS 3D CMS Logo

Debug.h
Go to the documentation of this file.
1 #ifndef RecoTracker_MkFitCore_src_Debug_h
2 
3 namespace mkfit {
4  extern bool g_debug;
5 }
6 
7 #ifdef DEBUG
8 #define RecoTracker_MkFitCore_src_Debug_h
9 
10 #ifdef dprint
11 
12 #undef dprint
13 #undef dprint_np
14 #undef dcall
15 #undef dprintf
16 #undef dprintf_np
17 
18 #endif
19 /*
20  Usage: DEBUG must be defined before this header file is included, typically
21 
22  #define DEBUG
23  #include "Debug.h"
24 
25  This defines macros dprint(), dcall() and dprintf();
26  dprint(x) is equivalent to std::cout << x << std::endl;
27  example: dprint("Hits in layer=" << ilayer);
28 
29  dcall(x) simply calls x
30  example: dcall(pre_prop_print(ilay, mkfp));
31 
32  dprintf(x) is equivalent to printf(x)
33  example: dprintf("Bad label for simtrack %d -- %d\n", itrack, track.label());
34 
35  All printouts are also controlled by a bool variable "debug"
36  bool debug = true; is declared as a file global in an anonymous
37  namespace, and thus can be overridden within any interior scope
38  as needed, so one could change the global to false and only set
39  a local to true within certain scopes.
40 
41  All are protected by a file scope mutex to avoid mixed printouts.
42  This mutex can also be acquired within a block via dmutex_guard:
43 
44  if (debug && g_debug) {
45  dmutex_guard;
46  [do complicated stuff]
47  }
48 
49  The mutex is not reentrant, so avoid using dprint et al. within a scope
50  where the mutex has already been acquired, as doing so will deadlock.
51  */
52 #include <mutex>
53 
54 #define dmutex_guard std::lock_guard<std::mutex> dlock(debug_mutex)
55 #define dprint(x) \
56  if (debug && g_debug) { \
57  dmutex_guard; \
58  std::cout << x << std::endl; \
59  }
60 #define dprint_np(n, x) \
61  if (debug && g_debug && n < N_proc) { \
62  dmutex_guard; \
63  std::cout << n << ": " << x << std::endl; \
64  }
65 #define dcall(x) \
66  if (debug && g_debug) { \
67  dmutex_guard; \
68  x; \
69  }
70 #define dprintf(...) \
71  if (debug && g_debug) { \
72  dmutex_guard; \
73  printf(__VA_ARGS__); \
74  }
75 #define dprintf_np(n, ...) \
76  if (debug && g_debug && n < N_proc) { \
77  dmutex_guard; \
78  std::cout << n << ": "; \
79  printf(__VA_ARGS__); \
80  }
81 
82 namespace {
83  bool debug = false; // default, can be overridden locally
84  std::mutex debug_mutex;
85 
86  struct debug_guard {
87  bool m_prev_debug;
88  debug_guard(bool state = true) : m_prev_debug(debug) { debug = state; }
89  ~debug_guard() { debug = m_prev_debug; }
90  };
91 } // namespace
92 
93 #else
94 
95 #define dprint(x) (void(0))
96 #define dprint_np(n, x) (void(0))
97 #define dcall(x) (void(0))
98 #define dprintf(...) (void(0))
99 #define dprintf_np(n, ...) (void(0))
100 
101 #endif
102 
103 #endif
static std::mutex mutex
Definition: Proxy.cc:8
bool g_debug
Definition: Debug.cc:2
#define debug
Definition: HDRShower.cc:19