diff --git a/man/tio.1.in b/man/tio.1.in index c37f225..c3658a3 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -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[=] +.BR \-t ", " \-\-timestamp[=\fI\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 +.BR \-l ", " \-\-log[=\fI\fR\fB] -Log to file. +Log to file. If no filename is provided the filename will be automatically generated. .TP .BR \-m ", " "\-\-map " \fI diff --git a/src/log.c b/src/log.c index 9b2c15e..4aa774f 100644 --- a/src/log.c +++ b/src/log.c @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include #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); + } } diff --git a/src/options.c b/src/options.c index ed68437..09eab0a 100644 --- a/src/options.c +++ b/src/options.c @@ -68,7 +68,7 @@ void print_help(char *argv[]) printf(" -e, --local-echo Enable local echo\n"); printf(" -t, --timestamp[=] Enable timestamp (default: 24hour)\n"); printf(" -L, --list-devices List available serial devices\n"); - printf(" -l, --log Log to file\n"); + printf(" -l, --log[=] Log to file\n"); printf(" -m, --map 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)