From 70913fe120f1f8456647e00e921fc90af050d513 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Thu, 4 Apr 2024 12:31:39 +0200 Subject: [PATCH] Add --log-directory option For specifying directory path in which to save automatically named log files. --- man/tio.1.in | 9 ++++++++- src/bash-completion/tio.in | 5 +++++ src/log.c | 29 +++++++++++++++++++++++------ src/misc.c | 17 +++++++++++++++++ src/misc.h | 3 +++ src/options.c | 8 ++++++++ src/options.h | 1 + 7 files changed, 65 insertions(+), 7 deletions(-) diff --git a/man/tio.1.in b/man/tio.1.in index 41138de..501a260 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -131,7 +131,7 @@ List available serial devices by ID. Enable log to file. -The filename will be automatically generated using the following format +The log file will be automatically named using the following format tio_DEVICE_YYYY-MM-DDTHH:MM:SS.log. The filename can be manually set using the \-\-log-file option. @@ -141,6 +141,11 @@ The filename can be manually set using the \-\-log-file option. Set log filename. +.TP +.BR " \-\-log\-directory \fI + +Set log directory path in which to save automatically named log files. + .TP .BR " \-\-log\-append @@ -437,6 +442,8 @@ Disable automatic connect Enable log to file .IP "\fBlog-file" Set log filename +.IP "\fBlog-directory" +Set log directory path in which to save automatically named log files. .IP "\fBlog-append" Append to log file .IP "\fBlog-strip" diff --git a/src/bash-completion/tio.in b/src/bash-completion/tio.in index 6466db1..9bf1629 100644 --- a/src/bash-completion/tio.in +++ b/src/bash-completion/tio.in @@ -22,6 +22,7 @@ _tio() -e --local-echo \ -l --log \ --log-file \ + --log-directory \ --log-append \ --log-strip \ -m --map \ @@ -94,6 +95,10 @@ _tio() COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 ;; + --log-directory) + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; --log-append) COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 diff --git a/src/log.c b/src/log.c index de3be86..f940d44 100644 --- a/src/log.c +++ b/src/log.c @@ -33,6 +33,7 @@ #include "options.h" #include "print.h" #include "error.h" +#include "misc.h" #define IS_ESC_CSI_INTERMEDIATE_CHAR(c) ((c >= 0x20) && (c <= 0x3F)) #define IS_ESC_END_CHAR(c) ((c >= 0x30) && (c <= 0x7E)) @@ -58,13 +59,29 @@ static char *date_time(void) int log_open(const char *filename) { - static char automatic_filename[400]; + char *automatic_filename; + char *dir_plus_automatic_filename; if (filename == NULL) { // 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; + asprintf(&automatic_filename, "tio_%s_%s.log", basename((char *)option.tty_device), date_time()); + + if (option.log_directory != NULL) + { + if (fs_dir_exists(option.log_directory) == false) + { + tio_error_printf("Log directory not found"); + exit(EXIT_FAILURE); + } + + asprintf(&dir_plus_automatic_filename, "%s/%s", option.log_directory, automatic_filename); + filename = dir_plus_automatic_filename; + } + else + { + filename = automatic_filename; + } } log_filename = filename; @@ -72,12 +89,12 @@ int log_open(const char *filename) // Open log file if (option.log_append) { - // Appends to existing log file + // Append to existing log file fp = fopen(filename, "a+"); } else { - // Truncates existing log file + // Truncate existing log file fp = fopen(filename, "w+"); } if (fp == NULL) @@ -202,7 +219,7 @@ void log_close(void) void log_exit(void) { - if (option.log) + if ((option.log) && (log_filename != NULL)) { tio_printf("Saved log to file %s", log_filename); log_close(); diff --git a/src/misc.c b/src/misc.c index 1983950..6770a28 100644 --- a/src/misc.c +++ b/src/misc.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include "error.h" @@ -70,3 +71,19 @@ int ctrl_key_code(unsigned char key) return -1; } + +bool fs_dir_exists(const char *path) +{ + struct stat st; + + if (stat(path, &st) != 0) + { + return false; + } + else if (!S_ISDIR(st.st_mode)) + { + return false; + } + + return true; +} diff --git a/src/misc.h b/src/misc.h index d9a8e2b..e949db7 100644 --- a/src/misc.h +++ b/src/misc.h @@ -21,6 +21,8 @@ #pragma once +#include + #define UNUSED(expr) do { (void)(expr); } while (0) char * current_time(void); @@ -29,6 +31,7 @@ long string_to_long(char *string); int ctrl_key_code(unsigned char key); void alert_connect(void); void alert_disconnect(void); +bool fs_dir_exists(const char *path); extern char key_hit; int xymodem_send(int sio, const char *filename, char mode); diff --git a/src/options.c b/src/options.c index 1f70122..510259e 100644 --- a/src/options.c +++ b/src/options.c @@ -46,6 +46,7 @@ enum opt_t OPT_NONE, OPT_TIMESTAMP_FORMAT, OPT_LOG_FILE, + OPT_LOG_DIRECTORY, OPT_LOG_STRIP, OPT_LOG_APPEND, OPT_LINE_PULSE_DURATION, @@ -81,6 +82,7 @@ struct option_t option = .log = false, .log_append = false, .log_filename = NULL, + .log_directory = NULL, .log_strip = false, .local_echo = false, .timestamp = TIMESTAMP_NONE, @@ -129,6 +131,7 @@ void print_help(char *argv[]) printf(" -L, --list-devices List available serial devices by ID\n"); printf(" -l, --log Enable log to file\n"); printf(" --log-file Set log filename\n"); + printf(" --log-directory Set log directory path (for automatic named logs)\n"); printf(" --log-append Append to log file\n"); printf(" --log-strip Strip control characters and escape sequences\n"); printf(" -m, --map Map characters\n"); @@ -295,6 +298,7 @@ void options_parse(int argc, char *argv[]) {"list-devices", no_argument, 0, 'L' }, {"log", no_argument, 0, 'l' }, {"log-file", required_argument, 0, OPT_LOG_FILE }, + {"log-directory", required_argument, 0, OPT_LOG_DIRECTORY }, {"log-append", no_argument, 0, OPT_LOG_APPEND }, {"log-strip", no_argument, 0, OPT_LOG_STRIP }, {"socket", required_argument, 0, 'S' }, @@ -399,6 +403,10 @@ void options_parse(int argc, char *argv[]) option.log_filename = optarg; break; + case OPT_LOG_DIRECTORY: + option.log_directory = optarg; + break; + case OPT_LOG_STRIP: option.log_strip = true; break; diff --git a/src/options.h b/src/options.h index e909b98..1cc9a60 100644 --- a/src/options.h +++ b/src/options.h @@ -54,6 +54,7 @@ struct option_t bool local_echo; enum timestamp_t timestamp; const char *log_filename; + const char *log_directory; const char *map; const char *socket; int color;