diff --git a/README.md b/README.md index 0033c64..c58d4b4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/man/tio.1.in b/man/tio.1.in index 01349d8..87cd9ef 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -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" diff --git a/src/log.c b/src/log.c index aa86847..d0cf543 100644 --- a/src/log.c +++ b/src/log.c @@ -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,20 +163,22 @@ void log_printf(const char *format, ...) void log_putc(char c) { - if (fp != NULL) + if (fp == NULL) { - if (option.log_strip) - { - if (!log_strip(c)) - { - fputc(c, fp); - } - } - else + return; + } + + if (option.log_strip) + { + if (!log_strip(c)) { fputc(c, fp); } } + else + { + 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; } diff --git a/src/log.h b/src/log.h index d2a2188..b24114e 100644 --- a/src/log.h +++ b/src/log.h @@ -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); diff --git a/src/options.c b/src/options.c index cac6222..eb75144 100644 --- a/src/options.c +++ b/src/options.c @@ -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); } diff --git a/src/tty.c b/src/tty.c index b27c73f..7329303 100644 --- a/src/tty.c +++ b/src/tty.c @@ -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)");