2323#include < cstdio>
2424#include " asio/detail/handler_tracking.hpp"
2525
26- #include " asio/detail/push_options.hpp"
27- #include < boost/date_time/posix_time/posix_time_types.hpp>
28- #include " asio/detail/pop_options.hpp"
26+ #if defined(ASIO_HAS_BOOST_DATE_TIME)
27+ # include " asio/time_traits.hpp"
28+ #else // defined(ASIO_HAS_BOOST_DATE_TIME)
29+ # if defined(ASIO_HAS_STD_CHRONO)
30+ # include < chrono>
31+ # elif defined(ASIO_HAS_BOOST_CHRONO)
32+ # include < boost/chrono/system_clocks.hpp>
33+ # endif
34+ # include " asio/detail/chrono_time_traits.hpp"
35+ # include " asio/wait_traits.hpp"
36+ #endif // defined(ASIO_HAS_BOOST_DATE_TIME)
2937
3038#if !defined(ASIO_WINDOWS)
3139# include < unistd.h>
3644namespace asio {
3745namespace detail {
3846
47+ struct handler_tracking_timestamp
48+ {
49+ uint64_t seconds;
50+ uint64_t microseconds;
51+
52+ handler_tracking_timestamp ()
53+ {
54+ #if defined(ASIO_HAS_BOOST_DATE_TIME)
55+ boost::posix_time::ptime epoch (boost::gregorian::date (1970 , 1 , 1 ));
56+ boost::posix_time::time_duration now =
57+ boost::posix_time::microsec_clock::universal_time () - epoch;
58+ #elif defined(ASIO_HAS_STD_CHRONO)
59+ typedef chrono_time_traits<std::chrono::system_clock,
60+ asio::wait_traits<std::chrono::system_clock> > traits_helper;
61+ traits_helper::posix_time_duration now (
62+ std::chrono::system_clock::now ().time_since_epoch ());
63+ #elif defined(ASIO_HAS_BOOST_CHRONO)
64+ typedef chrono_time_traits<boost::chrono::system_clock,
65+ asio::wait_traits<boost::chrono::system_clock> > traits_helper;
66+ traits_helper::posix_time_duration now (
67+ boost::chrono::system_clock::now ().time_since_epoch ());
68+ #endif
69+ seconds = static_cast <uint64_t >(now.total_seconds ());
70+ microseconds = static_cast <uint64_t >(now.total_microseconds () % 1000000 );
71+ }
72+ };
73+
3974struct handler_tracking ::tracking_state
4075{
4176 static_mutex mutex_;
@@ -69,9 +104,7 @@ void handler_tracking::creation(handler_tracking::tracked_handler* h,
69104 h->id_ = state->next_id_ ++;
70105 lock.unlock ();
71106
72- boost::posix_time::ptime epoch (boost::gregorian::date (1970 , 1 , 1 ));
73- boost::posix_time::time_duration now =
74- boost::posix_time::microsec_clock::universal_time () - epoch;
107+ handler_tracking_timestamp timestamp;
75108
76109 uint64_t current_id = 0 ;
77110 if (completion* current_completion = *state->current_completion_ )
@@ -83,8 +116,7 @@ void handler_tracking::creation(handler_tracking::tracked_handler* h,
83116#else // defined(ASIO_WINDOWS)
84117 " @asio|%llu.%06llu|%llu*%llu|%.20s@%p.%.50s\n " ,
85118#endif // defined(ASIO_WINDOWS)
86- static_cast <uint64_t >(now.total_seconds ()),
87- static_cast <uint64_t >(now.total_microseconds () % 1000000 ),
119+ timestamp.seconds , timestamp.microseconds ,
88120 current_id, h->id_ , object_type, object, op_name);
89121}
90122
@@ -100,18 +132,15 @@ handler_tracking::completion::~completion()
100132{
101133 if (id_)
102134 {
103- boost::posix_time::ptime epoch (boost::gregorian::date (1970 , 1 , 1 ));
104- boost::posix_time::time_duration now =
105- boost::posix_time::microsec_clock::universal_time () - epoch;
135+ handler_tracking_timestamp timestamp;
106136
107137 write_line (
108138#if defined(ASIO_WINDOWS)
109139 " @asio|%I64u.%06I64u|%c%I64u|\n " ,
110140#else // defined(ASIO_WINDOWS)
111141 " @asio|%llu.%06llu|%c%llu|\n " ,
112142#endif // defined(ASIO_WINDOWS)
113- static_cast <uint64_t >(now.total_seconds ()),
114- static_cast <uint64_t >(now.total_microseconds () % 1000000 ),
143+ timestamp.seconds , timestamp.microseconds ,
115144 invoked_ ? ' !' : ' ~' , id_);
116145 }
117146
@@ -120,37 +149,31 @@ handler_tracking::completion::~completion()
120149
121150void handler_tracking::completion::invocation_begin ()
122151{
123- boost::posix_time::ptime epoch (boost::gregorian::date (1970 , 1 , 1 ));
124- boost::posix_time::time_duration now =
125- boost::posix_time::microsec_clock::universal_time () - epoch;
152+ handler_tracking_timestamp timestamp;
126153
127154 write_line (
128155#if defined(ASIO_WINDOWS)
129156 " @asio|%I64u.%06I64u|>%I64u|\n " ,
130157#else // defined(ASIO_WINDOWS)
131158 " @asio|%llu.%06llu|>%llu|\n " ,
132159#endif // defined(ASIO_WINDOWS)
133- static_cast <uint64_t >(now.total_seconds ()),
134- static_cast <uint64_t >(now.total_microseconds () % 1000000 ), id_);
160+ timestamp.seconds , timestamp.microseconds );
135161
136162 invoked_ = true ;
137163}
138164
139165void handler_tracking::completion::invocation_begin (
140166 const asio::error_code& ec)
141167{
142- boost::posix_time::ptime epoch (boost::gregorian::date (1970 , 1 , 1 ));
143- boost::posix_time::time_duration now =
144- boost::posix_time::microsec_clock::universal_time () - epoch;
168+ handler_tracking_timestamp timestamp;
145169
146170 write_line (
147171#if defined(ASIO_WINDOWS)
148172 " @asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d\n " ,
149173#else // defined(ASIO_WINDOWS)
150174 " @asio|%llu.%06llu|>%llu|ec=%.20s:%d\n " ,
151175#endif // defined(ASIO_WINDOWS)
152- static_cast <uint64_t >(now.total_seconds ()),
153- static_cast <uint64_t >(now.total_microseconds () % 1000000 ),
176+ timestamp.seconds , timestamp.microseconds ,
154177 id_, ec.category ().name (), ec.value ());
155178
156179 invoked_ = true ;
@@ -159,18 +182,15 @@ void handler_tracking::completion::invocation_begin(
159182void handler_tracking::completion::invocation_begin (
160183 const asio::error_code& ec, std::size_t bytes_transferred)
161184{
162- boost::posix_time::ptime epoch (boost::gregorian::date (1970 , 1 , 1 ));
163- boost::posix_time::time_duration now =
164- boost::posix_time::microsec_clock::universal_time () - epoch;
185+ handler_tracking_timestamp timestamp;
165186
166187 write_line (
167188#if defined(ASIO_WINDOWS)
168189 " @asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,bytes_transferred=%I64u\n " ,
169190#else // defined(ASIO_WINDOWS)
170191 " @asio|%llu.%06llu|>%llu|ec=%.20s:%d,bytes_transferred=%llu\n " ,
171192#endif // defined(ASIO_WINDOWS)
172- static_cast <uint64_t >(now.total_seconds ()),
173- static_cast <uint64_t >(now.total_microseconds () % 1000000 ),
193+ timestamp.seconds , timestamp.microseconds ,
174194 id_, ec.category ().name (), ec.value (),
175195 static_cast <uint64_t >(bytes_transferred));
176196
@@ -180,18 +200,15 @@ void handler_tracking::completion::invocation_begin(
180200void handler_tracking::completion::invocation_begin (
181201 const asio::error_code& ec, int signal_number)
182202{
183- boost::posix_time::ptime epoch (boost::gregorian::date (1970 , 1 , 1 ));
184- boost::posix_time::time_duration now =
185- boost::posix_time::microsec_clock::universal_time () - epoch;
203+ handler_tracking_timestamp timestamp;
186204
187205 write_line (
188206#if defined(ASIO_WINDOWS)
189207 " @asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,signal_number=%d\n " ,
190208#else // defined(ASIO_WINDOWS)
191209 " @asio|%llu.%06llu|>%llu|ec=%.20s:%d,signal_number=%d\n " ,
192210#endif // defined(ASIO_WINDOWS)
193- static_cast <uint64_t >(now.total_seconds ()),
194- static_cast <uint64_t >(now.total_microseconds () % 1000000 ),
211+ timestamp.seconds , timestamp.microseconds ,
195212 id_, ec.category ().name (), ec.value (), signal_number);
196213
197214 invoked_ = true ;
@@ -200,18 +217,15 @@ void handler_tracking::completion::invocation_begin(
200217void handler_tracking::completion::invocation_begin (
201218 const asio::error_code& ec, const char * arg)
202219{
203- boost::posix_time::ptime epoch (boost::gregorian::date (1970 , 1 , 1 ));
204- boost::posix_time::time_duration now =
205- boost::posix_time::microsec_clock::universal_time () - epoch;
220+ handler_tracking_timestamp timestamp;
206221
207222 write_line (
208223#if defined(ASIO_WINDOWS)
209224 " @asio|%I64u.%06I64u|>%I64u|ec=%.20s:%d,%.50s\n " ,
210225#else // defined(ASIO_WINDOWS)
211226 " @asio|%llu.%06llu|>%llu|ec=%.20s:%d,%.50s\n " ,
212227#endif // defined(ASIO_WINDOWS)
213- static_cast <uint64_t >(now.total_seconds ()),
214- static_cast <uint64_t >(now.total_microseconds () % 1000000 ),
228+ timestamp.seconds , timestamp.microseconds ,
215229 id_, ec.category ().name (), ec.value (), arg);
216230
217231 invoked_ = true ;
@@ -221,18 +235,15 @@ void handler_tracking::completion::invocation_end()
221235{
222236 if (id_)
223237 {
224- boost::posix_time::ptime epoch (boost::gregorian::date (1970 , 1 , 1 ));
225- boost::posix_time::time_duration now =
226- boost::posix_time::microsec_clock::universal_time () - epoch;
238+ handler_tracking_timestamp timestamp;
227239
228240 write_line (
229241#if defined(ASIO_WINDOWS)
230242 " @asio|%I64u.%06I64u|<%I64u|\n " ,
231243#else // defined(ASIO_WINDOWS)
232244 " @asio|%llu.%06llu|<%llu|\n " ,
233245#endif // defined(ASIO_WINDOWS)
234- static_cast <uint64_t >(now.total_seconds ()),
235- static_cast <uint64_t >(now.total_microseconds () % 1000000 ), id_);
246+ timestamp.seconds , timestamp.microseconds );
236247
237248 id_ = 0 ;
238249 }
@@ -243,9 +254,7 @@ void handler_tracking::operation(const char* object_type,
243254{
244255 static tracking_state* state = get_state ();
245256
246- boost::posix_time::ptime epoch (boost::gregorian::date (1970 , 1 , 1 ));
247- boost::posix_time::time_duration now =
248- boost::posix_time::microsec_clock::universal_time () - epoch;
257+ handler_tracking_timestamp timestamp;
249258
250259 unsigned long long current_id = 0 ;
251260 if (completion* current_completion = *state->current_completion_ )
@@ -257,8 +266,7 @@ void handler_tracking::operation(const char* object_type,
257266#else // defined(ASIO_WINDOWS)
258267 " @asio|%llu.%06llu|%llu|%.20s@%p.%.50s\n " ,
259268#endif // defined(ASIO_WINDOWS)
260- static_cast <uint64_t >(now.total_seconds ()),
261- static_cast <uint64_t >(now.total_microseconds () % 1000000 ),
269+ timestamp.seconds , timestamp.microseconds ,
262270 current_id, object_type, object, op_name);
263271}
264272
0 commit comments