Autodetect us or ns logfiles

Rikus Wessels 2025-07-12 22:31:03 +02:00
parent f69fccd528
commit 81dd2ff511
3 changed files with 40 additions and 13 deletions

View File

@ -452,14 +452,20 @@ int main(int argc, char **argv)
return 1;
}
log_tv.tv_sec = sec;
log_tv.tv_usec = usec;
/*
* ensure the fractions of seconds are 6 decimal places long to catch
* ensure the fractions of seconds are 6 or 9 decimal places long to catch
* 3rd party or handcrafted logfiles that treat the timestamp as float
*/
if (strchr(buf, ')') - strchr(buf, '.') != 7) {
fprintf(stderr, "timestamp format in logfile requires 6 decimal places\n");
switch (strchr(buf, ')') - strchr(buf, '.')) {
case 7: //6
log_tv.tv_usec = usec;
break;
case 10: //9
log_tv.tv_usec = usec / 1000;
break;
default:
fprintf(stderr, "timestamp format in logfile requires 6 or 9 decimal places\n");
return 1;
}
@ -541,19 +547,25 @@ int main(int argc, char **argv)
break;
}
if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, device, afrbuf) != 4) {
if (sscanf(buf, "(%llu.%llu) %21s %6299s", &sec, &usec, device, afrbuf) != 4) {
fprintf(stderr, "incorrect line format in logfile\n");
return 1;
}
log_tv.tv_sec = sec;
log_tv.tv_usec = usec;
/*
* ensure the fractions of seconds are 6 decimal places long to catch
* ensure the fractions of seconds are 6 or 9 decimal places long to catch
* 3rd party or handcrafted logfiles that treat the timestamp as float
*/
if (strchr(buf, ')') - strchr(buf, '.') != 7) {
fprintf(stderr, "timestamp format in logfile requires 6 decimal places\n");
switch (strchr(buf, ')') - strchr(buf, '.')) {
case 7: //6
log_tv.tv_usec = usec;
break;
case 10: //9
log_tv.tv_usec = usec / 1000;
break;
default:
fprintf(stderr, "timestamp format in logfile requires 6 or 9 decimal places\n");
return 1;
}

View File

@ -407,7 +407,22 @@ int main(int argc, char **argv)
}
}
tv.tv_sec = sec;
/*
* ensure the fractions of seconds are 6 or 9 decimal places long to catch
* 3rd party or handcrafted logfiles that treat the timestamp as float
*/
switch (strchr(buf, ')') - strchr(buf, '.')) {
case 7: //6
tv.tv_usec = usec;
break;
case 10: //9
tv.tv_usec = usec / 1000;
break;
default:
fprintf(stderr, "timestamp format in logfile requires 6 or 9 decimal places\n");
return 1;
}
if (print_banner) { /* print banner */
print_banner = 0;

View File

@ -51,7 +51,7 @@
#include "lib.h"
#define DEVSZ 22
#define TIMESZ 22 /* sizeof("(1345212884.318850) ") */
#define TIMESZ 25 /* sizeof("(1345212884.318850123) ") */
#define BUFSZ (DEVSZ + AFRSZ + TIMESZ)
/* adapt sscanf() functions below on error */
@ -61,7 +61,7 @@
#if (DEVSZ != 22)
#error "DEVSZ value does not fit sscanf restrictions!"
#endif
#if (TIMESZ != 22)
#if (TIMESZ != 25)
#error "TIMESZ value does not fit sscanf restrictions!"
#endif
@ -78,7 +78,7 @@ int main(void)
return 1;
}
if (sscanf(buf, "%21s %21s %6299s", timestamp, device, afrbuf) != 3)
if (sscanf(buf, "%24s %21s %6299s", timestamp, device, afrbuf) != 3)
return 1;
mtu = parse_canframe(afrbuf, &cu);