public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [RFC PATCH 01/14] btrfs-progs: add global verbose helper functions
Date: Mon, 21 Oct 2019 18:01:09 +0800	[thread overview]
Message-ID: <1571652082-25982-2-git-send-email-anand.jain@oracle.com> (raw)
In-Reply-To: <1571652082-25982-1-git-send-email-anand.jain@oracle.com>

The idea is to use the global --verbose command option to show
verbose output from the sub-commands. This patch adds a global
bool variable, %global_verbose, to transpire the verbose requisites
to the sub-command level. And provides pr_verbose() helper
function to log the verbose messages.

Suggested-by: David Sterba <dsterba@suse.com>
Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 btrfs.c           | 12 ++++++++++--
 common/help.h     |  8 ++++++++
 common/messages.c | 19 +++++++++++++++++++
 common/messages.h |  5 +++++
 4 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/btrfs.c b/btrfs.c
index 72dad6fb3983..ac10d8110495 100644
--- a/btrfs.c
+++ b/btrfs.c
@@ -26,8 +26,10 @@
 #include "common/help.h"
 #include "common/box.h"
 
+extern bool global_verbose;
+
 static const char * const btrfs_cmd_group_usage[] = {
-	"btrfs [--help] [--version] [--format <format>] <group> [<group>...] <command> [<args>]",
+	"btrfs [--help] [--version] [--format <format>] [--verbose] <group> [<group>...] <command> [<args>]",
 	NULL
 };
 
@@ -242,12 +244,13 @@ static void handle_output_format(const char *format)
  */
 static int handle_global_options(int argc, char **argv)
 {
-	enum { OPT_HELP = 256, OPT_VERSION, OPT_FULL, OPT_FORMAT };
+	enum { OPT_HELP = 256, OPT_VERSION, OPT_FULL, OPT_FORMAT, OPT_VERBOSE };
 	static const struct option long_options[] = {
 		{ "help", no_argument, NULL, OPT_HELP },
 		{ "version", no_argument, NULL, OPT_VERSION },
 		{ "format", required_argument, NULL, OPT_FORMAT },
 		{ "full", no_argument, NULL, OPT_FULL },
+		{ "verbose", no_argument, NULL, OPT_VERBOSE },
 		{ NULL, 0, NULL, 0}
 	};
 	int shift;
@@ -270,6 +273,7 @@ static int handle_global_options(int argc, char **argv)
 		case OPT_FORMAT:
 			handle_output_format(optarg);
 			break;
+		case OPT_VERBOSE: break;
 		default:
 			fprintf(stderr, "Unknown global option: %s\n",
 					argv[optind - 1]);
@@ -310,6 +314,10 @@ 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)
+			global_verbose = true;
 }
 
 static const struct cmd_group btrfs_cmd_group = {
diff --git a/common/help.h b/common/help.h
index 01dfc68a7c8d..7bb3074b0be6 100644
--- a/common/help.h
+++ b/common/help.h
@@ -53,6 +53,14 @@
 	"-t|--tbytes        show sizes in TiB, or TB with --si"
 
 /*
+ * Global verbose option for the sub-commands
+ */
+#define HELPINFO_INSERT_VERBOSE							\
+	"-v|--verbose       show verbose output"
+#define HELPINFO_INSERT_VERBOSE_SHORT						\
+	"-v                 show verbose output"
+
+/*
  * Special marker in the help strings that will preemptively insert the global
  * options and then continue with the following text that possibly follows
  * after the regular options
diff --git a/common/messages.c b/common/messages.c
index 0e5694ecd467..e14c112ebbbf 100644
--- a/common/messages.c
+++ b/common/messages.c
@@ -16,6 +16,7 @@
 
 #include <stdio.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include "common/messages.h"
 
 __attribute__ ((format (printf, 1, 2)))
@@ -75,3 +76,21 @@ int __btrfs_error_on(int condition, const char *fmt, ...)
 
 	return 1;
 }
+
+bool global_verbose = false;
+
+__attribute__ ((format (printf, 2, 3)))
+void pr_verbose(bool condition, const char *fmt, ...)
+{
+	va_list args;
+
+	if (condition == false)
+		return;
+
+	if (global_verbose == false)
+		return;
+
+	va_start(args, fmt);
+	vfprintf(stdout, fmt, args);
+	va_end(args);
+}
diff --git a/common/messages.h b/common/messages.h
index 596047948fef..a14e2d62f3a0 100644
--- a/common/messages.h
+++ b/common/messages.h
@@ -14,6 +14,8 @@
  * Boston, MA 021110-1307, USA.
  */
 
+#include <stdbool.h>
+
 #ifndef __BTRFS_MESSAGES_H__
 #define __BTRFS_MESSAGES_H__
 
@@ -96,3 +98,6 @@ __attribute__ ((format (printf, 2, 3)))
 int __btrfs_error_on(int condition, const char *fmt, ...);
 
 #endif
+
+__attribute__ ((format (printf, 2, 3)))
+void pr_verbose(bool condition, const char *fmt, ...);
-- 
1.8.3.1


  reply	other threads:[~2019-10-21 10:01 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-21 10:01 [RFC PATCH 00/14] btrfs-progs: global-verbose option Anand Jain
2019-10-21 10:01 ` Anand Jain [this message]
2019-10-21 10:01 ` [RFC PATCH 02/14] btrfs-progs: migrate subvolume delete to global verbose Anand Jain
2019-10-21 10:01 ` [RFC PATCH 03/14] btrfs-progs: migrate filesystem defragment " Anand Jain
2019-10-21 10:01 ` [RFC PATCH 04/14] btrfs-progs: migrate btrfs balance start " Anand Jain
2019-10-21 10:01 ` [RFC PATCH 05/14] btrfs-progs: migrate balance status " Anand Jain
2019-10-21 10:01 ` [RFC PATCH 06/14] btrfs-progs: fix help, show long option in balance start and status Anand Jain
2019-10-21 10:01 ` [RFC PATCH 07/14] btrfs-progs: migrate rescue chunk-recover to global verbose Anand Jain
2019-10-21 10:01 ` [RFC PATCH 08/14] btrfs-progs: migrate rescue super-recover " Anand Jain
2019-10-21 10:01 ` [RFC PATCH 09/14] btrfs-progs: restore: delete unreachable code Anand Jain
2019-10-21 10:01 ` [RFC PATCH 10/14] btrfs-progs: migrate restore to global verbose Anand Jain
2019-10-21 10:01 ` [RFC PATCH 11/14] btrfs-progs: migrate inspect-internal inode-resolve " Anand Jain
2019-10-21 10:01 ` [RFC PATCH 12/14] btrfs-progs: migrate inspect-internal logical-resolve " Anand Jain
2019-10-21 10:01 ` [RFC PATCH 13/14] btrfs-progs: refactor btrfs_scan_devices() to accept verbose argument Anand Jain
2019-10-21 10:01 ` [RFC PATCH 14/14] btrfs-progs: enable verbose for btrfs device scan Anand Jain
2019-10-21 16:12 ` [RFC PATCH 00/14] btrfs-progs: global-verbose option David Sterba
2019-10-22  1:54   ` Anand Jain
2019-10-22 11:45     ` David Sterba

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=1571652082-25982-2-git-send-email-anand.jain@oracle.com \
    --to=anand.jain@oracle.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox