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..cb9ab00 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) { @@ -212,12 +217,33 @@ char *answerback; 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) { + if (buf[len - 1] == '\n' || buf[len -1] == '\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); + } + if (logfd >= 0) write(logfd, buf, len); } static int ios_printf(struct ios_ops *ios, const char *format, ...)