mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Add --log-directory option
For specifying directory path in which to save automatically named log files.
This commit is contained in:
parent
83f826349b
commit
70913fe120
7 changed files with 65 additions and 7 deletions
|
|
@ -131,7 +131,7 @@ List available serial devices by ID.
|
||||||
|
|
||||||
Enable log to file.
|
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.
|
tio_DEVICE_YYYY-MM-DDTHH:MM:SS.log.
|
||||||
|
|
||||||
The filename can be manually set using the \-\-log-file option.
|
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.
|
Set log filename.
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.BR " \-\-log\-directory \fI<path>
|
||||||
|
|
||||||
|
Set log directory path in which to save automatically named log files.
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.BR " \-\-log\-append
|
.BR " \-\-log\-append
|
||||||
|
|
||||||
|
|
@ -437,6 +442,8 @@ Disable automatic connect
|
||||||
Enable log to file
|
Enable log to file
|
||||||
.IP "\fBlog-file"
|
.IP "\fBlog-file"
|
||||||
Set log filename
|
Set log filename
|
||||||
|
.IP "\fBlog-directory"
|
||||||
|
Set log directory path in which to save automatically named log files.
|
||||||
.IP "\fBlog-append"
|
.IP "\fBlog-append"
|
||||||
Append to log file
|
Append to log file
|
||||||
.IP "\fBlog-strip"
|
.IP "\fBlog-strip"
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ _tio()
|
||||||
-e --local-echo \
|
-e --local-echo \
|
||||||
-l --log \
|
-l --log \
|
||||||
--log-file \
|
--log-file \
|
||||||
|
--log-directory \
|
||||||
--log-append \
|
--log-append \
|
||||||
--log-strip \
|
--log-strip \
|
||||||
-m --map \
|
-m --map \
|
||||||
|
|
@ -94,6 +95,10 @@ _tio()
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
|
--log-directory)
|
||||||
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
--log-append)
|
--log-append)
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||||
return 0
|
return 0
|
||||||
|
|
|
||||||
27
src/log.c
27
src/log.c
|
|
@ -33,6 +33,7 @@
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
|
#include "misc.h"
|
||||||
|
|
||||||
#define IS_ESC_CSI_INTERMEDIATE_CHAR(c) ((c >= 0x20) && (c <= 0x3F))
|
#define IS_ESC_CSI_INTERMEDIATE_CHAR(c) ((c >= 0x20) && (c <= 0x3F))
|
||||||
#define IS_ESC_END_CHAR(c) ((c >= 0x30) && (c <= 0x7E))
|
#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)
|
int log_open(const char *filename)
|
||||||
{
|
{
|
||||||
static char automatic_filename[400];
|
char *automatic_filename;
|
||||||
|
char *dir_plus_automatic_filename;
|
||||||
|
|
||||||
if (filename == NULL)
|
if (filename == NULL)
|
||||||
{
|
{
|
||||||
// Generate filename if none provided ("tio_DEVICE_YYYY-MM-DDTHH:MM:SS.log")
|
// 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;
|
filename = automatic_filename;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log_filename = filename;
|
log_filename = filename;
|
||||||
|
|
||||||
// Open log file
|
// Open log file
|
||||||
if (option.log_append)
|
if (option.log_append)
|
||||||
{
|
{
|
||||||
// Appends to existing log file
|
// Append to existing log file
|
||||||
fp = fopen(filename, "a+");
|
fp = fopen(filename, "a+");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Truncates existing log file
|
// Truncate existing log file
|
||||||
fp = fopen(filename, "w+");
|
fp = fopen(filename, "w+");
|
||||||
}
|
}
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
|
|
@ -202,7 +219,7 @@ void log_close(void)
|
||||||
|
|
||||||
void log_exit(void)
|
void log_exit(void)
|
||||||
{
|
{
|
||||||
if (option.log)
|
if ((option.log) && (log_filename != NULL))
|
||||||
{
|
{
|
||||||
tio_printf("Saved log to file %s", log_filename);
|
tio_printf("Saved log to file %s", log_filename);
|
||||||
log_close();
|
log_close();
|
||||||
|
|
|
||||||
17
src/misc.c
17
src/misc.c
|
|
@ -24,6 +24,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
|
|
@ -70,3 +71,19 @@ int ctrl_key_code(unsigned char key)
|
||||||
|
|
||||||
return -1;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define UNUSED(expr) do { (void)(expr); } while (0)
|
#define UNUSED(expr) do { (void)(expr); } while (0)
|
||||||
|
|
||||||
char * current_time(void);
|
char * current_time(void);
|
||||||
|
|
@ -29,6 +31,7 @@ long string_to_long(char *string);
|
||||||
int ctrl_key_code(unsigned char key);
|
int ctrl_key_code(unsigned char key);
|
||||||
void alert_connect(void);
|
void alert_connect(void);
|
||||||
void alert_disconnect(void);
|
void alert_disconnect(void);
|
||||||
|
bool fs_dir_exists(const char *path);
|
||||||
|
|
||||||
extern char key_hit;
|
extern char key_hit;
|
||||||
int xymodem_send(int sio, const char *filename, char mode);
|
int xymodem_send(int sio, const char *filename, char mode);
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ enum opt_t
|
||||||
OPT_NONE,
|
OPT_NONE,
|
||||||
OPT_TIMESTAMP_FORMAT,
|
OPT_TIMESTAMP_FORMAT,
|
||||||
OPT_LOG_FILE,
|
OPT_LOG_FILE,
|
||||||
|
OPT_LOG_DIRECTORY,
|
||||||
OPT_LOG_STRIP,
|
OPT_LOG_STRIP,
|
||||||
OPT_LOG_APPEND,
|
OPT_LOG_APPEND,
|
||||||
OPT_LINE_PULSE_DURATION,
|
OPT_LINE_PULSE_DURATION,
|
||||||
|
|
@ -81,6 +82,7 @@ struct option_t option =
|
||||||
.log = false,
|
.log = false,
|
||||||
.log_append = false,
|
.log_append = false,
|
||||||
.log_filename = NULL,
|
.log_filename = NULL,
|
||||||
|
.log_directory = NULL,
|
||||||
.log_strip = false,
|
.log_strip = false,
|
||||||
.local_echo = false,
|
.local_echo = false,
|
||||||
.timestamp = TIMESTAMP_NONE,
|
.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, --list-devices List available serial devices by ID\n");
|
||||||
printf(" -l, --log Enable log to file\n");
|
printf(" -l, --log Enable log to file\n");
|
||||||
printf(" --log-file <filename> Set log filename\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-append Append to log file\n");
|
||||||
printf(" --log-strip Strip control characters and escape sequences\n");
|
printf(" --log-strip Strip control characters and escape sequences\n");
|
||||||
printf(" -m, --map <flags> Map characters\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' },
|
{"list-devices", no_argument, 0, 'L' },
|
||||||
{"log", no_argument, 0, 'l' },
|
{"log", no_argument, 0, 'l' },
|
||||||
{"log-file", required_argument, 0, OPT_LOG_FILE },
|
{"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-append", no_argument, 0, OPT_LOG_APPEND },
|
||||||
{"log-strip", no_argument, 0, OPT_LOG_STRIP },
|
{"log-strip", no_argument, 0, OPT_LOG_STRIP },
|
||||||
{"socket", required_argument, 0, 'S' },
|
{"socket", required_argument, 0, 'S' },
|
||||||
|
|
@ -399,6 +403,10 @@ void options_parse(int argc, char *argv[])
|
||||||
option.log_filename = optarg;
|
option.log_filename = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case OPT_LOG_DIRECTORY:
|
||||||
|
option.log_directory = optarg;
|
||||||
|
break;
|
||||||
|
|
||||||
case OPT_LOG_STRIP:
|
case OPT_LOG_STRIP:
|
||||||
option.log_strip = true;
|
option.log_strip = true;
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,7 @@ struct option_t
|
||||||
bool local_echo;
|
bool local_echo;
|
||||||
enum timestamp_t timestamp;
|
enum timestamp_t timestamp;
|
||||||
const char *log_filename;
|
const char *log_filename;
|
||||||
|
const char *log_directory;
|
||||||
const char *map;
|
const char *map;
|
||||||
const char *socket;
|
const char *socket;
|
||||||
int color;
|
int color;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue