All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.com
Subject: [PATCH v3 02/16] btrfs-progs: add global verbose and quiet options and helper functions
Date: Fri, 12 Jun 2020 18:56:06 +0800	[thread overview]
Message-ID: <20200612105606.18210-1-anand.jain@oracle.com> (raw)
In-Reply-To: <1574678357-22222-3-git-send-email-anand.jain@oracle.com>

Add btrfs(8) global --verbose and --quiet command options to show
verbose or no output from the sub-commands.
By introducing global a %bconf::verbose memeber to transpire the same
down to the sub-command.
Further the added helper function pr_verbose() helps to logs the verbose
messages, based on the state of the %bconf::verbose. And further HELPINFO_
defines are provides for the usage.

Suggested-by: David Sterba <dsterba@suse.com>
Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
v3:
  Add define MUST_LOG
  Add comment about the argument %level in the function pr_verbose()
v2:
  Add missing -q along with --quiet.
  Create and use bconf_be_verbose() and bconf_be_quiet().
  Change help wordings for verbose and quiet.
  Define and use BTRFS_BCONF_UNSET and BTRFS_BCONF_QUIET.
  Use HELPINFO_INSERT_GLOBALS.

 btrfs.c           | 20 ++++++++++++++++++--
 common/help.h     |  5 +++++
 common/messages.c | 30 ++++++++++++++++++++++++++++++
 common/messages.h |  4 ++++
 common/utils.c    | 14 ++++++++++++++
 common/utils.h    | 11 +++++++++++
 6 files changed, 82 insertions(+), 2 deletions(-)

diff --git a/btrfs.c b/btrfs.c
index 72dad6fb3983..eb3b6183eb21 100644
--- a/btrfs.c
+++ b/btrfs.c
@@ -27,7 +27,7 @@
 #include "common/box.h"
 
 static const char * const btrfs_cmd_group_usage[] = {
-	"btrfs [--help] [--version] [--format <format>] <group> [<group>...] <command> [<args>]",
+	"btrfs [--help] [--version] [--format <format>] [-v|--verbose] [-q|--quiet] <group> [<group>...] <command> [<args>]",
 	NULL
 };
 
@@ -248,6 +248,8 @@ static int handle_global_options(int argc, char **argv)
 		{ "version", no_argument, NULL, OPT_VERSION },
 		{ "format", required_argument, NULL, OPT_FORMAT },
 		{ "full", no_argument, NULL, OPT_FULL },
+		{ "verbose", no_argument, NULL, 'v' },
+		{ "quiet", no_argument, NULL, 'q' },
 		{ NULL, 0, NULL, 0}
 	};
 	int shift;
@@ -259,7 +261,7 @@ static int handle_global_options(int argc, char **argv)
 	while (1) {
 		int c;
 
-		c = getopt_long(argc, argv, "+", long_options, NULL);
+		c = getopt_long(argc, argv, "+vq", long_options, NULL);
 		if (c < 0)
 			break;
 
@@ -270,6 +272,12 @@ static int handle_global_options(int argc, char **argv)
 		case OPT_FORMAT:
 			handle_output_format(optarg);
 			break;
+		case 'v':
+			bconf_be_verbose();
+			break;
+		case 'q':
+			bconf_be_quiet();
+			break;
 		default:
 			fprintf(stderr, "Unknown global option: %s\n",
 					argv[optind - 1]);
@@ -310,6 +318,14 @@ static void handle_special_globals(int shift, int argc, char **argv)
 			cmd_execute(&cmd_struct_version, argc, argv);
 			exit(0);
 		}
+
+	for (i = 0; i < shift; i++)
+		if (strcmp(argv[i], "--verbose") == 0)
+			bconf_be_verbose();
+
+	for (i = 0; i < shift; i++)
+		if (strcmp(argv[i], "--quiet") == 0)
+			bconf_be_quiet();
 }
 
 static const struct cmd_group btrfs_cmd_group = {
diff --git a/common/help.h b/common/help.h
index 91874abfe207..dbc5259d2277 100644
--- a/common/help.h
+++ b/common/help.h
@@ -60,6 +60,11 @@
 #define HELPINFO_INSERT_GLOBALS		"",					\
 					"Global options:"
 #define HELPINFO_INSERT_FORMAT		"--foramt TYPE"
+/*
+ * Global verbose option for the sub-commands
+ */
+#define HELPINFO_INSERT_VERBOSE	"-v|--verbose       increase output verbosity"
+#define HELPINFO_INSERT_QUIET	"-q|--quiet         print only errors"
 
 struct cmd_struct;
 struct cmd_group;
diff --git a/common/messages.c b/common/messages.c
index 0e5694ecd467..43ababe77519 100644
--- a/common/messages.c
+++ b/common/messages.c
@@ -17,6 +17,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include "common/messages.h"
+#include "common/utils.h"
 
 __attribute__ ((format (printf, 1, 2)))
 void __btrfs_warning(const char *fmt, ...)
@@ -75,3 +76,32 @@ int __btrfs_error_on(int condition, const char *fmt, ...)
 
 	return 1;
 }
+
+/*
+ * Print verbose helper function
+ * level: Minimum verbose level at which the message has to be printed.
+ *
+ * Values for argument level:
+ * MUST_LOG - Will log the message unless a quiet option is set.
+ *            Used where messages have to be printed for backward compatibility.
+ * > 0        Prints the message at the corresponding level.
+ */
+__attribute__ ((format (printf, 2, 3)))
+void pr_verbose(int level, const char *fmt, ...)
+{
+	va_list args;
+
+	if (bconf.verbose == BTRFS_BCONF_QUIET || level == BTRFS_BCONF_QUIET)
+		return;
+
+	/*
+	 * level is set by the threads requesting to print only if the command
+	 * verbose option is higher than the level.
+	 */
+	if (bconf.verbose < level)
+		return;
+
+	va_start(args, fmt);
+	vfprintf(stdout, fmt, args);
+	va_end(args);
+}
diff --git a/common/messages.h b/common/messages.h
index 596047948fef..288b32a77c10 100644
--- a/common/messages.h
+++ b/common/messages.h
@@ -96,3 +96,7 @@ __attribute__ ((format (printf, 2, 3)))
 int __btrfs_error_on(int condition, const char *fmt, ...);
 
 #endif
+
+#define	MUST_LOG	-1
+__attribute__ ((format (printf, 2, 3)))
+void pr_verbose(int level, const char *fmt, ...);
diff --git a/common/utils.c b/common/utils.c
index ebc50de2c143..0301efb0a348 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -1679,6 +1679,20 @@ u8 rand_u8(void)
 void btrfs_config_init(void)
 {
 	bconf.output_format = CMD_FORMAT_TEXT;
+	bconf.verbose = BTRFS_BCONF_UNSET;
+}
+
+void bconf_be_verbose(void)
+{
+	if (bconf.verbose == BTRFS_BCONF_UNSET)
+		bconf.verbose = 1;
+	else
+		bconf.verbose++;
+}
+
+void bconf_be_quiet(void)
+{
+	bconf.verbose = BTRFS_BCONF_QUIET;
 }
 
 /* Returns total size of main memory in bytes, -1UL if error. */
diff --git a/common/utils.h b/common/utils.h
index 1172618b8bb1..c650819644e5 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -117,16 +117,27 @@ unsigned long total_memory(void);
 void print_device_info(struct btrfs_device *device, char *prefix);
 void print_all_devices(struct list_head *devices);
 
+#define BTRFS_BCONF_UNSET	-1
+#define BTRFS_BCONF_QUIET	 0
 /*
  * Global program state, configurable by command line and available to
  * functions without extra context passing.
  */
 struct btrfs_config {
 	unsigned int output_format;
+
+	/* values
+	 *   BTRFS_BCONF_QUIET
+	 *   BTRFS_BCONF_UNSET
+	 *   > 0: Verbose level
+	 */
+	int verbose;
 };
 extern struct btrfs_config bconf;
 
 void btrfs_config_init(void);
+void bconf_be_verbose(void);
+void bconf_be_quiet(void);
 
 /* Pseudo random number generator wrappers */
 int rand_int(void);
-- 
2.25.1


  reply	other threads:[~2020-06-12 10:57 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-25 10:39 [PATCH v2 00/16] btrfs-progs: global verbose and quiet option Anand Jain
2019-11-25 10:39 ` [PATCH 01/16] btrfs-progs: split global help HELPINFO_INSERT_GLOBALS Anand Jain
2019-11-25 10:39 ` [PATCH v2 02/16] btrfs-progs: add global verbose and quiet options and helper functions Anand Jain
2020-06-12 10:56   ` Anand Jain [this message]
2020-06-12 15:39     ` [PATCH v3 " David Sterba
2020-06-12 22:48       ` Anand Jain
2020-06-23 16:44         ` David Sterba
2020-06-29 15:36     ` David Sterba
2019-11-25 10:39 ` [PATCH v2 03/16] btrfs-progs: send: use global verbose and quiet options Anand Jain
2020-06-12 10:58   ` [PATCH v3 " Anand Jain
2019-11-25 10:39 ` [PATCH v2 04/16] btrfs-progs: receive: " Anand Jain
2020-06-12 11:24   ` [PATCH v3 " Anand Jain
2019-11-25 10:39 ` [PATCH v2 05/16] btrfs-progs: subvolume delete: use global verbose option Anand Jain
2020-06-12 11:24   ` [PATCH v3 " Anand Jain
2019-11-25 10:39 ` [PATCH v2 06/16] btrfs-progs: filesystem defragment: " Anand Jain
2020-06-08  6:31   ` Anand Jain
2020-06-11 16:56     ` [PATCH v3 " Anand Jain
2020-06-12 11:25   ` [PATCH v4 " Anand Jain
2019-11-25 10:39 ` [PATCH v2 07/16] btrfs-progs: balance start: " Anand Jain
2020-06-12 11:25   ` [PATCH v3 " Anand Jain
2019-11-25 10:39 ` [PATCH v2 08/16] btrfs-progs: balance status: " Anand Jain
2020-06-12 11:25   ` [PATCH v3 " Anand Jain
2019-11-25 10:39 ` [PATCH v2 09/16] btrfs-progs: rescue chunk-recover: " Anand Jain
2020-06-12 11:25   ` [PATCH v3 " Anand Jain
2019-11-25 10:39 ` [PATCH v2 10/16] btrfs-progs: rescue super-recover: " Anand Jain
2020-06-12 11:25   ` [PATCH v3 " Anand Jain
2019-11-25 10:39 ` [PATCH v2 11/16] btrfs-progs: restore: " Anand Jain
2020-06-12 11:26   ` [PATCH v3 " Anand Jain
2019-11-25 10:39 ` [PATCH v2 12/16] btrfs-progs: inspect-internal inode-resolve: use global verbose Anand Jain
2020-06-12 11:26   ` [PATCH v3 " Anand Jain
2019-11-25 10:39 ` [PATCH v2 13/16] btrfs-progs: inspect-internal logical-resolve: use global verbose option Anand Jain
2020-06-12 11:26   ` [PATCH v3 " Anand Jain
2019-11-25 10:39 ` [PATCH 14/16] btrfs-progs: refactor btrfs_scan_devices() to accept verbose argument Anand Jain
2019-11-25 10:39 ` [PATCH v2 15/16] btrfs-progs: device scan: add verbose option Anand Jain
2019-11-25 10:39 ` [PATCH 16/16] btrfs-progs: device scan: add quiet option Anand Jain
2019-12-20  5:36 ` [PATCH v2 00/16] btrfs-progs: global verbose and " Anand Jain
2020-01-14  6:14 ` Anand Jain
2020-01-14 11:40   ` David Sterba
2020-03-28  5:45     ` Anand Jain
2020-05-20 10:01       ` Anand Jain
2020-06-05  9:24         ` David Sterba
2020-06-05 10:12           ` Anand Jain
2020-06-10 10:17             ` David Sterba
2020-06-11 18:13           ` Anand Jain
2020-06-11 16:36 ` [PATCH v3 02/16] btrfs-progs: add global verbose and quiet options and helper functions Anand Jain
2020-06-11 16:39 ` [PATCH v3 11/16] btrfs-progs: restore: use global verbose option Anand Jain
2020-06-11 16:41 ` [PATCH v2 16/16] btrfs-progs: device scan: add quiet option Anand Jain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200612105606.18210-1-anand.jain@oracle.com \
    --to=anand.jain@oracle.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.