From f96ad43b1f53c26fbb900dbcedc5767acf48487d Mon Sep 17 00:00:00 2001 From: Robey Pointer Date: Thu, 1 Nov 2018 17:23:57 -0700 Subject: [PATCH] add optional timestamps with "-t" or "C-t T", toggle a timestamp prefix to each line. --- src/include/tio/options.h | 1 + src/include/tio/tty.h | 1 + src/options.c | 9 ++++++++- src/tty.c | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/include/tio/options.h b/src/include/tio/options.h index c2292e1..1ae1974 100644 --- a/src/include/tio/options.h +++ b/src/include/tio/options.h @@ -40,6 +40,7 @@ struct option_t bool no_autoconnect; bool log; bool local_echo; + bool timestamp; const char *log_filename; const char *map; }; diff --git a/src/include/tio/tty.h b/src/include/tio/tty.h index 888469f..ee8894e 100644 --- a/src/include/tio/tty.h +++ b/src/include/tio/tty.h @@ -31,6 +31,7 @@ #define KEY_Q 0x71 #define KEY_S 0x73 #define KEY_T 0x74 +#define KEY_SHIFT_T 0x54 #define KEY_CTRL_T 0x14 #define NORMAL 0 diff --git a/src/options.c b/src/options.c index f50f30b..c88582a 100644 --- a/src/options.c +++ b/src/options.c @@ -46,6 +46,7 @@ struct option_t option = false, // No autoconnect false, // No log false, // No local echo + false, // No timestamp "", // Log filename "" // Map string }; @@ -63,6 +64,7 @@ void print_help(char *argv[]) printf(" -o, --output-delay Output delay (default: 0)\n"); printf(" -n, --no-autoconnect Disable automatic connect\n"); printf(" -e, --local-echo Do local echo\n"); + printf(" -t, --timestamp Prefix each new line with a timestamp\n"); printf(" -l, --log Log to file\n"); printf(" -m, --map Map special characters\n"); printf(" -v, --version Display version\n"); @@ -112,6 +114,7 @@ void parse_options(int argc, char *argv[]) {"output-delay", required_argument, 0, 'o'}, {"no-autoconnect", no_argument, 0, 'n'}, {"local-echo", no_argument, 0, 'e'}, + {"timestamp", no_argument, 0, 't'}, {"log", required_argument, 0, 'l'}, {"map", required_argument, 0, 'm'}, {"version", no_argument, 0, 'v'}, @@ -123,7 +126,7 @@ void parse_options(int argc, char *argv[]) int option_index = 0; /* Parse argument using getopt_long */ - c = getopt_long(argc, argv, "b:d:f:s:p:o:nel:m:vh", long_options, &option_index); + c = getopt_long(argc, argv, "b:d:f:s:p:o:netl:m:vh", long_options, &option_index); /* Detect the end of the options */ if (c == -1) @@ -173,6 +176,10 @@ void parse_options(int argc, char *argv[]) option.local_echo = true; break; + case 't': + option.timestamp = true; + break; + case 'l': option.log = true; option.log_filename = optarg; diff --git a/src/tty.c b/src/tty.c index b337ba8..d72e436 100644 --- a/src/tty.c +++ b/src/tty.c @@ -34,6 +34,7 @@ #include #include #include +#include #include "config.h" #include "tio/tty.h" #include "tio/print.h" @@ -107,6 +108,7 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c tio_printf(" ctrl-t q Quit"); tio_printf(" ctrl-t s Show statistics"); tio_printf(" ctrl-t t Send ctrl-t key code"); + tio_printf(" ctrl-t T Toggle timestamps"); break; case KEY_B: @@ -122,6 +124,7 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c tio_printf(" Stopbits: %d", option.stopbits); tio_printf(" Parity: %s", option.parity); tio_printf(" Local Echo: %s", option.local_echo ? "yes":"no"); + tio_printf(" Timestamps: %s", option.timestamp ? "yes" : "no"); tio_printf(" Output delay: %d", option.output_delay); if (option.map[0] != 0) tio_printf(" Map flags: %s", option.map); @@ -171,6 +174,10 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c *forward = true; break; + case KEY_SHIFT_T: + option.timestamp = !option.timestamp; + break; + default: /* Ignore unknown ctrl-t escaped keys */ break; @@ -535,6 +542,7 @@ int tty_connect(void) static char previous_char = 0; static bool first = true; int status; + time_t next_timestamp = 0; /* Open tty device */ #ifdef __APPLE__ @@ -571,6 +579,8 @@ int tty_connect(void) connected = true; tainted = false; + if (option.timestamp) next_timestamp = time(NULL); + /* Save current port settings */ if (tcgetattr(fd, &tio_old) < 0) goto error_tcgetattr; @@ -622,11 +632,18 @@ int tty_connect(void) /* Update receive statistics */ rx_total++; + /* Print timestamp on new line, if desired. */ + if (next_timestamp && input_char != '\n' && input_char != '\r') { + fprintf(stdout, ANSI_COLOR_GRAY "[%s] " ANSI_COLOR_RESET, current_time()); + next_timestamp = 0; + } + /* Map input character */ if ((input_char == '\n') && (map_i_nl_crnl)) { print('\r'); print('\n'); + if (option.timestamp) next_timestamp = time(NULL); } else { /* Print received tty character to stdout */ @@ -640,6 +657,7 @@ int tty_connect(void) tainted = true; + if (input_char == '\n' && option.timestamp) next_timestamp = time(NULL); } else { /* Error reading - device is likely unplugged */