From 89ca11678cef8c7768d0d2bea2a79e73988ea8e9 Mon Sep 17 00:00:00 2001 From: Slyvtt Date: Tue, 19 Apr 2022 18:36:09 +0200 Subject: [PATCH] added --silent-mode and --fxlink-log options --- fxlink/interactive.c | 54 +++++++++++++++++++++++++++++++++++++++----- fxlink/main.c | 35 ++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/fxlink/interactive.c b/fxlink/interactive.c index a7cfdbe..806a764 100644 --- a/fxlink/interactive.c +++ b/fxlink/interactive.c @@ -20,6 +20,11 @@ static int last_message_was_video = 0; static int video_frame_count = 0; +/* external global variables coming from user arguments*/ +extern bool silentmode; +extern bool loginfile; +extern char *userlogfilename; + static char *output_file(char const *path,char const *type,char const *suffix) { char *filename = NULL; @@ -62,7 +67,7 @@ static bool message_new(message_t *msg, usb_fxlink_header_t const *h) last_message_was_video = 0; } - fprintf(stderr, "New message (v%d.%d): application '%.16s', type '%.16s', " + if (!silentmode) fprintf(stderr, "New message (v%d.%d): application '%.16s', type '%.16s', " "size %d bytes", version_major, version_minor, h->application, h->type, h->size); @@ -103,10 +108,47 @@ static void message_finish(message_t *msg) } if(!strncmp(msg->header.type, "text", 16)) { - printf("------------------\n"); - fwrite(msg->output, 1, msg->header.size, stdout); - if(msg->output[msg->header.size - 1] != '\n') printf("\n"); - printf("------------------\n"); + + /* I choose the option of maintening a console ouptut even if log in file is set */ + /* this can be removed by uncommenting the following line */ + + //if (!loginfile) + { + if (!silentmode) + { + printf("------------------\n"); + fwrite(msg->output, 1, msg->header.size, stdout); + if(msg->output[msg->header.size - 1] != '\n') printf("\n"); + printf("------------------\n"); + } + else if (silentmode) + { + fwrite(msg->output, 1, msg->header.size, stdout); + if(msg->output[msg->header.size - 1] != '\n') printf("\n"); + } + } + if (loginfile) + { + FILE *fp = fopen( userlogfilename, "a" ); + if(!fp) { + err("could not save to '%s': %m", userlogfilename); + return; + } + + if (!silentmode) + { + fprintf(fp, "------------------\n"); + fwrite(msg->output, 1, msg->header.size, fp); + if(msg->output[msg->header.size - 1] != '\n') fprintf(fp, "\n"); + fprintf(fp, "------------------\n"); + } + else if (silentmode) + { + fwrite(msg->output, 1, msg->header.size, fp); + if(msg->output[msg->header.size - 1] != '\n') fprintf(fp, "\n"); + } + fclose( fp ); + } return; } @@ -152,7 +194,7 @@ static void message_output(message_t *msg, void *buffer, int size) if(msg->size_read >= msg->header.size) { bool is_video = !strncmp(msg->header.application, "fxlink", 16) && !strncmp(msg->header.type, "video", 16); - if(!is_video) + if(!is_video && !silentmode) fprintf(stderr, "Successfully read %d bytes\n", msg->size_read); message_finish(msg); msg->valid = false; diff --git a/fxlink/main.c b/fxlink/main.c index b5a5f27..0e4fcb7 100644 --- a/fxlink/main.c +++ b/fxlink/main.c @@ -11,6 +11,8 @@ #include #include #include +#include + int main_test(libusb_device *device, libusb_context *context); @@ -36,6 +38,10 @@ static const char *help_string = " connect. If DELAY is unspecified, wait indefinitely.\n" " -f FILTER Filter which calculators can be detected and used\n" " --libusb-log=LEVEL libusb log level: NONE, ERROR, WARNING, INFO, DEBUG\n" +" --fxlink-log=file Log text data into a logfile called file. If file is\n" +" missing, a generic filename will be created.\n" +" --silent-mode Will turn output to the minimum value, without\n" +" decorations (no extra messages).\n" "\n" "Device filters:\n" " A device filter is a comma-separated list of properties that a device has\n" @@ -55,6 +61,10 @@ static const char *help_string = " serial_number=ID Matches this specific serial number. Requires write\n" " access to the device in libusb. [libusb, udisks2]\n"; +bool silentmode = false; +bool loginfile = false; +char *userlogfilename = NULL; + int main(int argc, char **argv) { int rc=1, mode=0, error=0, option=0, loglevel=LIBUSB_LOG_LEVEL_ERROR; @@ -66,6 +76,8 @@ int main(int argc, char **argv) //--- enum { LIBUSB_LOG=1 }; + enum { SILENT_MODE=2 }; + enum { LOG_IN_FILE=3 }; const struct option longs[] = { { "help", no_argument, NULL, 'h' }, { "list", no_argument, NULL, 'l' }, @@ -73,6 +85,8 @@ int main(int argc, char **argv) { "send", no_argument, NULL, 's' }, { "interactive", no_argument, NULL, 'i' }, { "libusb-log", required_argument, NULL, LIBUSB_LOG }, + { "silent-mode", no_argument, NULL, SILENT_MODE }, + { "fxlink-log", optional_argument, NULL, LOG_IN_FILE }, }; while(option >= 0 && option != '?') @@ -101,6 +115,27 @@ int main(int argc, char **argv) else fprintf(stderr, "warning: ignoring log level '%s'; should be " "NONE, ERROR, WARNING, INFO or DEBUG\n", optarg); break; + case SILENT_MODE: + printf("Enabling Silent (i.e. minimum verbosity) Mode\n"); + silentmode = true; + break; + case LOG_IN_FILE: + printf("Enabling Log in File Mode\n"); + loginfile = true; + if (optarg) + { + asprintf( &userlogfilename, "%s", optarg ); + } + else + { + time_t time_raw; + struct tm time_bd; + time(&time_raw); + localtime_r(&time_raw, &time_bd); + asprintf( &userlogfilename, "./fxlink-logfile-%04d.%02d.%02d-%02dh%02d.log", time_bd.tm_year + 1900, time_bd.tm_mon + 1, time_bd.tm_mday, time_bd.tm_hour, time_bd.tm_min ); + } + printf("Log File will be : %s \n", userlogfilename ); + break; case 'w': if(!optarg) { delay = delay_infinite();