From 148a3c1da1dd837f2403ddf1456fc0deaf21fe69 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Fri, 28 Apr 2023 20:50:31 +0200 Subject: [PATCH] Add --log-append option Add --log-append option which makes tio append to any existing log file. This also changes the default behaviour of tio from appending to overwriting any existing log file. Now you have to use this new option to make tio append. --- example/config | 1 + man/tio.1.in | 5 +++++ man/tio.1.txt | 4 ++++ src/configfile.c | 4 ++++ src/log.c | 13 +++++++++++-- src/options.c | 2 ++ src/options.h | 1 + 7 files changed, 28 insertions(+), 2 deletions(-) diff --git a/example/config b/example/config index 1b66d07..c208fba 100644 --- a/example/config +++ b/example/config @@ -22,6 +22,7 @@ no-autoconnect = disable hexadecimal = disable timestamp = disable log = disable +log-append = disable log-strip = disable local-echo = disable color = bold diff --git a/man/tio.1.in b/man/tio.1.in index 36118e5..ba2a9ad 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -141,6 +141,11 @@ The filename can be manually set using the \-\-log-file option. Set log filename. +.TP +.BR " \-\-log-append + +Append to log file. + .TP .BR " \-\-log-strip diff --git a/man/tio.1.txt b/man/tio.1.txt index 2aa04e6..252ca60 100644 --- a/man/tio.1.txt +++ b/man/tio.1.txt @@ -108,6 +108,10 @@ OPTIONS Set log filename. + --log-append + + Append to log file. + --log-strip Strip control characters and escape sequences from log. diff --git a/src/configfile.c b/src/configfile.c index d3dbd16..6715059 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -198,6 +198,10 @@ static int data_handler(void *user, const char *section, const char *name, asprintf(&c.log_filename, "%s", value); option.log_filename = c.log_filename; } + else if (!strcmp(name, "log-append")) + { + option.log_append = read_boolean(value, name); + } else if (!strcmp(name, "log-strip")) { option.log_strip = read_boolean(value, name); diff --git a/src/log.c b/src/log.c index d0cf543..de3be86 100644 --- a/src/log.c +++ b/src/log.c @@ -69,8 +69,17 @@ int log_open(const char *filename) log_filename = filename; - // Open log file in append write mode - fp = fopen(filename, "a+"); + // Open log file + if (option.log_append) + { + // Appends to existing log file + fp = fopen(filename, "a+"); + } + else + { + // Truncates existing log file + fp = fopen(filename, "w+"); + } if (fp == NULL) { tio_warning_printf("Could not open log file %s (%s)", filename, strerror(errno)); diff --git a/src/options.c b/src/options.c index eb75144..6217cc4 100644 --- a/src/options.c +++ b/src/options.c @@ -74,6 +74,7 @@ struct option_t option = .ri_pulse_duration = 100, .no_autoconnect = false, .log = false, + .log_append = false, .log_filename = NULL, .log_strip = false, .local_echo = false, @@ -119,6 +120,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-append Append to log file\n"); printf(" --log-strip Strip control characters and escape sequences\n"); printf(" -m, --map Map characters\n"); printf(" -c, --color 0..255|bold|none|list Colorize tio text (default: bold)\n"); diff --git a/src/options.h b/src/options.h index 8ae556d..b18d8ff 100644 --- a/src/options.h +++ b/src/options.h @@ -48,6 +48,7 @@ struct option_t unsigned int ri_pulse_duration; bool no_autoconnect; bool log; + bool log_append; bool log_strip; bool local_echo; enum timestamp_t timestamp;