From b1a619065abd65ec167c6109f840daab5e426d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dragan=20Jovanovi=C4=87?= Date: Mon, 15 Aug 2022 13:02:16 +0200 Subject: [PATCH 1/3] Msg: Added timestamp option for STDOUT logs, on each line, option can be toggled from commands input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dragan Jovanović --- commands.c | 14 ++++++++++++++ microcom.c | 1 + microcom.h | 1 + mux.c | 40 +++++++++++++++++++++++++++++++++------- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/commands.c b/commands.c index 6d6c0e9..10aa65e 100644 --- a/commands.c +++ b/commands.c @@ -189,6 +189,15 @@ static int cmd_log(int argc, char *argv[]) return ret; } +static int cmd_timestamp(int argc, char *argv[]) +{ + if(timestamps) + timestamps = 0; + else + timestamps = 1; + return MICROCOM_CMD_START; +} + static int cmd_comment(int argc, char *argv[]) { return 0; @@ -245,6 +254,11 @@ static struct cmd cmds[] = { .name = "#", .fn = cmd_comment, .info = "comment", + }, { + .name = "timestamps", + .fn = cmd_timestamp, + .info = "turns on timestamps for each line of output", + .help = "toggle on/off", }, }; diff --git a/microcom.c b/microcom.c index 5bda7b4..e7490db 100644 --- a/microcom.c +++ b/microcom.c @@ -36,6 +36,7 @@ static struct termios sots; /* old stdout/in termios settings to restore */ struct ios_ops *ios; int debug; +int timestamps = 0; void init_terminal(void) { diff --git a/microcom.h b/microcom.h index 0956e7d..04f707d 100644 --- a/microcom.h +++ b/microcom.h @@ -77,6 +77,7 @@ extern int debug; extern int opt_force; extern int listenonly; extern char *answerback; +extern int timestamps; struct cmd { char *name; diff --git a/mux.c b/mux.c index 33c92f7..5d50dc1 100644 --- a/mux.c +++ b/mux.c @@ -25,6 +25,11 @@ #define BUFSIZE 1024 +struct timeval now; +struct tm *timeinfo; + +char new_line = true; + /* This is called with buf[-2:0] being IAC SB COM_PORT_OPTION */ static int do_com_port_option(struct ios_ops *ios, unsigned char *buf, int len) { @@ -210,14 +215,35 @@ static int do_subneg(struct ios_ops *ios, unsigned char *buf, int len) static int logfd = -1; char *answerback; -static void write_receive_buf(const unsigned char *buf, int len) -{ - if (!len) - return; +static void write_receive_buf(const unsigned char *buf, int len) { + if (!len) return; + if (timestamps) { + if (buf[0] == '\n' || buf[0] == '\r') { + new_line = true; + write(STDOUT_FILENO, buf, len); + } else { + if (new_line) { + new_line = false; + char tbuf[30]; + memset(tbuf, 0, sizeof(tbuf)); + gettimeofday(&now, NULL); + timeinfo = gmtime(&now.tv_sec); + snprintf(tbuf, sizeof(tbuf), + "[%02d-%02d-%02d %02d:%02d:%02d:%03d] ", + timeinfo->tm_mday, timeinfo->tm_mon + 1, + timeinfo->tm_year + 1900, timeinfo->tm_hour, + timeinfo->tm_min, timeinfo->tm_sec, now.tv_usec / 1000); + write(STDOUT_FILENO, tbuf, strlen(tbuf)); + write(STDOUT_FILENO, buf, len); + } else { + write(STDOUT_FILENO, buf, len); + } + } + } else { + write(STDOUT_FILENO, buf, len); + } - write(STDOUT_FILENO, buf, len); - if (logfd >= 0) - write(logfd, buf, len); + if (logfd >= 0) write(logfd, buf, len); } static int ios_printf(struct ios_ops *ios, const char *format, ...) From 548a4913913e1647f7a433dc51e17f3de51cae43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dragan=20Jovanovi=C4=87?= Date: Mon, 15 Aug 2022 15:39:15 +0200 Subject: [PATCH 2/3] Formating of mux.c is fixed to be aligned with the rest of the project MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dragan Jovanović --- mux.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/mux.c b/mux.c index 5d50dc1..a8f9af4 100644 --- a/mux.c +++ b/mux.c @@ -215,34 +215,34 @@ static int do_subneg(struct ios_ops *ios, unsigned char *buf, int len) static int logfd = -1; char *answerback; -static void write_receive_buf(const unsigned char *buf, int len) { +static void write_receive_buf(const unsigned char *buf, int len) +{ if (!len) return; if (timestamps) { - if (buf[0] == '\n' || buf[0] == '\r') { - new_line = true; - write(STDOUT_FILENO, buf, len); - } else { - if (new_line) { - new_line = false; - char tbuf[30]; - memset(tbuf, 0, sizeof(tbuf)); - gettimeofday(&now, NULL); - timeinfo = gmtime(&now.tv_sec); - snprintf(tbuf, sizeof(tbuf), - "[%02d-%02d-%02d %02d:%02d:%02d:%03d] ", - timeinfo->tm_mday, timeinfo->tm_mon + 1, - timeinfo->tm_year + 1900, timeinfo->tm_hour, - timeinfo->tm_min, timeinfo->tm_sec, now.tv_usec / 1000); - write(STDOUT_FILENO, tbuf, strlen(tbuf)); + if (buf[len - 1] == '\n' || buf[len -1] == '\r') { + new_line = true; write(STDOUT_FILENO, buf, len); } else { - write(STDOUT_FILENO, buf, len); + if (new_line) { + new_line = false; + char tbuf[30]; + memset(tbuf, 0, sizeof(tbuf)); + gettimeofday(&now, NULL); + timeinfo = gmtime(&now.tv_sec); + snprintf(tbuf, sizeof(tbuf), + "[%02d-%02d-%02d %02d:%02d:%02d:%03d] ", + timeinfo->tm_mday, timeinfo->tm_mon + 1, + timeinfo->tm_year + 1900, timeinfo->tm_hour, + timeinfo->tm_min, timeinfo->tm_sec, now.tv_usec / 1000); + write(STDOUT_FILENO, tbuf, strlen(tbuf)); + write(STDOUT_FILENO, buf, len); + } else { + write(STDOUT_FILENO, buf, len); + } } - } } else { write(STDOUT_FILENO, buf, len); } - if (logfd >= 0) write(logfd, buf, len); } From aa400609de6c4b19b5541eb4ab835b0e50b43d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dragan=20Jovanovi=C4=87?= Date: Mon, 15 Aug 2022 15:42:38 +0200 Subject: [PATCH 3/3] Formating of mux.c is fixed to be aligned with the rest of the project. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dragan Jovanović --- mux.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mux.c b/mux.c index a8f9af4..cb9ab00 100644 --- a/mux.c +++ b/mux.c @@ -217,7 +217,7 @@ char *answerback; static void write_receive_buf(const unsigned char *buf, int len) { - if (!len) return; + if (!len) return; if (timestamps) { if (buf[len - 1] == '\n' || buf[len -1] == '\r') { new_line = true; @@ -243,7 +243,7 @@ static void write_receive_buf(const unsigned char *buf, int len) } else { write(STDOUT_FILENO, buf, len); } - if (logfd >= 0) write(logfd, buf, len); + if (logfd >= 0) write(logfd, buf, len); } static int ios_printf(struct ios_ops *ios, const char *format, ...)