mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Add support for automatically generated log filename
Automatically generate log filename if none is provided. The auto generated file name is on the form: "tio_DEVICE_YYYY-MM-DDTHH:MM:SS.log"
This commit is contained in:
parent
d8a822a3fb
commit
45210bc741
3 changed files with 45 additions and 6 deletions
|
|
@ -57,7 +57,7 @@ option is provided, tio will exit if the device is not present or an established
|
|||
Enable local echo.
|
||||
|
||||
.TP
|
||||
.BR \-t ", " \-\-timestamp[=<format>]
|
||||
.BR \-t ", " \-\-timestamp[=\fI<format>\fR\fB]
|
||||
|
||||
Enable timestamp. Optionally you can specify any of the following timestamp formats:
|
||||
.RS
|
||||
|
|
@ -79,9 +79,9 @@ Default format is
|
|||
List available serial devices.
|
||||
|
||||
.TP
|
||||
.BR \-l ", " "\-\-log " \fI<filename>
|
||||
.BR \-l ", " \-\-log[=\fI<filename>\fR\fB]
|
||||
|
||||
Log to file.
|
||||
Log to file. If no filename is provided the filename will be automatically generated.
|
||||
|
||||
.TP
|
||||
.BR \-m ", " "\-\-map " \fI<flags>
|
||||
|
|
|
|||
39
src/log.c
39
src/log.c
|
|
@ -24,6 +24,9 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <libgen.h>
|
||||
#include "options.h"
|
||||
#include "print.h"
|
||||
#include "error.h"
|
||||
|
|
@ -31,8 +34,32 @@
|
|||
static FILE *fp;
|
||||
static bool log_error = false;
|
||||
|
||||
static char *date_time(void)
|
||||
{
|
||||
static char date_time_string[50];
|
||||
struct tm *tm;
|
||||
struct timeval tv;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
tm = localtime(&tv.tv_sec);
|
||||
strftime(date_time_string, sizeof(date_time_string), "%Y-%m-%dT%H:%M:%S", tm);
|
||||
|
||||
return date_time_string;
|
||||
}
|
||||
|
||||
void log_open(const char *filename)
|
||||
{
|
||||
static char automatic_filename[400];
|
||||
|
||||
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;
|
||||
option.log_filename = automatic_filename;
|
||||
}
|
||||
|
||||
fp = fopen(filename, "w+");
|
||||
|
||||
if (fp == NULL)
|
||||
|
|
@ -46,20 +73,32 @@ void log_open(const char *filename)
|
|||
void log_write(char c)
|
||||
{
|
||||
if (fp != NULL)
|
||||
{
|
||||
fputc(c, fp);
|
||||
}
|
||||
}
|
||||
|
||||
void log_close(void)
|
||||
{
|
||||
if (fp != NULL)
|
||||
{
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
|
||||
void log_exit(void)
|
||||
{
|
||||
if (option.log)
|
||||
{
|
||||
log_close();
|
||||
}
|
||||
|
||||
if (log_error)
|
||||
{
|
||||
error_printf("Could not open log file %s (%s)", option.log_filename, strerror(errno));
|
||||
}
|
||||
else
|
||||
{
|
||||
tio_printf("Saved log to file %s", option.log_filename);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ void print_help(char *argv[])
|
|||
printf(" -e, --local-echo Enable local echo\n");
|
||||
printf(" -t, --timestamp[=<format>] Enable timestamp (default: 24hour)\n");
|
||||
printf(" -L, --list-devices List available serial devices\n");
|
||||
printf(" -l, --log <filename> Log to file\n");
|
||||
printf(" -l, --log[=<filename>] Log to file\n");
|
||||
printf(" -m, --map <flags> Map special characters\n");
|
||||
printf(" -c, --color <0..255> Colorize tio text\n");
|
||||
printf(" -v, --version Display version\n");
|
||||
|
|
@ -120,7 +120,7 @@ void parse_options(int argc, char *argv[])
|
|||
{"local-echo", no_argument, 0, 'e'},
|
||||
{"timestamp", optional_argument, 0, 't'},
|
||||
{"list-devices", no_argument, 0, 'L'},
|
||||
{"log", required_argument, 0, 'l'},
|
||||
{"log", optional_argument, 0, 'l'},
|
||||
{"map", required_argument, 0, 'm'},
|
||||
{"color", required_argument, 0, 'c'},
|
||||
{"version", no_argument, 0, 'v'},
|
||||
|
|
@ -132,7 +132,7 @@ void parse_options(int argc, char *argv[])
|
|||
int option_index = 0;
|
||||
|
||||
/* Parse argument using getopt_long */
|
||||
c = getopt_long(argc, argv, "b:d:f:s:p:o:net::Ll:m:c:vh", long_options, &option_index);
|
||||
c = getopt_long(argc, argv, "b:d:f:s:p:o:net::Ll::m:c:vh", long_options, &option_index);
|
||||
|
||||
/* Detect the end of the options */
|
||||
if (c == -1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue