Add key command to toggle log on/off

Add key command 'ctrl-t f' which will toggle log on/off.

If no log filename has been specified via the 'log-filename' option then
tio will automatically generate a new log filename every time the log
feature is toggled on. Meaning, when toggled multiple times, multiple
log files will be generated.

However, if a log filename has been specified, tio will only write and
append to that same file.
This commit is contained in:
Martin Lund 2022-11-23 17:24:51 +01:00
parent a4f0d4da53
commit 419fbdc3fa
6 changed files with 58 additions and 26 deletions

View file

@ -178,6 +178,7 @@ ctrl-t ? to list the available key commands.
[20:19:12.040] ctrl-t b Send break
[20:19:12.040] ctrl-t c Show configuration
[20:19:12.040] ctrl-t e Toggle local echo mode
[20:19:12.040] ctrl-t f Toggle log to file
[20:19:12.040] ctrl-t g Toggle serial port line
[20:19:12.040] ctrl-t h Toggle hexadecimal mode
[20:19:12.040] ctrl-t l Clear screen

View file

@ -289,6 +289,8 @@ Send serial break (triggers SysRq on Linux, etc.)
Show configuration (baudrate, databits, etc.)
.IP "\fBctrl-t e"
Toggle local echo mode
.IP "\fBctrl-t f"
Toggle log to file
.IP "\fBctrl-t g"
Toggle serial port line
.IP "\fBctrl-t h"

View file

@ -38,9 +38,9 @@
#define IS_ESC_END_CHAR(c) ((c >= 0x30) && (c <= 0x7E))
#define IS_CTRL_CHAR(c) ((c >= 0x00) && (c <= 0x1F))
static FILE *fp;
static bool log_error = false;
static FILE *fp = NULL;
static char file_buffer[BUFSIZ];
static const char *log_filename = NULL;
static char *date_time(void)
{
@ -56,7 +56,7 @@ static char *date_time(void)
return date_time_string;
}
void log_open(const char *filename)
int log_open(const char *filename)
{
static char automatic_filename[400];
@ -65,19 +65,22 @@ void log_open(const char *filename)
// Generate filename if none provided ("tio_DEVICE_YYYY-MM-DDTHH:MM:SS.log")
sprintf(automatic_filename, "tio_%s_%s.log", basename((char *)option.tty_device), date_time());
filename = automatic_filename;
option.log_filename = automatic_filename;
}
log_filename = filename;
// Open log file in append write mode
fp = fopen(filename, "a+");
if (fp == NULL)
{
log_error = true;
exit(EXIT_FAILURE);
tio_warning_printf("Could not open log file %s (%s)", filename, strerror(errno));
return -1;
}
// Enable line buffering
setvbuf(fp, file_buffer, _IOLBF, BUFSIZ);
return 0;
}
bool log_strip(char c)
@ -141,6 +144,11 @@ bool log_strip(char c)
void log_printf(const char *format, ...)
{
if (fp == NULL)
{
return;
}
char *line;
va_list(args);
@ -155,8 +163,11 @@ void log_printf(const char *format, ...)
void log_putc(char c)
{
if (fp != NULL)
if (fp == NULL)
{
return;
}
if (option.log_strip)
{
if (!log_strip(c))
@ -168,7 +179,6 @@ void log_putc(char c)
{
fputc(c, fp);
}
}
}
void log_close(void)
@ -176,6 +186,8 @@ void log_close(void)
if (fp != NULL)
{
fclose(fp);
fp = NULL;
log_filename = NULL;
}
}
@ -183,15 +195,12 @@ void log_exit(void)
{
if (option.log)
{
tio_printf("Saved log to file %s", log_filename);
log_close();
}
if (log_error)
{
tio_error_printf("Could not open log file %s (%s)", option.log_filename, strerror(errno));
}
else if (option.log)
{
tio_printf("Saved log to file %s", option.log_filename);
}
}
const char *log_get_filename(void)
{
return log_filename;
}

View file

@ -21,8 +21,9 @@
#pragma once
void log_open(const char *filename);
int log_open(const char *filename);
void log_printf(const char *format, ...);
void log_putc(char c);
void log_close(void);
void log_exit(void);
const char * log_get_filename(void);

View file

@ -38,6 +38,7 @@
#include "rs485.h"
#include "timestamp.h"
#include "alert.h"
#include "log.h"
enum opt_t
{
@ -216,7 +217,7 @@ void options_print()
if (option.map[0] != 0)
tio_printf(" Map flags: %s", option.map);
if (option.log)
tio_printf(" Log file: %s", option.log_filename);
tio_printf(" Log file: %s", log_get_filename());
if (option.socket)
tio_printf(" Socket: %s", option.socket);
}

View file

@ -80,6 +80,7 @@
#define KEY_B 0x62
#define KEY_C 0x63
#define KEY_E 0x65
#define KEY_F 0x66
#define KEY_G 0x67
#define KEY_H 0x68
#define KEY_L 0x6C
@ -414,6 +415,7 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c
tio_printf(" ctrl-%c b Send break", option.prefix_key);
tio_printf(" ctrl-%c c Show configuration", option.prefix_key);
tio_printf(" ctrl-%c e Toggle local echo mode", option.prefix_key);
tio_printf(" ctrl-%c f Toggle log to file", option.prefix_key);
tio_printf(" ctrl-%c g Toggle serial port line", option.prefix_key);
tio_printf(" ctrl-%c h Toggle hexadecimal mode", option.prefix_key);
tio_printf(" ctrl-%c l Clear screen", option.prefix_key);
@ -443,6 +445,22 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c
tio_printf(" RI : %s", (state & TIOCM_RI) ? "HIGH" : "LOW");
break;
case KEY_F:
if (option.log)
{
log_close();
option.log = false;
}
else
{
if (log_open(option.log_filename) == 0)
{
option.log = true;
}
}
tio_printf("Switched log to file %s", option.log ? "on" : "off");
break;
case KEY_G:
tio_printf("Please enter which serial line number to toggle:");
tio_printf(" DTR (0)");