2 // detail/impl/handler_tracking.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_IMPL_HANDLER_TRACKING_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_HANDLER_TRACKING_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_CUSTOM_HANDLER_TRACKING)
22 // The handler tracking implementation is provided by the user-specified header.
24 #elif defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
28 #include <boost/asio/detail/handler_tracking.hpp>
30 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
31 # include <boost/asio/time_traits.hpp>
32 #elif defined(BOOST_ASIO_HAS_CHRONO)
33 # include <boost/asio/detail/chrono.hpp>
34 # include <boost/asio/detail/chrono_time_traits.hpp>
35 # include <boost/asio/wait_traits.hpp>
36 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
38 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
39 # include <boost/asio/detail/socket_types.hpp>
40 #elif !defined(BOOST_ASIO_WINDOWS)
42 #endif // !defined(BOOST_ASIO_WINDOWS)
44 #include <boost/asio/detail/push_options.hpp>
50 struct handler_tracking_timestamp
53 uint64_t microseconds;
55 handler_tracking_timestamp()
57 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
58 boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
59 boost::posix_time::time_duration now =
60 boost::posix_time::microsec_clock::universal_time() - epoch;
61 #elif defined(BOOST_ASIO_HAS_CHRONO)
62 typedef chrono_time_traits<chrono::system_clock,
63 boost::asio::wait_traits<chrono::system_clock> > traits_helper;
64 traits_helper::posix_time_duration now(
65 chrono::system_clock::now().time_since_epoch());
67 seconds = static_cast<uint64_t>(now.total_seconds());
68 microseconds = static_cast<uint64_t>(now.total_microseconds() % 1000000);
72 struct handler_tracking::tracking_state
76 tss_ptr<completion>* current_completion_;
79 handler_tracking::tracking_state* handler_tracking::get_state()
81 static tracking_state state = { BOOST_ASIO_STATIC_MUTEX_INIT, 1, 0 };
85 void handler_tracking::init()
87 static tracking_state* state = get_state();
91 static_mutex::scoped_lock lock(state->mutex_);
92 if (state->current_completion_ == 0)
93 state->current_completion_ = new tss_ptr<completion>;
96 void handler_tracking::creation(execution_context&,
97 handler_tracking::tracked_handler& h,
98 const char* object_type, void* object,
99 uintmax_t /*native_handle*/, const char* op_name)
101 static tracking_state* state = get_state();
103 static_mutex::scoped_lock lock(state->mutex_);
104 h.id_ = state->next_id_++;
107 handler_tracking_timestamp timestamp;
109 uint64_t current_id = 0;
110 if (completion* current_completion = *state->current_completion_)
111 current_id = current_completion->id_;
114 #if defined(BOOST_ASIO_WINDOWS)
115 "@asio|%I64u.%06I64u|%I64u*%I64u|%.20s@%p.%.50s\n",
116 #else // defined(BOOST_ASIO_WINDOWS)
117 "@asio|%llu.%06llu|%llu*%llu|%.20s@%p.%.50s\n",
118 #endif // defined(BOOST_ASIO_WINDOWS)
119 timestamp.seconds, timestamp.microseconds,
120 current_id, h.id_, object_type, object, op_name);
123 handler_tracking::completion::completion(
124 const handler_tracking::tracked_handler& h)
127 next_(*get_state()->current_completion_)
129 *get_state()->current_completion_ = this;
132 handler_tracking::completion::~completion()
136 handler_tracking_timestamp timestamp;
139 #if defined(BOOST_ASIO_WINDOWS)
140 "@asio|%I64u.%06I64u|%c%I64u|\n",
141 #else // defined(BOOST_ASIO_WINDOWS)
142 "@asio|%llu.%06llu|%c%llu|\n",
143 #endif // defined(BOOST_ASIO_WINDOWS)
144 timestamp.seconds, timestamp.microseconds,
145 invoked_ ? '!' : '~', id_);
148 *get_state()->current_completion_ = next_;
151 void handler_tracking::completion::invocation_begin()
153 handler_tracking_timestamp timestamp;
156 #if defined(BOOST_ASIO_WINDOWS)
157 "@asio|%I64u.%06I64u|>%I64u|\n",
158 #else // defined(BOOST_ASIO_WINDOWS)
159 "@asio|%llu.%06llu|>%llu|\n",
160 #endif // defined(BOOST_ASIO_WINDOWS)
161 timestamp.seconds, timestamp.microseconds, id_);
166 void handler_tracking::completion::invocation_begin(
167 const boost::system::error_code& ec)
169 handler_tracking_timestamp timestamp;
172 #if defined(BOOST_ASIO_WINDOWS)
173 "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d\n",
174 #else // defined(BOOST_ASIO_WINDOWS)
175 "@asio|%llu.%06llu|>%llu|ec=%.20s:%d\n",
176 #endif // defined(BOOST_ASIO_WINDOWS)
177 timestamp.seconds, timestamp.microseconds,
178 id_, ec.category().name(), ec.value());
183 void handler_tracking::completion::invocation_begin(
184 const boost::system::error_code& ec, std::size_t bytes_transferred)
186 handler_tracking_timestamp timestamp;
189 #if defined(BOOST_ASIO_WINDOWS)
190 "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,bytes_transferred=%I64u\n",
191 #else // defined(BOOST_ASIO_WINDOWS)
192 "@asio|%llu.%06llu|>%llu|ec=%.20s:%d,bytes_transferred=%llu\n",
193 #endif // defined(BOOST_ASIO_WINDOWS)
194 timestamp.seconds, timestamp.microseconds,
195 id_, ec.category().name(), ec.value(),
196 static_cast<uint64_t>(bytes_transferred));
201 void handler_tracking::completion::invocation_begin(
202 const boost::system::error_code& ec, int signal_number)
204 handler_tracking_timestamp timestamp;
207 #if defined(BOOST_ASIO_WINDOWS)
208 "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,signal_number=%d\n",
209 #else // defined(BOOST_ASIO_WINDOWS)
210 "@asio|%llu.%06llu|>%llu|ec=%.20s:%d,signal_number=%d\n",
211 #endif // defined(BOOST_ASIO_WINDOWS)
212 timestamp.seconds, timestamp.microseconds,
213 id_, ec.category().name(), ec.value(), signal_number);
218 void handler_tracking::completion::invocation_begin(
219 const boost::system::error_code& ec, const char* arg)
221 handler_tracking_timestamp timestamp;
224 #if defined(BOOST_ASIO_WINDOWS)
225 "@asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,%.50s\n",
226 #else // defined(BOOST_ASIO_WINDOWS)
227 "@asio|%llu.%06llu|>%llu|ec=%.20s:%d,%.50s\n",
228 #endif // defined(BOOST_ASIO_WINDOWS)
229 timestamp.seconds, timestamp.microseconds,
230 id_, ec.category().name(), ec.value(), arg);
235 void handler_tracking::completion::invocation_end()
239 handler_tracking_timestamp timestamp;
242 #if defined(BOOST_ASIO_WINDOWS)
243 "@asio|%I64u.%06I64u|<%I64u|\n",
244 #else // defined(BOOST_ASIO_WINDOWS)
245 "@asio|%llu.%06llu|<%llu|\n",
246 #endif // defined(BOOST_ASIO_WINDOWS)
247 timestamp.seconds, timestamp.microseconds, id_);
253 void handler_tracking::operation(execution_context&,
254 const char* object_type, void* object,
255 uintmax_t /*native_handle*/, const char* op_name)
257 static tracking_state* state = get_state();
259 handler_tracking_timestamp timestamp;
261 unsigned long long current_id = 0;
262 if (completion* current_completion = *state->current_completion_)
263 current_id = current_completion->id_;
266 #if defined(BOOST_ASIO_WINDOWS)
267 "@asio|%I64u.%06I64u|%I64u|%.20s@%p.%.50s\n",
268 #else // defined(BOOST_ASIO_WINDOWS)
269 "@asio|%llu.%06llu|%llu|%.20s@%p.%.50s\n",
270 #endif // defined(BOOST_ASIO_WINDOWS)
271 timestamp.seconds, timestamp.microseconds,
272 current_id, object_type, object, op_name);
275 void handler_tracking::reactor_registration(execution_context& /*context*/,
276 uintmax_t /*native_handle*/, uintmax_t /*registration*/)
280 void handler_tracking::reactor_deregistration(execution_context& /*context*/,
281 uintmax_t /*native_handle*/, uintmax_t /*registration*/)
285 void handler_tracking::reactor_events(execution_context& /*context*/,
286 uintmax_t /*native_handle*/, unsigned /*events*/)
290 void handler_tracking::reactor_operation(
291 const tracked_handler& h, const char* op_name,
292 const boost::system::error_code& ec)
294 handler_tracking_timestamp timestamp;
297 #if defined(BOOST_ASIO_WINDOWS)
298 "@asio|%I64u.%06I64u|.%I64u|%s,ec=%.20s:%d\n",
299 #else // defined(BOOST_ASIO_WINDOWS)
300 "@asio|%llu.%06llu|.%llu|%s,ec=%.20s:%d\n",
301 #endif // defined(BOOST_ASIO_WINDOWS)
302 timestamp.seconds, timestamp.microseconds,
303 h.id_, op_name, ec.category().name(), ec.value());
306 void handler_tracking::reactor_operation(
307 const tracked_handler& h, const char* op_name,
308 const boost::system::error_code& ec, std::size_t bytes_transferred)
310 handler_tracking_timestamp timestamp;
313 #if defined(BOOST_ASIO_WINDOWS)
314 "@asio|%I64u.%06I64u|.%I64u|%s,ec=%.20s:%d,bytes_transferred=%I64u\n",
315 #else // defined(BOOST_ASIO_WINDOWS)
316 "@asio|%llu.%06llu|.%llu|%s,ec=%.20s:%d,bytes_transferred=%llu\n",
317 #endif // defined(BOOST_ASIO_WINDOWS)
318 timestamp.seconds, timestamp.microseconds,
319 h.id_, op_name, ec.category().name(), ec.value(),
320 static_cast<uint64_t>(bytes_transferred));
323 void handler_tracking::write_line(const char* format, ...)
325 using namespace std; // For sprintf (or equivalent).
328 va_start(args, format);
331 #if defined(BOOST_ASIO_HAS_SECURE_RTL)
332 int length = vsprintf_s(line, sizeof(line), format, args);
333 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
334 int length = vsprintf(line, format, args);
335 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
339 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
340 wchar_t wline[256] = L"";
341 mbstowcs_s(0, wline, sizeof(wline) / sizeof(wchar_t), line, length);
342 ::OutputDebugStringW(wline);
343 #elif defined(BOOST_ASIO_WINDOWS)
344 HANDLE stderr_handle = ::GetStdHandle(STD_ERROR_HANDLE);
345 DWORD bytes_written = 0;
346 ::WriteFile(stderr_handle, line, length, &bytes_written, 0);
347 #else // defined(BOOST_ASIO_WINDOWS)
348 ::write(STDERR_FILENO, line, length);
349 #endif // defined(BOOST_ASIO_WINDOWS)
352 } // namespace detail
356 #include <boost/asio/detail/pop_options.hpp>
358 #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
360 #endif // BOOST_ASIO_DETAIL_IMPL_HANDLER_TRACKING_IPP