handler_tracking.ipp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // detail/impl/handler_tracking.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_IMPL_HANDLER_TRACKING_IPP
  11. #define ASIO_DETAIL_IMPL_HANDLER_TRACKING_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_ENABLE_HANDLER_TRACKING)
  17. #include <cstdarg>
  18. #include <cstdio>
  19. #include "asio/detail/handler_tracking.hpp"
  20. #if defined(ASIO_HAS_BOOST_DATE_TIME)
  21. # include "asio/time_traits.hpp"
  22. #else // defined(ASIO_HAS_BOOST_DATE_TIME)
  23. # if defined(ASIO_HAS_STD_CHRONO)
  24. # include <chrono>
  25. # elif defined(ASIO_HAS_BOOST_CHRONO)
  26. # include <boost/chrono/system_clocks.hpp>
  27. # endif
  28. # include "asio/detail/chrono_time_traits.hpp"
  29. # include "asio/wait_traits.hpp"
  30. #endif // defined(ASIO_HAS_BOOST_DATE_TIME)
  31. #if !defined(ASIO_WINDOWS)
  32. # include <unistd.h>
  33. #endif // !defined(ASIO_WINDOWS)
  34. #include "asio/detail/push_options.hpp"
  35. namespace asio {
  36. namespace detail {
  37. struct handler_tracking_timestamp
  38. {
  39. uint64_t seconds;
  40. uint64_t microseconds;
  41. handler_tracking_timestamp()
  42. {
  43. #if defined(ASIO_HAS_BOOST_DATE_TIME)
  44. boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
  45. boost::posix_time::time_duration now =
  46. boost::posix_time::microsec_clock::universal_time() - epoch;
  47. #elif defined(ASIO_HAS_STD_CHRONO)
  48. typedef chrono_time_traits<std::chrono::system_clock,
  49. asio::wait_traits<std::chrono::system_clock> > traits_helper;
  50. traits_helper::posix_time_duration now(
  51. std::chrono::system_clock::now().time_since_epoch());
  52. #elif defined(ASIO_HAS_BOOST_CHRONO)
  53. typedef chrono_time_traits<boost::chrono::system_clock,
  54. asio::wait_traits<boost::chrono::system_clock> > traits_helper;
  55. traits_helper::posix_time_duration now(
  56. boost::chrono::system_clock::now().time_since_epoch());
  57. #endif
  58. seconds = static_cast<uint64_t>(now.total_seconds());
  59. microseconds = static_cast<uint64_t>(now.total_microseconds() % 1000000);
  60. }
  61. };
  62. struct handler_tracking::tracking_state
  63. {
  64. static_mutex mutex_;
  65. uint64_t next_id_;
  66. tss_ptr<completion>* current_completion_;
  67. };
  68. handler_tracking::tracking_state* handler_tracking::get_state()
  69. {
  70. static tracking_state state = { ASIO_STATIC_MUTEX_INIT, 1, 0 };
  71. return &state;
  72. }
  73. void handler_tracking::init()
  74. {
  75. static tracking_state* state = get_state();
  76. state->mutex_.init();
  77. static_mutex::scoped_lock lock(state->mutex_);
  78. if (state->current_completion_ == 0)
  79. state->current_completion_ = new tss_ptr<completion>;
  80. }
  81. void handler_tracking::creation(handler_tracking::tracked_handler* h,
  82. const char* object_type, void* object, const char* op_name)
  83. {
  84. static tracking_state* state = get_state();
  85. static_mutex::scoped_lock lock(state->mutex_);
  86. h->id_ = state->next_id_++;
  87. lock.unlock();
  88. handler_tracking_timestamp timestamp;
  89. uint64_t current_id = 0;
  90. if (completion* current_completion = *state->current_completion_)
  91. current_id = current_completion->id_;
  92. write_line(
  93. #if defined(ASIO_WINDOWS)
  94. "@asio|%I64u.%06I64u|%I64u*%I64u|%.20s@%p.%.50s\n",
  95. #else // defined(ASIO_WINDOWS)
  96. "@asio|%llu.%06llu|%llu*%llu|%.20s@%p.%.50s\n",
  97. #endif // defined(ASIO_WINDOWS)
  98. timestamp.seconds, timestamp.microseconds,
  99. current_id, h->id_, object_type, object, op_name);
  100. }
  101. handler_tracking::completion::completion(handler_tracking::tracked_handler* h)
  102. : id_(h->id_),
  103. invoked_(false),
  104. next_(*get_state()->current_completion_)
  105. {
  106. *get_state()->current_completion_ = this;
  107. }
  108. handler_tracking::completion::~completion()
  109. {
  110. if (id_)
  111. {
  112. handler_tracking_timestamp timestamp;
  113. write_line(
  114. #if defined(ASIO_WINDOWS)
  115. "@asio|%I64u.%06I64u|%c%I64u|\n",
  116. #else // defined(ASIO_WINDOWS)
  117. "@asio|%llu.%06llu|%c%llu|\n",
  118. #endif // defined(ASIO_WINDOWS)
  119. timestamp.seconds, timestamp.microseconds,
  120. invoked_ ? '!' : '~', id_);
  121. }
  122. *get_state()->current_completion_ = next_;
  123. }
  124. void handler_tracking::completion::invocation_begin()
  125. {
  126. handler_tracking_timestamp timestamp;
  127. write_line(
  128. #if defined(ASIO_WINDOWS)
  129. "@asio|%I64u.%06I64u|>%I64u|\n",
  130. #else // defined(ASIO_WINDOWS)
  131. "@asio|%llu.%06llu|>%llu|\n",
  132. #endif // defined(ASIO_WINDOWS)
  133. timestamp.seconds, timestamp.microseconds, id_);
  134. invoked_ = true;
  135. }
  136. void handler_tracking::completion::invocation_begin(
  137. const asio::error_code& ec)
  138. {
  139. handler_tracking_timestamp timestamp;
  140. write_line(
  141. #if defined(ASIO_WINDOWS)
  142. "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d\n",
  143. #else // defined(ASIO_WINDOWS)
  144. "@asio|%llu.%06llu|>%llu|ec=%.20s:%d\n",
  145. #endif // defined(ASIO_WINDOWS)
  146. timestamp.seconds, timestamp.microseconds,
  147. id_, ec.category().name(), ec.value());
  148. invoked_ = true;
  149. }
  150. void handler_tracking::completion::invocation_begin(
  151. const asio::error_code& ec, std::size_t bytes_transferred)
  152. {
  153. handler_tracking_timestamp timestamp;
  154. write_line(
  155. #if defined(ASIO_WINDOWS)
  156. "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,bytes_transferred=%I64u\n",
  157. #else // defined(ASIO_WINDOWS)
  158. "@asio|%llu.%06llu|>%llu|ec=%.20s:%d,bytes_transferred=%llu\n",
  159. #endif // defined(ASIO_WINDOWS)
  160. timestamp.seconds, timestamp.microseconds,
  161. id_, ec.category().name(), ec.value(),
  162. static_cast<uint64_t>(bytes_transferred));
  163. invoked_ = true;
  164. }
  165. void handler_tracking::completion::invocation_begin(
  166. const asio::error_code& ec, int signal_number)
  167. {
  168. handler_tracking_timestamp timestamp;
  169. write_line(
  170. #if defined(ASIO_WINDOWS)
  171. "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,signal_number=%d\n",
  172. #else // defined(ASIO_WINDOWS)
  173. "@asio|%llu.%06llu|>%llu|ec=%.20s:%d,signal_number=%d\n",
  174. #endif // defined(ASIO_WINDOWS)
  175. timestamp.seconds, timestamp.microseconds,
  176. id_, ec.category().name(), ec.value(), signal_number);
  177. invoked_ = true;
  178. }
  179. void handler_tracking::completion::invocation_begin(
  180. const asio::error_code& ec, const char* arg)
  181. {
  182. handler_tracking_timestamp timestamp;
  183. write_line(
  184. #if defined(ASIO_WINDOWS)
  185. "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,%.50s\n",
  186. #else // defined(ASIO_WINDOWS)
  187. "@asio|%llu.%06llu|>%llu|ec=%.20s:%d,%.50s\n",
  188. #endif // defined(ASIO_WINDOWS)
  189. timestamp.seconds, timestamp.microseconds,
  190. id_, ec.category().name(), ec.value(), arg);
  191. invoked_ = true;
  192. }
  193. void handler_tracking::completion::invocation_end()
  194. {
  195. if (id_)
  196. {
  197. handler_tracking_timestamp timestamp;
  198. write_line(
  199. #if defined(ASIO_WINDOWS)
  200. "@asio|%I64u.%06I64u|<%I64u|\n",
  201. #else // defined(ASIO_WINDOWS)
  202. "@asio|%llu.%06llu|<%llu|\n",
  203. #endif // defined(ASIO_WINDOWS)
  204. timestamp.seconds, timestamp.microseconds, id_);
  205. id_ = 0;
  206. }
  207. }
  208. void handler_tracking::operation(const char* object_type,
  209. void* object, const char* op_name)
  210. {
  211. static tracking_state* state = get_state();
  212. handler_tracking_timestamp timestamp;
  213. unsigned long long current_id = 0;
  214. if (completion* current_completion = *state->current_completion_)
  215. current_id = current_completion->id_;
  216. write_line(
  217. #if defined(ASIO_WINDOWS)
  218. "@asio|%I64u.%06I64u|%I64u|%.20s@%p.%.50s\n",
  219. #else // defined(ASIO_WINDOWS)
  220. "@asio|%llu.%06llu|%llu|%.20s@%p.%.50s\n",
  221. #endif // defined(ASIO_WINDOWS)
  222. timestamp.seconds, timestamp.microseconds,
  223. current_id, object_type, object, op_name);
  224. }
  225. void handler_tracking::write_line(const char* format, ...)
  226. {
  227. using namespace std; // For sprintf (or equivalent).
  228. va_list args;
  229. va_start(args, format);
  230. char line[256] = "";
  231. #if defined(ASIO_HAS_SECURE_RTL)
  232. int length = vsprintf_s(line, sizeof(line), format, args);
  233. #else // defined(ASIO_HAS_SECURE_RTL)
  234. int length = vsprintf(line, format, args);
  235. #endif // defined(ASIO_HAS_SECURE_RTL)
  236. va_end(args);
  237. #if defined(ASIO_WINDOWS)
  238. HANDLE stderr_handle = ::GetStdHandle(STD_ERROR_HANDLE);
  239. DWORD bytes_written = 0;
  240. ::WriteFile(stderr_handle, line, length, &bytes_written, 0);
  241. #else // defined(ASIO_WINDOWS)
  242. ::write(STDERR_FILENO, line, length);
  243. #endif // defined(ASIO_WINDOWS)
  244. }
  245. } // namespace detail
  246. } // namespace asio
  247. #include "asio/detail/pop_options.hpp"
  248. #endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
  249. #endif // ASIO_DETAIL_IMPL_HANDLER_TRACKING_IPP