handler_tracking.ipp 8.7 KB

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