@@ -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 = {
@@ -52,6 +52,17 @@
"-g|--gbytes show sizes in GiB, or GB with --si", \
"-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_QUITE \
+ "-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
@@ -16,7 +16,9 @@
#include <stdio.h>
#include <stdarg.h>
+#include <stdbool.h>
#include "common/messages.h"
+#include "common/utils.h"
__attribute__ ((format (printf, 1, 2)))
void __btrfs_warning(const char *fmt, ...)
@@ -75,3 +77,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);
+}
@@ -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(int level, const char *fmt, ...);
@@ -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. */
@@ -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;
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> --- btrfs.c | 20 ++++++++++++++++++-- common/help.h | 11 +++++++++++ common/messages.c | 18 ++++++++++++++++++ common/messages.h | 5 +++++ common/utils.c | 1 + common/utils.h | 3 +++ 6 files changed, 56 insertions(+), 2 deletions(-)