CMS 3D CMS Logo

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