NexusCPP  3.5.0
nxdebug.h
Go to the documentation of this file.
1 //*****************************************************************************
17 //*****************************************************************************
18 
19 #ifndef __NX_TOOLS_H__
20 #define __NX_TOOLS_H__
21 
22 // standard library objets
23 #include <iostream>
24 #include <iomanip>
25 #include <vector>
26 
27 // YAT
28 #include <yat/threading/Utilities.h>
29 #include <yat/threading/Mutex.h>
30 
31 namespace nxcpp
32 {
33 
34 #ifdef NX_DEBUG
35  #define DBG_PREFIX(x) "[" << std::hex << std::setfill(' ') << std::setw(10)\
36  << (void*)(x) << "][" << std::setfill('0') << std::setw(8)\
37  << yat::ThreadingUtilities::self() << "] " << std::dec
38 
39  class dbg_helper
40  {
41  private:
42  std::string _s;
43  void *_this_object;
44  public:
45  dbg_helper(const std::string &s, void* this_object) : _s(s), _this_object(this_object)
46  {
47  yat::AutoMutex<> lock(mutex());
48  std::cout << DBG_PREFIX(_this_object) << indent() << "> " << _s << std::endl;
49  indent().append(2, ' ');
50  }
51  ~dbg_helper()
52  {
53  yat::AutoMutex<> lock(mutex());
54  indent().erase(0,2);
55  std::cout << DBG_PREFIX(_this_object) << indent() << "< " << _s << std::endl;
56  }
57  static std::string& indent()
58  {
59  static std::string s_indent;
60  return s_indent;
61  }
62  static yat::Mutex& mutex()
63  {
64  static yat::Mutex s_mtx;
65  return s_mtx;
66  }
67  };
68  #define NX_SCOPE_DBG(s) dbg_helper _scope_dbg(s, (void*)this)
69  #define NX_DBG(s) \
70  { \
71  yat::AutoMutex<> lock(dbg_helper::mutex()); \
72  std::cout << DBG_PREFIX((void*)this) << dbg_helper::indent() << s << std::endl; \
73  }
74 
75 #else
76  #define NX_DBG(s)
77  #define NX_SCOPE_DBG(s)
78 #endif
79 
80 }
81 
82 #endif
Definition: nxdebug.h:31