From f342f8a4c6bbcfb89bd440565bac02cd077ac870 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Mon, 23 Dec 2019 13:33:35 +0000 Subject: [PATCH] coap_debug.c: Extend date+time to include millisecs reporting This helps to debug timing issues such as DTLS retransmits. --- src/coap_debug.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/coap_debug.c b/src/coap_debug.c index 3ed0e4a7..92937aca 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -77,9 +77,15 @@ static const char *loglevels[] = { COAP_STATIC_INLINE size_t print_timestamp(char *s, size_t len, coap_tick_t t) { struct tm *tmp; + size_t lensofar; time_t now = coap_ticks_to_rt(t); tmp = localtime(&now); - return strftime(s, len, "%b %d %H:%M:%S", tmp); + lensofar = strftime(s, len, "%b %d %H:%M:%S", tmp); + if (len > lensofar + 4) { + lensofar += snprintf(&s[lensofar], len-lensofar, ".%03u", + (unsigned int)((coap_ticks_to_rt_us(t) % 1000000)/1000)); + } + return lensofar; } #else /* alternative implementation: just print the timestamp */ @@ -89,7 +95,7 @@ print_timestamp(char *s, size_t len, coap_tick_t t) { #ifdef HAVE_SNPRINTF return snprintf(s, len, "%u.%03u", (unsigned int)coap_ticks_to_rt(t), - (unsigned int)(t % COAP_TICKS_PER_SECOND)); + (unsigned int)((coap_ticks_to_rt_us(t) % 1000000)/1000)); #else /* HAVE_SNPRINTF */ /* @todo do manual conversion of timestamp */ return 0;