From c6a084ce82763c97445daeee68de4c299641b28e Mon Sep 17 00:00:00 2001 From: Alex Young Date: Thu, 12 Jul 2012 14:45:55 +0100 Subject: [PATCH] Add a --quiet command-line option --quiet will suppress all log lines except FATAL. Conceptually it's exclusive with --verbose, but this isn't checked - last one wins. --- src/mode.c | 55 ++++++++++++++++++++++++++++--------- src/mode.h | 7 +++++ tests/acceptance/flexnbd.rb | 17 ++++++++++-- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/src/mode.c b/src/mode.c index f32783e..210854d 100644 --- a/src/mode.c +++ b/src/mode.c @@ -14,10 +14,11 @@ static struct option serve_options[] = { GETOPT_FILE, GETOPT_SOCK, GETOPT_DENY, + GETOPT_QUIET, GETOPT_VERBOSE, {0} }; -static char serve_short_options[] = "hl:p:f:s:d" SOPT_VERBOSE; +static char serve_short_options[] = "hl:p:f:s:d" SOPT_QUIET SOPT_VERBOSE; static char serve_help_text[] = "Usage: flexnbd " CMD_SERVE " [*]\n\n" "Serve FILE from ADDR:PORT, with an optional control socket at SOCK.\n\n" @@ -27,7 +28,8 @@ static char serve_help_text[] = "\t--" OPT_FILE ",-f \tThe file to serve.\n" "\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n" SOCK_LINE - VERBOSE_LINE; + VERBOSE_LINE + QUIET_LINE; static struct option listen_options[] = { @@ -39,10 +41,11 @@ static struct option listen_options[] = { GETOPT_FILE, GETOPT_SOCK, GETOPT_DENY, + GETOPT_QUIET, GETOPT_VERBOSE, {0} }; -static char listen_short_options[] = "hl:L:p:P:f:s:d" SOPT_VERBOSE; +static char listen_short_options[] = "hl:L:p:P:f:s:d" SOPT_QUIET SOPT_VERBOSE; static char listen_help_text[] = "Usage: flexnbd " CMD_LISTEN " [*]\n\n" "Listen for an incoming migration on ADDR:PORT, " @@ -56,7 +59,8 @@ static char listen_help_text[] = "\t--" OPT_FILE ",-f \tThe file to serve.\n" "\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n" SOCK_LINE - VERBOSE_LINE; + VERBOSE_LINE + QUIET_LINE; static struct option read_options[] = { @@ -66,10 +70,11 @@ static struct option read_options[] = { GETOPT_FROM, GETOPT_SIZE, GETOPT_BIND, + GETOPT_QUIET, GETOPT_VERBOSE, {0} }; -static char read_short_options[] = "hl:p:F:S:b:" SOPT_VERBOSE; +static char read_short_options[] = "hl:p:F:S:b:" SOPT_QUIET SOPT_VERBOSE; static char read_help_text[] = "Usage: flexnbd " CMD_READ " \n\n" "Read SIZE bytes from a server at ADDR:PORT to stdout, starting at OFFSET.\n\n" @@ -79,7 +84,8 @@ static char read_help_text[] = "\t--" OPT_FROM ",-F \tByte offset to read from.\n" "\t--" OPT_SIZE ",-S \tBytes to read.\n" BIND_LINE - VERBOSE_LINE; + VERBOSE_LINE + QUIET_LINE; static struct option *write_options = read_options; @@ -93,21 +99,24 @@ static char write_help_text[] = "\t--" OPT_FROM ",-F \tByte offset to write from.\n" "\t--" OPT_SIZE ",-S \tBytes to write.\n" BIND_LINE - VERBOSE_LINE; + VERBOSE_LINE + QUIET_LINE; static struct option acl_options[] = { GETOPT_HELP, GETOPT_SOCK, + GETOPT_QUIET, GETOPT_VERBOSE, {0} }; -static char acl_short_options[] = "hs:" SOPT_VERBOSE; +static char acl_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE; static char acl_help_text[] = "Usage: flexnbd " CMD_ACL " [+]\n\n" "Set the access control list for a server with control socket SOCK.\n\n" HELP_LINE SOCK_LINE - VERBOSE_LINE; + VERBOSE_LINE + QUIET_LINE; static struct option mirror_options[] = { GETOPT_HELP, @@ -115,10 +124,11 @@ static struct option mirror_options[] = { GETOPT_ADDR, GETOPT_PORT, GETOPT_BIND, + GETOPT_QUIET, GETOPT_VERBOSE, {0} }; -static char mirror_short_options[] = "hs:l:p:b:" SOPT_VERBOSE; +static char mirror_short_options[] = "hs:l:p:b:" SOPT_QUIET SOPT_VERBOSE; static char mirror_help_text[] = "Usage: flexnbd " CMD_MIRROR " \n\n" "Start mirroring from the server with control socket SOCK to one at ADDR:PORT.\n\n" @@ -127,22 +137,25 @@ static char mirror_help_text[] = "\t--" OPT_PORT ",-p \tThe port to mirror to.\n" SOCK_LINE BIND_LINE - VERBOSE_LINE; + VERBOSE_LINE + QUIET_LINE; static struct option status_options[] = { GETOPT_HELP, GETOPT_SOCK, + GETOPT_QUIET, GETOPT_VERBOSE, {0} }; -static char status_short_options[] = "hs:" SOPT_VERBOSE; +static char status_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE; static char status_help_text[] = "Usage: flexnbd " CMD_STATUS " \n\n" "Get the status for a server with control socket SOCK.\n\n" HELP_LINE SOCK_LINE - VERBOSE_LINE; + VERBOSE_LINE + QUIET_LINE; char help_help_text_arr[] = "Usage: flexnbd [cmd options]\n\n" @@ -190,6 +203,9 @@ void read_serve_param( int c, char **ip_addr, char **ip_port, char **file, char case 'd': *default_deny = 1; break; + case 'q': + log_level = 4; + break; case 'v': log_level = 0; break; @@ -235,6 +251,9 @@ void read_listen_param( int c, case 'd': *default_deny = 1; break; + case 'q': + log_level = 4; + break; case 'v': log_level = 0; break; @@ -266,6 +285,9 @@ void read_readwrite_param( int c, char **ip_addr, char **ip_port, char **bind_ad case 'b': *bind_addr = optarg; break; + case 'q': + log_level = 4; + break; case 'v': log_level = 0; break; @@ -285,6 +307,9 @@ void read_sock_param( int c, char **sock, char *help_text ) case 's': *sock = optarg; break; + case 'q': + log_level = 4; + break; case 'v': log_level = 0; break; @@ -317,6 +342,10 @@ void read_mirror_param( int c, char **sock, char **ip_addr, char **ip_port, char break; case 'b': *bind_addr = optarg; + break; + case 'q': + log_level = 4; + break; case 'v': log_level = 0; break; diff --git a/src/mode.h b/src/mode.h index 46d3c92..fc53f83 100644 --- a/src/mode.h +++ b/src/mode.h @@ -63,6 +63,13 @@ void mode(char* mode, int argc, char **argv); # define SOPT_VERBOSE "" #endif +#define OPT_QUIET "quiet" +#define SOPT_QUIET "q" +#define GETOPT_QUIET GETOPT_FLAG( OPT_QUIET, 'q' ) +#define QUIET_LINE \ + "\t--" OPT_QUIET ",-" SOPT_QUIET "\t\tOutput only fatal informatio.\n" + + #define HELP_LINE \ "\t--" OPT_HELP ",-h \tThis text.\n" #define SOCK_LINE \ diff --git a/tests/acceptance/flexnbd.rb b/tests/acceptance/flexnbd.rb index deb02c3..2411599 100644 --- a/tests/acceptance/flexnbd.rb +++ b/tests/acceptance/flexnbd.rb @@ -187,9 +187,22 @@ class FlexNBD end + def build_debug_opt + if @do_debug + if `#{@bin} serve --help` =~ /--verbose/ + "--verbose" + else + "" + end + else + "--quiet" + end + end + def initialize(bin, ip, port, rebind_ip = ip, rebind_port = port) @bin = bin - @debug = (ENV['DEBUG'] && `#{@bin} serve --help` =~ /--verbose/) ? "--verbose" : "" + @do_debug = ENV['DEBUG'] + @debug = build_debug_opt raise "#{bin} not executable" unless File.executable?(bin) @executor = pick_executor.new @ctrl = "/tmp/.flexnbd.ctrl.#{Time.now.to_i}.#{rand}" @@ -202,7 +215,7 @@ class FlexNBD def debug? - !@debug.empty? || ENV['DEBUG'] + !!@do_debug end def debug( msg )