From 89ca11678cef8c7768d0d2bea2a79e73988ea8e9 Mon Sep 17 00:00:00 2001 From: Slyvtt Date: Tue, 19 Apr 2022 18:36:09 +0200 Subject: [PATCH 1/3] 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(); From 85314f8310ff134cd77977d27084aee5addb2491 Mon Sep 17 00:00:00 2001 From: Slyvtt Date: Wed, 20 Apr 2022 13:07:30 +0200 Subject: [PATCH 2/3] change option name to -q, --quiet and improved code layout --- fxlink/interactive.c | 18 ++++++------------ fxlink/main.c | 25 ++++++++++++------------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/fxlink/interactive.c b/fxlink/interactive.c index 806a764..109279e 100644 --- a/fxlink/interactive.c +++ b/fxlink/interactive.c @@ -108,45 +108,39 @@ static void message_finish(message_t *msg) } if(!strncmp(msg->header.type, "text", 16)) { - /* 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) - { + 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) - { + else { fwrite(msg->output, 1, msg->header.size, stdout); if(msg->output[msg->header.size - 1] != '\n') printf("\n"); } } - if (loginfile) - { + if (loginfile) { FILE *fp = fopen( userlogfilename, "a" ); if(!fp) { err("could not save to '%s': %m", userlogfilename); return; } - if (!silentmode) - { + 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) - { + else { fwrite(msg->output, 1, msg->header.size, fp); if(msg->output[msg->header.size - 1] != '\n') fprintf(fp, "\n"); } + fclose( fp ); } return; diff --git a/fxlink/main.c b/fxlink/main.c index 0e4fcb7..1892eda 100644 --- a/fxlink/main.c +++ b/fxlink/main.c @@ -40,8 +40,8 @@ static const char *help_string = " --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" +" -q, --quiet Activate quiet mode, i.e. minimum verbosity and very\n" +" limited decorations (no extra messages).\n" "\n" "Device filters:\n" " A device filter is a comma-separated list of properties that a device has\n" @@ -76,8 +76,7 @@ int main(int argc, char **argv) //--- enum { LIBUSB_LOG=1 }; - enum { SILENT_MODE=2 }; - enum { LOG_IN_FILE=3 }; + enum { LOG_IN_FILE=2 }; const struct option longs[] = { { "help", no_argument, NULL, 'h' }, { "list", no_argument, NULL, 'l' }, @@ -85,12 +84,12 @@ 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 }, + { "quiet", no_argument, NULL, 'q' }, { "fxlink-log", optional_argument, NULL, LOG_IN_FILE }, }; while(option >= 0 && option != '?') - switch((option = getopt_long(argc, argv, "hlbsif:w::", longs, NULL))) + switch((option = getopt_long(argc, argv, "hlbsiqf:w::", longs, NULL))) { case 'h': fprintf(stderr, help_string, argv[0]); @@ -115,24 +114,24 @@ 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"); + case 'q': + printf("Enabling quiet mode (i.e. minimum verbosity)\n"); silentmode = true; break; case LOG_IN_FILE: printf("Enabling Log in File Mode\n"); loginfile = true; - if (optarg) - { + if (optarg) { asprintf( &userlogfilename, "%s", optarg ); } - else - { + 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 ); + 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; From 6103d852d56e0a8e6a68373c65fea2e6342e5951 Mon Sep 17 00:00:00 2001 From: Slyvtt Date: Wed, 20 Apr 2022 18:07:58 +0200 Subject: [PATCH 3/3] added -u, --unmount option to force unmounting the disk after end of operations --- fxlink/main.c | 10 +++++++++- fxlink/ud2.c | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/fxlink/main.c b/fxlink/main.c index 1892eda..0c13fad 100644 --- a/fxlink/main.c +++ b/fxlink/main.c @@ -42,6 +42,8 @@ static const char *help_string = " missing, a generic filename will be created.\n" " -q, --quiet Activate quiet mode, i.e. minimum verbosity and very\n" " limited decorations (no extra messages).\n" +" -u, --unmount Force unmount disk at the end of operations, even if\n" +" not mounted here\n" "\n" "Device filters:\n" " A device filter is a comma-separated list of properties that a device has\n" @@ -64,6 +66,7 @@ static const char *help_string = bool silentmode = false; bool loginfile = false; char *userlogfilename = NULL; +bool forceunmount = false; int main(int argc, char **argv) { @@ -86,10 +89,11 @@ int main(int argc, char **argv) { "libusb-log", required_argument, NULL, LIBUSB_LOG }, { "quiet", no_argument, NULL, 'q' }, { "fxlink-log", optional_argument, NULL, LOG_IN_FILE }, + { "unmount", no_argument, NULL, 'u' }, }; while(option >= 0 && option != '?') - switch((option = getopt_long(argc, argv, "hlbsiqf:w::", longs, NULL))) + switch((option = getopt_long(argc, argv, "hlbsiquf:w::", longs, NULL))) { case 'h': fprintf(stderr, help_string, argv[0]); @@ -118,6 +122,10 @@ int main(int argc, char **argv) printf("Enabling quiet mode (i.e. minimum verbosity)\n"); silentmode = true; break; + case 'u': + printf("Force unmount the disk at the end.\n"); + forceunmount = true; + break; case LOG_IN_FILE: printf("Enabling Log in File Mode\n"); loginfile = true; diff --git a/fxlink/ud2.c b/fxlink/ud2.c index 5eae092..765d4e4 100644 --- a/fxlink/ud2.c +++ b/fxlink/ud2.c @@ -16,6 +16,8 @@ // UDisks2 utility functions //--- +extern bool forceunmount; + int ud2_start(UDisksClient **udc_ptr, UDisksManager **udm_ptr) { GError *error = NULL; @@ -358,7 +360,7 @@ int main_send(filter_t *filter, delay_t *delay, char **files) } /* Unmount the filesystem and eject the device if we mounted it */ - if(mounted_here) { + if(mounted_here || forceunmount) { GVariant *args = g_variant_new("a{sv}", NULL); udisks_filesystem_call_unmount_sync(fs, args, NULL, &error); if(error) err("while unmounting %s: %s", dev, error->message);