From 21f4b81510a7ad4cbca5e1eab3598ad75e03f7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=B8=D0=BB=20=D0=96=D1=83=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Fri, 13 Dec 2019 14:47:00 +0300 Subject: [PATCH 1/5] Log splitting --- candump.c | 62 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/candump.c b/candump.c index f2ff03b1..cca725e2 100644 --- a/candump.c +++ b/candump.c @@ -213,6 +213,33 @@ int idx2dindex(int ifidx, int socket) { return i; } +int openlogfile(FILE **logfile) { + time_t currtime; + struct tm now; + char fname[83]; /* suggested by -Wformat-overflow= */ + + if (time(&currtime) == (time_t)-1) { + perror("time"); + return 1; + } + + localtime_r(&currtime, &now); + + sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log", now.tm_year + 1900, + now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec); + + fprintf(stderr, "Enabling Logfile '%s'\n", fname); + + FILE *tmpfile = fopen(fname, "w"); + if (!tmpfile) { + perror("logfile"); + return 1; + } + *logfile = tmpfile; + + return 0; +} + int main(int argc, char **argv) { fd_set rdfs; @@ -618,35 +645,11 @@ int main(int argc, char **argv) } if (log) { - time_t currtime; - struct tm now; - char fname[83]; /* suggested by -Wformat-overflow= */ - - if (time(&currtime) == (time_t)-1) { - perror("time"); + if (openlogfile(&logfile) != 0) return 1; - } - - localtime_r(&currtime, &now); - - sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log", - now.tm_year + 1900, - now.tm_mon + 1, - now.tm_mday, - now.tm_hour, - now.tm_min, - now.tm_sec); if (silent != SILENT_ON) fprintf(stderr, "Warning: Console output active while logging!\n"); - - fprintf(stderr, "Enabling Logfile '%s'\n", fname); - - logfile = fopen(fname, "w"); - if (!logfile) { - perror("logfile"); - return 1; - } } /* these settings are static and can be held out of the hot path */ @@ -765,12 +768,19 @@ int main(int argc, char **argv) if (log) { char buf[CL_CFSZ]; /* max length */ + char line[CL_CFSZ + max_devname_len + 22]; /* log CAN frame with absolute timestamp & device */ sprint_canframe(buf, &frame, 0, maxdlen); - fprintf(logfile, "(%010ld.%06ld) %*s %s\n", + int n = sprintf(line, "(%010ld.%06ld) %*s %s\n", tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx], buf); + if (ftell(logfile) + n > 1024) { + fclose(logfile); + if (openlogfile(&logfile) != 0) + return 1; + } + fprintf(logfile, "%s", line); } if ((logfrmt) && (silent == SILENT_OFF)){ From c2b2cf0f92eb391abdfaa992342d39bfd1c08f00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=B8=D0=BB=20=D0=96=D1=83=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Tue, 17 Dec 2019 11:30:00 +0300 Subject: [PATCH 2/5] Log file max size flag --- candump.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/candump.c b/candump.c index cca725e2..4c56e593 100644 --- a/candump.c +++ b/candump.c @@ -129,6 +129,7 @@ void print_usage(char *prg) fprintf(stderr, " -B (bridge mode - like '-b' with disabled loopback)\n"); fprintf(stderr, " -u (delay bridge forwarding by microseconds)\n"); fprintf(stderr, " -l (log CAN-frames into file. Sets '-s %d' by default)\n", SILENT_ON); + fprintf(stderr, " -m (log file max size)\n"); fprintf(stderr, " -L (use log file format on stdout)\n"); fprintf(stderr, " -n (terminate after reception of CAN frames)\n"); fprintf(stderr, " -r (set socket receive buffer to )\n"); @@ -256,6 +257,7 @@ int main(int argc, char **argv) unsigned char color = 0; unsigned char view = 0; unsigned char log = 0; + unsigned long logmax = 0; unsigned char logfrmt = 0; int count = 0; int rcvbuf_size = 0; @@ -284,7 +286,7 @@ int main(int argc, char **argv) last_tv.tv_sec = 0; last_tv.tv_usec = 0; - while ((opt = getopt(argc, argv, "t:HciaSs:b:B:u:lDdxLn:r:heT:?")) != -1) { + while ((opt = getopt(argc, argv, "t:HciaSs:b:B:u:lm:DdxLn:r:heT:?")) != -1) { switch (opt) { case 't': timestamp = optarg[0]; @@ -378,6 +380,10 @@ int main(int argc, char **argv) log = 1; break; + case 'm': + logmax = strtoul(optarg, (char **)NULL, 10); // TODO: Parse strings with sizes: 10mb, 1gb etc + break; + case 'D': down_causes_exit = 0; break; @@ -775,7 +781,7 @@ int main(int argc, char **argv) int n = sprintf(line, "(%010ld.%06ld) %*s %s\n", tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx], buf); - if (ftell(logfile) + n > 1024) { + if (logmax && ftell(logfile) + n > logmax) { fclose(logfile); if (openlogfile(&logfile) != 0) return 1; From 205c1519d3d6304f724ef09aa0469174a59b503b Mon Sep 17 00:00:00 2001 From: mesuchan Date: Tue, 17 Dec 2019 13:19:49 +0300 Subject: [PATCH 3/5] Pretty size string --- candump.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/candump.c b/candump.c index 4c56e593..414f7f7c 100644 --- a/candump.c +++ b/candump.c @@ -241,6 +241,28 @@ int openlogfile(FILE **logfile) { return 0; } +unsigned long convertsize(const char *str) { + char *tmp; + unsigned long ret = strtoul(str, &tmp, 10); + + if (strlen(tmp) == 0) + return ret; + + if (strlen(tmp) == 1) + { + if (tmp[0] == 'k') + return ret * 1024; + + if (tmp[0] == 'm') + return ret * 1024 * 1024; + + if (tmp[0] == 'g') + return ret * 1024 * 1024 * 1024; + } + + return 0; +} + int main(int argc, char **argv) { fd_set rdfs; @@ -381,7 +403,7 @@ int main(int argc, char **argv) break; case 'm': - logmax = strtoul(optarg, (char **)NULL, 10); // TODO: Parse strings with sizes: 10mb, 1gb etc + logmax = convertsize(optarg); break; case 'D': From c5155451399371eb8340207b5a0e933ee8987511 Mon Sep 17 00:00:00 2001 From: mesuchan Date: Tue, 17 Dec 2019 14:23:14 +0300 Subject: [PATCH 4/5] Pretty file name --- candump.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/candump.c b/candump.c index 414f7f7c..515bcf3c 100644 --- a/candump.c +++ b/candump.c @@ -114,6 +114,9 @@ extern int optind, opterr, optopt; static volatile int running = 1; +static int part = 0; +static unsigned long logmax = 0; + void print_usage(char *prg) { fprintf(stderr, "\nUsage: %s [options] +\n", prg); @@ -226,8 +229,14 @@ int openlogfile(FILE **logfile) { localtime_r(&currtime, &now); - sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log", now.tm_year + 1900, + if (!logmax) { + sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log", now.tm_year + 1900, now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec); + } else { + sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.pt%d.log", now.tm_year + 1900, + now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, part); + part++; + } fprintf(stderr, "Enabling Logfile '%s'\n", fname); @@ -279,7 +288,6 @@ int main(int argc, char **argv) unsigned char color = 0; unsigned char view = 0; unsigned char log = 0; - unsigned long logmax = 0; unsigned char logfrmt = 0; int count = 0; int rcvbuf_size = 0; From 1fe71b00ff73cacc22cba893552cc92531119654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=B8=D0=BB=20=D0=96=D1=83=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Mon, 23 Dec 2019 17:34:42 +0300 Subject: [PATCH 5/5] Keeping original log file name --- candump.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/candump.c b/candump.c index 515bcf3c..c0b1198e 100644 --- a/candump.c +++ b/candump.c @@ -106,6 +106,8 @@ static int dindex[MAXIFNAMES]; static int max_devname_len; /* to prevent frazzled device name output */ const int canfd_on = 1; +static char fname[83] = { 0 }; /* suggested by -Wformat-overflow= */ + #define MAXANI 4 const char anichar[MAXANI] = {'|', '/', '-', '\\'}; const char extra_m_info[4][4] = {"- -", "B -", "- E", "B E"}; @@ -114,9 +116,6 @@ extern int optind, opterr, optopt; static volatile int running = 1; -static int part = 0; -static unsigned long logmax = 0; - void print_usage(char *prg) { fprintf(stderr, "\nUsage: %s [options] +\n", prg); @@ -132,7 +131,7 @@ void print_usage(char *prg) fprintf(stderr, " -B (bridge mode - like '-b' with disabled loopback)\n"); fprintf(stderr, " -u (delay bridge forwarding by microseconds)\n"); fprintf(stderr, " -l (log CAN-frames into file. Sets '-s %d' by default)\n", SILENT_ON); - fprintf(stderr, " -m (log file max size)\n"); + fprintf(stderr, " -m (log file max size - size examples: 1024, 1k, 100m, 1g)\n"); fprintf(stderr, " -L (use log file format on stdout)\n"); fprintf(stderr, " -n (terminate after reception of CAN frames)\n"); fprintf(stderr, " -r (set socket receive buffer to )\n"); @@ -220,7 +219,6 @@ int idx2dindex(int ifidx, int socket) { int openlogfile(FILE **logfile) { time_t currtime; struct tm now; - char fname[83]; /* suggested by -Wformat-overflow= */ if (time(&currtime) == (time_t)-1) { perror("time"); @@ -229,16 +227,12 @@ int openlogfile(FILE **logfile) { localtime_r(&currtime, &now); - if (!logmax) { + if (fname[0] == 0) { sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log", now.tm_year + 1900, now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec); - } else { - sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.pt%d.log", now.tm_year + 1900, - now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, part); - part++; - } - fprintf(stderr, "Enabling Logfile '%s'\n", fname); + fprintf(stderr, "Enabling Logfile '%s'\n", fname); + } FILE *tmpfile = fopen(fname, "w"); if (!tmpfile) { @@ -289,6 +283,8 @@ int main(int argc, char **argv) unsigned char view = 0; unsigned char log = 0; unsigned char logfrmt = 0; + unsigned long logmax = 0; + unsigned char part = 0; int count = 0; int rcvbuf_size = 0; int opt, ret; @@ -813,6 +809,13 @@ int main(int argc, char **argv) max_devname_len, devname[idx], buf); if (logmax && ftell(logfile) + n > logmax) { fclose(logfile); + char postfix[7] = { 0 }; + char fullfname[sizeof(fname) + sizeof(postfix)]; + sprintf(postfix, ".pt%u", part); + strcpy(fullfname, fname); + strcat(fullfname, postfix); + rename(fname, fullfname); + ++part; if (openlogfile(&logfile) != 0) return 1; }