Add timeout based timestamps in hex output mode

This change reintroduces timestamping in hex output mode but based on
timeout instead of new lines which made no sense. This means that
timestamps will only be printed when timeout time has elapsed with no
output activity from serial device.

Adds option --timestamp-timeout <ms> for setting the timeout value in
milliseconds.

Defaults to 200 ms.
This commit is contained in:
Martin Lund 2024-04-18 15:45:50 +02:00
parent a8e0d2693d
commit 6fff4939e4
8 changed files with 76 additions and 20 deletions

View file

@ -28,6 +28,7 @@ _tio()
-m --map \
-t --timestamp \
--timestamp-format \
--timestamp-timeout \
-L --list-devices \
-c --color \
-S --socket \
@ -118,6 +119,10 @@ _tio()
COMPREPLY=( $(compgen -W "24hour 24hour-start 24hour-delta iso8601" -- ${cur}) )
return 0
;;
--timestamp-timeout)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
-L | --list-devices)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0

View file

@ -230,6 +230,10 @@ static int data_handler(void *user, const char *section, const char *name,
{
option.timestamp = timestamp_option_parse(value);
}
else if (!strcmp(name, "timestamp-timeout"))
{
option.timestamp_timeout = read_integer(value, name, 0, LONG_MAX);
}
else if (!strcmp(name, "map"))
{
asprintf(&c.map, "%s", value);

View file

@ -45,6 +45,7 @@ enum opt_t
{
OPT_NONE,
OPT_TIMESTAMP_FORMAT,
OPT_TIMESTAMP_TIMEOUT,
OPT_LOG_FILE,
OPT_LOG_DIRECTORY,
OPT_LOG_STRIP,
@ -105,6 +106,7 @@ struct option_t option =
.script = NULL,
.script_filename = NULL,
.script_run = SCRIPT_RUN_ALWAYS,
.timestamp_timeout = 200,
};
void print_help(char *argv[])
@ -130,6 +132,7 @@ void print_help(char *argv[])
printf(" --output-mode normal|hex Select output mode (default: normal)\n");
printf(" -t, --timestamp Enable line timestamp\n");
printf(" --timestamp-format <format> Set timestamp format (default: 24hour)\n");
printf(" --timestamp-timeout <ms> Set timestamp timeout (default: 200)\n");
printf(" -L, --list-devices List available serial devices by ID\n");
printf(" -l, --log Enable log to file\n");
printf(" --log-file <filename> Set log filename\n");
@ -367,6 +370,7 @@ void options_parse(int argc, char *argv[])
{"local-echo", no_argument, 0, 'e' },
{"timestamp", no_argument, 0, 't' },
{"timestamp-format", required_argument, 0, OPT_TIMESTAMP_FORMAT },
{"timestamp-timeout", required_argument, 0, OPT_TIMESTAMP_TIMEOUT },
{"list-devices", no_argument, 0, 'L' },
{"log", no_argument, 0, 'l' },
{"log-file", required_argument, 0, OPT_LOG_FILE },
@ -461,6 +465,10 @@ void options_parse(int argc, char *argv[])
option.timestamp = timestamp_option_parse(optarg);
break;
case OPT_TIMESTAMP_TIMEOUT:
option.timestamp_timeout = string_to_long(optarg);
break;
case 'L':
list_serial_devices();
exit(EXIT_SUCCESS);

View file

@ -88,6 +88,7 @@ struct option_t
const char *script;
const char *script_filename;
enum script_run_t script_run;
unsigned int timestamp_timeout;
};
extern struct option_t option;

View file

@ -22,6 +22,7 @@
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
@ -972,7 +973,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
break;
case TIMESTAMP_END:
option.timestamp = TIMESTAMP_NONE;
tio_printf("Switched timestamp mode to off");
tio_printf("Switched timestamp mode off");
break;
}
break;
@ -1545,6 +1546,7 @@ int tty_connect(void)
char* now = NULL;
unsigned int line_index = 0;
static char previous_char[2] = {};
struct timeval tval_before = {}, tval_now, tval_result;
/* Open tty device */
device_fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
@ -1713,23 +1715,50 @@ int tty_connect(void)
/* Update receive statistics */
rx_total += bytes_read;
// Manage timeout based timestamping in hex mode
if (option.output_mode == OUTPUT_MODE_HEX)
{
if (option.timestamp != TIMESTAMP_NONE)
{
gettimeofday(&tval_now, NULL);
timersub(&tval_now, &tval_before, &tval_result);
if ((tval_result.tv_sec * 1000 + tval_result.tv_usec / 1000) > option.timestamp_timeout)
{
now = timestamp_current_time();
if (now)
{
ansi_printf_raw("\r\n[%s] ", now);
if (option.log)
{
log_printf("\r\n[%s] ", now);
}
next_timestamp = false;
}
}
tval_before = tval_now;
}
}
/* Process input byte by byte */
for (int i=0; i<bytes_read; i++)
{
input_char = input_buffer[i];
/* Print timestamp on new line if enabled */
if ((next_timestamp && input_char != '\n' && input_char != '\r') && (option.output_mode == OUTPUT_MODE_NORMAL))
if (option.output_mode == OUTPUT_MODE_NORMAL)
{
now = timestamp_current_time();
if (now)
if ((next_timestamp && input_char != '\n' && input_char != '\r'))
{
ansi_printf_raw("[%s] ", now);
if (option.log)
now = timestamp_current_time();
if (now)
{
log_printf("[%s] ", now);
ansi_printf_raw("[%s] ", now);
if (option.log)
{
log_printf("[%s] ", now);
}
next_timestamp = false;
}
next_timestamp = false;
}
}