Compare commits

..

3 Commits

Author SHA1 Message Date
Rikus Wessels eb1bdd0732
Merge 81dd2ff511 into 6b46063eee 2025-07-12 22:31:48 +02:00
Rikus Wessels 81dd2ff511 Autodetect us or ns logfiles 2025-07-12 22:31:03 +02:00
Rikus Wessels f69fccd528 Optional nanosecond timestamp logging 2025-07-12 21:12:19 +02:00
4 changed files with 43 additions and 18 deletions

View File

@ -221,7 +221,7 @@ static int idx2dindex(int ifidx, int socket)
return i;
}
static int sprint_timestamp(char *ts_buffer, const char timestamp, unsigned char use_ns,
static int sprint_timestamp(char *ts_buffer, const char timestamp, const char use_ns,
const struct timespec *ts, struct timespec *const last_ts)
{
int numchars = 0;
@ -857,11 +857,9 @@ int main(int argc, char **argv)
/* build common log format output */
if ((log) || ((logfrmt) && (silent == SILENT_OFF))) {
alen = sprint_timestamp(afrbuf, logtimestamp, use_ns,
&ts, &last_ts);
alen = sprint_timestamp(afrbuf, logtimestamp, use_ns, &ts, &last_ts);
alen += sprintf(afrbuf + alen, "%*s ",
max_devname_len, devname[idx]);
alen += sprintf(afrbuf + alen, "%*s ", max_devname_len, devname[idx]);
alen += snprintf_canframe(afrbuf + alen, sizeof(afrbuf) - alen, &cu, 0);
}

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;
tv.tv_usec = usec;
/*
* 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);