add optional timestamps

with "-t" or "C-t T", toggle a timestamp prefix to each line.
This commit is contained in:
Robey Pointer 2018-11-01 17:23:57 -07:00
parent 36691e8bdf
commit f96ad43b1f
4 changed files with 28 additions and 1 deletions

View file

@ -40,6 +40,7 @@ struct option_t
bool no_autoconnect;
bool log;
bool local_echo;
bool timestamp;
const char *log_filename;
const char *map;
};

View file

@ -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

View file

@ -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 <ms> 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 <filename> Log to file\n");
printf(" -m, --map <flags> 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;

View file

@ -34,6 +34,7 @@
#include <termios.h>
#include <stdbool.h>
#include <errno.h>
#include <time.h>
#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 */