From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v1.1 04/18] btrfs-progs: add global verbose and quiet options and helper functions
Date: Mon, 4 Nov 2019 14:33:02 +0800 [thread overview]
Message-ID: <1572849196-21775-5-git-send-email-anand.jain@oracle.com> (raw)
In-Reply-To: <1572849196-21775-1-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>
---
v1.1: Fix typo in HELPINFO_INSERT_QUIET
Drop stale #include <stdbool.h> we don't need it because
%bconf.verbose is now declared as int.
btrfs.c | 20 ++++++++++++++++++--
common/help.h | 11 +++++++++++
common/messages.c | 17 +++++++++++++++++
common/messages.h | 3 +++
common/utils.c | 1 +
common/utils.h | 3 +++
6 files changed, 53 insertions(+), 2 deletions(-)
diff --git a/btrfs.c b/btrfs.c
index 6c8aabe24dc8..a97bc1858390 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] [--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.verbose < 0 ? bconf.verbose = 1 : bconf.verbose++;
+ break;
+ case 'q':
+ bconf.verbose = 0;
+ 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.verbose < 0 ? bconf.verbose = 1 : bconf.verbose++;
+
+ for (i = 0; i < shift; i++)
+ if (strcmp(argv[i], "--quiet") == 0)
+ bconf.verbose = 0;
}
static const struct cmd_group btrfs_cmd_group = {
diff --git a/common/help.h b/common/help.h
index 01dfc68a7c8d..2618f31de712 100644
--- a/common/help.h
+++ b/common/help.h
@@ -53,6 +53,17 @@
"-t|--tbytes show sizes in TiB, or TB with --si"
/*
+ * Global verbose option for the sub-commands
+ */
+#define HELPINFO_GLOBAL_OPTIONS_HEADER \
+ "", \
+ "Global options:"
+#define HELPINFO_INSERT_VERBOSE \
+ "-v|--verbose show verbose output"
+#define HELPINFO_INSERT_QUIET \
+ "-q|--quiet run the command quietly"
+
+/*
* 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..410630ffdd6d 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,19 @@ int __btrfs_error_on(int condition, const char *fmt, ...)
return 1;
}
+
+__attribute__ ((format (printf, 2, 3)))
+void pr_verbose(int level, const char *fmt, ...)
+{
+ va_list args;
+
+ if (level == 0 || bconf.verbose == 0)
+ return;
+
+ if (level > bconf.verbose)
+ return;
+
+ va_start(args, fmt);
+ vfprintf(stdout, fmt, args);
+ va_end(args);
+}
diff --git a/common/messages.h b/common/messages.h
index 596047948fef..f802a9413265 100644
--- a/common/messages.h
+++ b/common/messages.h
@@ -96,3 +96,6 @@ __attribute__ ((format (printf, 2, 3)))
int __btrfs_error_on(int condition, const char *fmt, ...);
#endif
+
+__attribute__ ((format (printf, 2, 3)))
+void pr_verbose(int level, const char *fmt, ...);
diff --git a/common/utils.c b/common/utils.c
index 2cf15c333f6b..c2c6d0af0efc 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -1649,6 +1649,7 @@ u8 rand_u8(void)
void btrfs_config_init(void)
{
bconf.output_format = CMD_FORMAT_TEXT;
+ bconf.verbose = -1;
}
/* Returns total size of main memory in bytes, -1UL if error. */
diff --git a/common/utils.h b/common/utils.h
index 0ef1d6e89c2b..8774194f4e9d 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -122,6 +122,9 @@ void print_all_devices(struct list_head *devices);
*/
struct btrfs_config {
unsigned int output_format;
+
+ /* -1:unset 0:quiet >0:verbose */
+ int verbose;
};
extern struct btrfs_config bconf;
--
1.8.3.1
next prev parent reply other threads:[~2019-11-04 6:34 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-04 6:32 [PATCH v1.1 00/18] btrfs-progs: global verbose and quiet option Anand Jain
2019-11-04 6:32 ` [PATCH v1 01/18] btrfs-progs: receive: fix option quiet Anand Jain
2019-11-04 6:33 ` [PATCH v1 02/18] btrfs-progs: balance status: fix usage show long verbose Anand Jain
2019-11-04 6:33 ` [PATCH v1 03/18] btrfs-progs: balance start: fix usage add " Anand Jain
2019-11-04 6:33 ` Anand Jain [this message]
2019-11-14 16:08 ` [PATCH v1.1 04/18] btrfs-progs: add global verbose and quiet options and helper functions David Sterba
2019-11-19 2:44 ` Anand Jain
2019-11-19 3:36 ` Anand Jain
2019-11-19 16:51 ` David Sterba
2019-11-25 10:36 ` Anand Jain
2019-11-15 15:58 ` David Sterba
2019-11-19 5:07 ` Anand Jain
2019-11-19 17:02 ` David Sterba
2019-11-04 6:33 ` [PATCH v1.1 05/18] btrfs-progs: send: use global verbose and quiet options Anand Jain
2019-11-04 6:33 ` [PATCH v1.1 06/18] btrfs-progs: receive: " Anand Jain
2019-11-04 6:33 ` [PATCH v1.1 07/18] btrfs-progs: subvolume delete: use global verbose option Anand Jain
2019-11-04 6:33 ` [PATCH v1 08/18] btrfs-progs: filesystem defragment: " Anand Jain
2019-11-14 16:16 ` David Sterba
2019-11-25 10:35 ` Anand Jain
2019-11-04 6:33 ` [PATCH v1 09/18] btrfs-progs: balance start: " Anand Jain
2019-11-04 6:33 ` [PATCH v1 10/18] btrfs-progs: balance status: " Anand Jain
2019-11-04 6:33 ` [PATCH v1 11/18] btrfs-progs: rescue chunk-recover: " Anand Jain
2019-11-04 6:33 ` [PATCH v1 12/18] btrfs-progs: rescue super-recover: " Anand Jain
2019-11-04 6:33 ` [PATCH v1.1 13/18] btrfs-progs: restore: " Anand Jain
2019-11-04 6:33 ` [PATCH v1 14/18] btrfs-progs: inspect-internal inode-resolve: use global verbose Anand Jain
2019-11-04 6:33 ` [PATCH v1 15/18] btrfs-progs: inspect-internal logical-resolve: use global verbose option Anand Jain
2019-11-04 6:33 ` [PATCH v1.1 16/18] btrfs-progs: refactor btrfs_scan_devices() to accept verbose argument Anand Jain
2019-11-04 6:33 ` [PATCH v1 17/18] btrfs-progs: device scan: add verbose option Anand Jain
2019-11-04 6:33 ` [PATCH v1.1 18/18] btrfs-progs: device scan: add quiet option Anand Jain
2019-11-15 16:11 ` [PATCH v1.1 00/18] btrfs-progs: global verbose and " David Sterba
2019-11-19 3:50 ` 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=1572849196-21775-5-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;
as well as URLs for NNTP newsgroup(s).