uvcc
libuv C++ bindings
debug.hpp
1 
2 #ifndef UVCC_DEBUG__HPP
3 #define UVCC_DEBUG__HPP
4 //! \cond
5 
6 #include <cstddef> // ptrdiff_t
7 #include <cstdio>
8 #include <uv.h>
9 
10 
11 #ifdef UVCC_DEBUG
12 
13 #define MAKE_LITERAL_STRING_VERBATIM(...) #__VA_ARGS__
14 #define MAKE_LITERAL_STRING_AFTER_EXPANDING(...) MAKE_LITERAL_STRING_VERBATIM(__VA_ARGS__)
15 
16 #define CONCATENATE_VERBATIM(left, right) left ## right
17 #define CONCATENATE_AFTER_EXPANDING(left, right) CONCATENATE_VERBATIM(left, right)
18 
19 
20 #define uvcc_debug_log_if(log_condition, printf_args...) do {
21  if ((log_condition))
22  {
23  std::fflush(stdout);
24  std::fprintf(stderr, "[debug]");
25  int n = std::fprintf(stderr, " " printf_args);
26  if (n > 1)
27  std::fprintf(stderr, "\n");
28  else
29  std::fprintf(stderr, "log: function=%s file=%s line=%i\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
30  std::fflush(stderr);
31  }\
32 }while (0)
33 
34 #define uvcc_debug_function_enter(printf_args...) do {
35  std::fflush(stdout);
36  std::fprintf(stderr, "[debug] enter function: %s", __PRETTY_FUNCTION__);
37  if (sizeof(MAKE_LITERAL_STRING_AFTER_EXPANDING(printf_args)) > 1) std::fprintf(stderr, ": " printf_args);
38  std::fprintf(stderr, "\n");
39  std::fflush(stderr);\
40 }while (0)
41 
42 #define uvcc_debug_function_return(printf_args...) do {
43  std::fflush(stdout);
44  std::fprintf(stderr, "[debug] return from function: %s", __PRETTY_FUNCTION__);
45  if (sizeof(MAKE_LITERAL_STRING_AFTER_EXPANDING(printf_args)) > 1) std::fprintf(stderr, ": " printf_args);
46  std::fprintf(stderr, "\n");
47  std::fflush(stderr);\
48 }while (0)
49 
50 #define uvcc_debug_condition(condition, context_printf_args...) do {
51  bool c = (condition);
52  std::fflush(stdout);
53  std::fprintf(stderr, "[debug] condition (%s):", MAKE_LITERAL_STRING_VERBATIM(condition));
54  int n = std::fprintf(stderr, " " context_printf_args);
55  std::fprintf(stderr, "%s%s\n", (n > 1)?": ":"", c?"TRUE":"false");
56  std::fflush(stderr);\
57 }while (0)
58 
59 #define uvcc_debug_do_if(log_condition, operators...) do {
60  if ((log_condition))
61  {
62  operators;
63  }\
64 }while(0)
65 
66 #else
67 
68 #define uvcc_debug_log_if(log_condition, ...) (void)(log_condition)
69 #define uvcc_debug_function_enter(...)
70 #define uvcc_debug_function_return(...)
71 #define uvcc_debug_condition(condition, ...) (void)(condition)
72 #define uvcc_debug_do_if(log_condition, ...) (void)(log_condition)
73 
74 #endif
75 
76 
77 #pragma GCC diagnostic push
78 #pragma GCC diagnostic ignored "-Wunused-function"
79 namespace uv
80 {
81 namespace debug
82 {
83 
84 
85 template< class _UVCC_CLASS_ >
86 typename _UVCC_CLASS_::instance* instance(_UVCC_CLASS_ &_var) noexcept
87 {
88  return _UVCC_CLASS_::instance::from(static_cast< typename _UVCC_CLASS_::uv_t* >(_var));
89 }
90 
91 static
92 const char* handle_type_name(::uv_handle_t *_uv_handle) noexcept
93 {
94  const char *ret;
95  switch (_uv_handle->type)
96  {
97  case UV_UNKNOWN_HANDLE: ret = "UNKNOWN"; break;
98 #define XX(X, x) case UV_##X: ret = #x; break;
99  UV_HANDLE_TYPE_MAP(XX)
100 #undef XX
101  case UV_FILE: ret = "file"; break;
102  default: ret = "<UNDEFINED>"; break;
103  }
104  return ret;
105 }
106 
107 static
108 void print_handle(::uv_handle_t* _uv_handle) noexcept
109 {
110  std::fprintf(stderr,
111  "[debug] %s handle [0x%08tX]: has_ref=%i is_active=%i is_closing=%i\n",
112  handle_type_name(_uv_handle), (ptrdiff_t)_uv_handle,
113  ::uv_has_ref(_uv_handle), ::uv_is_active(_uv_handle), ::uv_is_closing(_uv_handle)
114  );
115  std::fflush(stderr);
116 }
117 
118 static
119 void print_loop_handles(::uv_loop_t *_uv_loop)
120 {
121  std::fprintf(stderr, "[debug] handles associated with loop [0x%08tX]: {\n", (ptrdiff_t)_uv_loop);
122  ::uv_walk(
123  static_cast< ::uv_loop_t* >(_uv_loop),
124  [](::uv_handle_t *_h, void*){ print_handle(_h); },
125  nullptr
126  );
127  std::fprintf(stderr, "[debug] }\n");
128 }
129 
130 
131 }
132 }
133 #pragma GCC diagnostic pop
134 
135 //! \endcond
136 #endif
Namespace for all uvcc definitions.
Definition: buffer.hpp:17