Skip to content

Commit 68679f6

Browse files
committed
Optional nanosecond timestamp logging
1 parent 6b46063 commit 68679f6

File tree

4 files changed

+96
-45
lines changed

4 files changed

+96
-45
lines changed

candump.c

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ static void print_usage(void)
128128
fprintf(stderr, "Options:\n");
129129
fprintf(stderr, " -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
130130
fprintf(stderr, " -H (read hardware timestamps instead of system timestamps)\n");
131+
fprintf(stderr, " -N (log nanosecond timestamps instead of microseconds)\n");
131132
fprintf(stderr, " -c (increment color mode level)\n");
132133
fprintf(stderr, " -i (binary output - may exceed 80 chars/line)\n");
133134
fprintf(stderr, " -a (enable additional ASCII output)\n");
@@ -220,49 +221,66 @@ static int idx2dindex(int ifidx, int socket)
220221
return i;
221222
}
222223

223-
static int sprint_timestamp(char *ts_buffer, const char timestamp,
224-
const struct timeval *tv, struct timeval *const last_tv)
224+
static int sprint_timestamp(char *ts_buffer, const char timestamp, const char use_ns,
225+
const struct timespec *ts, struct timespec *const last_ts)
225226
{
226227
int numchars = 0;
227228

228229
switch (timestamp) {
229230
case 'a': /* absolute with timestamp */
230-
numchars = sprintf(ts_buffer, "(%010llu.%06llu) ",
231-
(unsigned long long)tv->tv_sec,
232-
(unsigned long long)tv->tv_usec);
231+
if (use_ns) {
232+
numchars = sprintf(ts_buffer, "(%010llu.%09llu) ",
233+
(unsigned long long)ts->tv_sec,
234+
(unsigned long long)ts->tv_nsec);
235+
} else {
236+
numchars = sprintf(ts_buffer, "(%010llu.%06llu) ",
237+
(unsigned long long)ts->tv_sec,
238+
(unsigned long long)ts->tv_nsec / 1000);
239+
}
233240
break;
234241

235242
case 'A': /* absolute with date */
236243
{
237244
struct tm tm;
238245
char timestring[25];
239246

240-
tm = *localtime(&tv->tv_sec);
247+
tm = *localtime(&ts->tv_sec);
241248
strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
242-
numchars = sprintf(ts_buffer, "(%s.%06llu) ", timestring,
243-
(unsigned long long)tv->tv_usec);
249+
if (use_ns) {
250+
numchars = sprintf(ts_buffer, "(%s.%09llu) ", timestring,
251+
(unsigned long long)ts->tv_nsec);
252+
} else {
253+
numchars = sprintf(ts_buffer, "(%s.%06llu) ", timestring,
254+
(unsigned long long)ts->tv_nsec / 1000);
255+
}
244256
}
245257
break;
246258

247259
case 'd': /* delta */
248260
case 'z': /* starting with zero */
249261
{
250-
struct timeval diff;
251-
252-
if (last_tv->tv_sec == 0) /* first init */
253-
*last_tv = *tv;
254-
diff.tv_sec = tv->tv_sec - last_tv->tv_sec;
255-
diff.tv_usec = tv->tv_usec - last_tv->tv_usec;
256-
if (diff.tv_usec < 0)
257-
diff.tv_sec--, diff.tv_usec += 1000000;
262+
struct timespec diff;
263+
264+
if (last_ts->tv_sec == 0) /* first init */
265+
*last_ts = *ts;
266+
diff.tv_sec = ts->tv_sec - last_ts->tv_sec;
267+
diff.tv_nsec = ts->tv_nsec - last_ts->tv_nsec;
268+
if (diff.tv_nsec < 0)
269+
diff.tv_sec--, diff.tv_nsec += 1000000000;
258270
if (diff.tv_sec < 0)
259-
diff.tv_sec = diff.tv_usec = 0;
260-
numchars = sprintf(ts_buffer, "(%03llu.%06llu) ",
261-
(unsigned long long)diff.tv_sec,
262-
(unsigned long long)diff.tv_usec);
271+
diff.tv_sec = diff.tv_nsec = 0;
272+
if (use_ns) {
273+
numchars = sprintf(ts_buffer, "(%03llu.%09llu) ",
274+
(unsigned long long)diff.tv_sec,
275+
(unsigned long long)diff.tv_nsec);
276+
} else {
277+
numchars = sprintf(ts_buffer, "(%03llu.%06llu) ",
278+
(unsigned long long)diff.tv_sec,
279+
(unsigned long long)diff.tv_nsec / 1000);
280+
}
263281

264282
if (timestamp == 'd')
265-
*last_tv = *tv; /* update for delta calculation */
283+
*last_ts = *ts; /* update for delta calculation */
266284
}
267285
break;
268286

@@ -288,6 +306,7 @@ int main(int argc, char **argv)
288306
unsigned char timestamp = 0;
289307
unsigned char logtimestamp = 'a';
290308
unsigned char hwtimestamp = 0;
309+
unsigned char use_ns = 0;
291310
unsigned char down_causes_exit = 1;
292311
unsigned char dropmonitor = 0;
293312
unsigned char extra_msg_info = 0;
@@ -322,7 +341,7 @@ int main(int argc, char **argv)
322341
static cu_t cu; /* union for CAN CC/FD/XL frames */
323342
int nbytes, i;
324343
struct ifreq ifr;
325-
struct timeval tv, last_tv;
344+
struct timespec ts, last_ts;
326345
int timeout_ms = -1; /* default to no timeout */
327346
FILE *logfile = NULL;
328347
char fname[83]; /* suggested by -Wformat-overflow= */
@@ -334,12 +353,12 @@ int main(int argc, char **argv)
334353
signal(SIGHUP, sigterm);
335354
signal(SIGINT, sigterm);
336355

337-
last_tv.tv_sec = 0;
338-
last_tv.tv_usec = 0;
356+
last_ts.tv_sec = 0;
357+
last_ts.tv_nsec = 0;
339358

340359
progname = basename(argv[0]);
341360

342-
while ((opt = getopt(argc, argv, "t:HciaSs:lf:Ln:r:Dde8xT:h?")) != -1) {
361+
while ((opt = getopt(argc, argv, "t:HNciaSs:lf:Ln:r:Dde8xT:h?")) != -1) {
343362
switch (opt) {
344363
case 't':
345364
timestamp = optarg[0];
@@ -359,6 +378,10 @@ int main(int argc, char **argv)
359378
hwtimestamp = 1;
360379
break;
361380

381+
case 'N':
382+
use_ns = 1;
383+
break;
384+
362385
case 'c':
363386
color++;
364387
break;
@@ -784,7 +807,11 @@ int main(int argc, char **argv)
784807
cmsg && (cmsg->cmsg_level == SOL_SOCKET);
785808
cmsg = CMSG_NXTHDR(&msg,cmsg)) {
786809
if (cmsg->cmsg_type == SO_TIMESTAMP) {
810+
struct timeval tv;
787811
memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv));
812+
ts.tv_sec = tv.tv_sec;
813+
ts.tv_nsec = tv.tv_usec;
814+
ts.tv_nsec *= 1000;
788815
} else if (cmsg->cmsg_type == SO_TIMESTAMPING) {
789816
struct timespec *stamp = (struct timespec *)CMSG_DATA(cmsg);
790817

@@ -795,8 +822,7 @@ int main(int argc, char **argv)
795822
* See chapter 2.1.2 Receive timestamps in
796823
* linux/Documentation/networking/timestamping.txt
797824
*/
798-
tv.tv_sec = stamp[2].tv_sec;
799-
tv.tv_usec = stamp[2].tv_nsec / 1000;
825+
ts = stamp[2];
800826
} else if (cmsg->cmsg_type == SO_RXQ_OVFL) {
801827
memcpy(&obj->dropcnt, CMSG_DATA(cmsg), sizeof(__u32));
802828
}
@@ -831,11 +857,9 @@ int main(int argc, char **argv)
831857
/* build common log format output */
832858
if ((log) || ((logfrmt) && (silent == SILENT_OFF))) {
833859

834-
alen = sprint_timestamp(afrbuf, logtimestamp,
835-
&tv, &last_tv);
860+
alen = sprint_timestamp(afrbuf, logtimestamp, use_ns, &ts, &last_ts);
836861

837-
alen += sprintf(afrbuf + alen, "%*s ",
838-
max_devname_len, devname[idx]);
862+
alen += sprintf(afrbuf + alen, "%*s ", max_devname_len, devname[idx]);
839863

840864
alen += snprintf_canframe(afrbuf + alen, sizeof(afrbuf) - alen, &cu, 0);
841865
}
@@ -861,7 +885,7 @@ int main(int argc, char **argv)
861885

862886
/* print (colored) long CAN frame style to stdout */
863887
alen = sprintf(afrbuf, " %s", (color > 2) ? col_on[idx % MAXCOL] : "");
864-
alen += sprint_timestamp(afrbuf + alen, timestamp, &tv, &last_tv);
888+
alen += sprint_timestamp(afrbuf + alen, timestamp, use_ns, &ts, &last_ts);
865889
alen += sprintf(afrbuf + alen, " %s%*s",
866890
(color && (color < 3)) ? col_on[idx % MAXCOL] : "",
867891
max_devname_len, devname[idx]);

canplayer.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,20 @@ int main(int argc, char **argv)
452452
return 1;
453453
}
454454
log_tv.tv_sec = sec;
455-
log_tv.tv_usec = usec;
456455

457456
/*
458-
* ensure the fractions of seconds are 6 decimal places long to catch
457+
* ensure the fractions of seconds are 6 or 9 decimal places long to catch
459458
* 3rd party or handcrafted logfiles that treat the timestamp as float
460459
*/
461-
if (strchr(buf, ')') - strchr(buf, '.') != 7) {
462-
fprintf(stderr, "timestamp format in logfile requires 6 decimal places\n");
460+
switch (strchr(buf, ')') - strchr(buf, '.')) {
461+
case 7: //6
462+
log_tv.tv_usec = usec;
463+
break;
464+
case 10: //9
465+
log_tv.tv_usec = usec / 1000;
466+
break;
467+
default:
468+
fprintf(stderr, "timestamp format in logfile requires 6 or 9 decimal places\n");
463469
return 1;
464470
}
465471

@@ -541,19 +547,25 @@ int main(int argc, char **argv)
541547
break;
542548
}
543549

544-
if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, device, afrbuf) != 4) {
550+
if (sscanf(buf, "(%llu.%llu) %21s %6299s", &sec, &usec, device, afrbuf) != 4) {
545551
fprintf(stderr, "incorrect line format in logfile\n");
546552
return 1;
547553
}
548554
log_tv.tv_sec = sec;
549-
log_tv.tv_usec = usec;
550555

551556
/*
552-
* ensure the fractions of seconds are 6 decimal places long to catch
557+
* ensure the fractions of seconds are 6 or 9 decimal places long to catch
553558
* 3rd party or handcrafted logfiles that treat the timestamp as float
554559
*/
555-
if (strchr(buf, ')') - strchr(buf, '.') != 7) {
556-
fprintf(stderr, "timestamp format in logfile requires 6 decimal places\n");
560+
switch (strchr(buf, ')') - strchr(buf, '.')) {
561+
case 7: //6
562+
log_tv.tv_usec = usec;
563+
break;
564+
case 10: //9
565+
log_tv.tv_usec = usec / 1000;
566+
break;
567+
default:
568+
fprintf(stderr, "timestamp format in logfile requires 6 or 9 decimal places\n");
557569
return 1;
558570
}
559571

log2asc.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,22 @@ int main(int argc, char **argv)
407407
}
408408
}
409409
tv.tv_sec = sec;
410-
tv.tv_usec = usec;
410+
411+
/*
412+
* ensure the fractions of seconds are 6 or 9 decimal places long to catch
413+
* 3rd party or handcrafted logfiles that treat the timestamp as float
414+
*/
415+
switch (strchr(buf, ')') - strchr(buf, '.')) {
416+
case 7: //6
417+
tv.tv_usec = usec;
418+
break;
419+
case 10: //9
420+
tv.tv_usec = usec / 1000;
421+
break;
422+
default:
423+
fprintf(stderr, "timestamp format in logfile requires 6 or 9 decimal places\n");
424+
return 1;
425+
}
411426

412427
if (print_banner) { /* print banner */
413428
print_banner = 0;

log2long.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#include "lib.h"
5252

5353
#define DEVSZ 22
54-
#define TIMESZ 22 /* sizeof("(1345212884.318850) ") */
54+
#define TIMESZ 25 /* sizeof("(1345212884.318850123) ") */
5555
#define BUFSZ (DEVSZ + AFRSZ + TIMESZ)
5656

5757
/* adapt sscanf() functions below on error */
@@ -61,7 +61,7 @@
6161
#if (DEVSZ != 22)
6262
#error "DEVSZ value does not fit sscanf restrictions!"
6363
#endif
64-
#if (TIMESZ != 22)
64+
#if (TIMESZ != 25)
6565
#error "TIMESZ value does not fit sscanf restrictions!"
6666
#endif
6767

@@ -78,7 +78,7 @@ int main(void)
7878
return 1;
7979
}
8080

81-
if (sscanf(buf, "%21s %21s %6299s", timestamp, device, afrbuf) != 3)
81+
if (sscanf(buf, "%24s %21s %6299s", timestamp, device, afrbuf) != 3)
8282
return 1;
8383

8484
mtu = parse_canframe(afrbuf, &cu);

0 commit comments

Comments
 (0)