Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 10:02:33

0001 #ifndef RecoTracker_MkFitCore_src_Debug_h
0002 
0003 namespace mkfit {
0004   extern bool g_debug;
0005 }
0006 
0007 #ifdef DEBUG
0008 #define RecoTracker_MkFitCore_src_Debug_h
0009 
0010 #ifdef dprint
0011 
0012 #undef dprint
0013 #undef dprint_np
0014 #undef dcall
0015 #undef dprintf
0016 #undef dprintf_np
0017 
0018 #endif
0019 /*
0020   Usage: DEBUG must be defined before this header file is included, typically
0021 
0022   #define DEBUG
0023   #include "Debug.h"
0024 
0025   This defines macros dprint(), dcall() and dprintf();
0026   dprint(x) is equivalent to std::cout << x << std::endl;
0027     example: dprint("Hits in layer=" << ilayer);
0028 
0029   dcall(x) simply calls x
0030     example: dcall(pre_prop_print(ilay, mkfp));
0031 
0032   dprintf(x) is equivalent to printf(x)
0033     example: dprintf("Bad label for simtrack %d -- %d\n", itrack, track.label());
0034 
0035   All printouts are also controlled by a bool variable "debug"
0036   bool debug = true; is declared as a file global in an anonymous
0037   namespace, and thus can be overridden within any interior scope
0038   as needed, so one could change the global to false and only set
0039   a local to true within certain scopes.
0040 
0041   All are protected by a file scope mutex to avoid mixed printouts.
0042   This mutex can also be acquired within a block via dmutex_guard:
0043 
0044   if (debug && g_debug) {
0045     dmutex_guard;
0046     [do complicated stuff]
0047   }
0048 
0049   The mutex is not reentrant, so avoid using dprint et al. within a scope
0050   where the mutex has already been acquired, as doing so will deadlock.
0051  */
0052 #include <mutex>
0053 
0054 #define dmutex_guard std::lock_guard<std::mutex> dlock(debug_mutex)
0055 #define dprint(x)                \
0056   if (debug && g_debug) {        \
0057     dmutex_guard;                \
0058     std::cout << x << std::endl; \
0059   }
0060 #define dprint_np(n, x)                       \
0061   if (debug && g_debug && n < N_proc) {       \
0062     dmutex_guard;                             \
0063     std::cout << n << ": " << x << std::endl; \
0064   }
0065 #define dcall(x)          \
0066   if (debug && g_debug) { \
0067     dmutex_guard;         \
0068     x;                    \
0069   }
0070 #define dprintf(...)      \
0071   if (debug && g_debug) { \
0072     dmutex_guard;         \
0073     printf(__VA_ARGS__);  \
0074   }
0075 #define dprintf_np(n, ...)              \
0076   if (debug && g_debug && n < N_proc) { \
0077     dmutex_guard;                       \
0078     std::cout << n << ": ";             \
0079     printf(__VA_ARGS__);                \
0080   }
0081 
0082 namespace {
0083   bool debug = false;  // default, can be overridden locally
0084   std::mutex debug_mutex;
0085 
0086   struct debug_guard {
0087     bool m_prev_debug;
0088     debug_guard(bool state = true) : m_prev_debug(debug) { debug = state; }
0089     ~debug_guard() { debug = m_prev_debug; }
0090   };
0091 }  // namespace
0092 
0093 #else
0094 
0095 #define dprint(x) (void(0))
0096 #define dprint_np(n, x) (void(0))
0097 #define dcall(x) (void(0))
0098 #define dprintf(...) (void(0))
0099 #define dprintf_np(n, ...) (void(0))
0100 
0101 #endif
0102 
0103 #endif