Add --log-directory option

For specifying directory path in which to save automatically named log
files.
This commit is contained in:
Martin Lund 2024-04-04 12:31:39 +02:00
parent 83f826349b
commit 70913fe120
7 changed files with 65 additions and 7 deletions

View file

@ -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<path>
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"

View file

@ -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

View file

@ -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,26 +59,42 @@ 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());
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;
// 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();

View file

@ -24,6 +24,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <errno.h>
#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;
}

View file

@ -21,6 +21,8 @@
#pragma once
#include <stdbool.h>
#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);

View file

@ -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 <filename> Set log filename\n");
printf(" --log-directory <path> 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 <flags> 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;

View file

@ -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;