From 83e31d924e00d198cf40d323dd0ad3bcec901747 Mon Sep 17 00:00:00 2001 From: Onur Yildiz Date: Sat, 22 Apr 2023 18:14:31 +0300 Subject: [PATCH] Timestamps feature added Timestamps can be toggled with timestamps command or enabled with "-z --timestamps" argument. Signed-off-by: Onur Yildiz --- commands.c | 14 ++++++++++++++ microcom.c | 8 +++++++- microcom.h | 1 + mux.c | 39 +++++++++++++++++++++++++++++++++------ 4 files changed, 55 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 2b3d484..ab6ac5a 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) { @@ -97,6 +98,7 @@ void main_usage(int exitcode, char *str, char *dev) " -f, --force ignore existing lock file\n" " -d, --debug output debugging info\n" " -l, --logfile= log output to \n" + " -z, --timestamps Enable timestamps\n" " -o, --listenonly Do not modify local terminal, do not send input\n" " from stdin\n" " -a, --answerback= specify the answerback string sent as response to\n" @@ -135,11 +137,12 @@ int main(int argc, char *argv[]) { "logfile", required_argument, NULL, 'l'}, { "listenonly", no_argument, NULL, 'o'}, { "answerback", required_argument, NULL, 'a'}, + { "timestamps", no_argument, NULL, 'z' }, { "version", no_argument, NULL, 'v' }, { }, }; - while ((opt = getopt_long(argc, argv, "hp:s:t:c:dfl:oi:a:v", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "hp:s:t:c:dfl:oi:a:vz", long_options, NULL)) != -1) { switch (opt) { case '?': main_usage(1, "", ""); @@ -180,6 +183,9 @@ int main(int argc, char *argv[]) case 'a': answerback = optarg; break; + case 'z': + timestamps = 1; + break; } } diff --git a/microcom.h b/microcom.h index 54aea56..f5f1947 100644 --- a/microcom.h +++ b/microcom.h @@ -74,6 +74,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..dd638a4 100644 --- a/mux.c +++ b/mux.c @@ -22,9 +22,13 @@ #include #include #include +#include #define BUFSIZE 1024 +struct timeval now; +struct tm *timeinfo; + /* 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 +214,37 @@ static int do_subneg(struct ios_ops *ios, unsigned char *buf, int len) static int logfd = -1; char *answerback; +static void write_with_ts(const unsigned char *buf, int len) { + char tbuf[30]; + + for (int i = 0 ; i < len ; i++) { + + write(STDOUT_FILENO, &buf[i], 1); + if (logfd >= 0) write(logfd, &buf[i], 1); + if (buf[i] == '\n') { + 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 (logfd >= 0) write(logfd, tbuf, strlen(tbuf)); + } + } +} + static void write_receive_buf(const unsigned char *buf, int len) { - if (!len) - return; - - write(STDOUT_FILENO, buf, len); - if (logfd >= 0) - write(logfd, buf, len); + if (!len) return; + if (timestamps) { + write_with_ts(buf, len); + } else { + write(STDOUT_FILENO, buf, len); + if (logfd >= 0) write(logfd, buf, len); + } } static int ios_printf(struct ios_ops *ios, const char *format, ...)