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.
This commit is contained in:
Alex Young
2012-07-12 14:45:55 +01:00
parent 10b46beeea
commit c6a084ce82
3 changed files with 64 additions and 15 deletions

View File

@@ -14,10 +14,11 @@ static struct option serve_options[] = {
GETOPT_FILE, GETOPT_FILE,
GETOPT_SOCK, GETOPT_SOCK,
GETOPT_DENY, GETOPT_DENY,
GETOPT_QUIET,
GETOPT_VERBOSE, GETOPT_VERBOSE,
{0} {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[] = static char serve_help_text[] =
"Usage: flexnbd " CMD_SERVE " <options> [<acl address>*]\n\n" "Usage: flexnbd " CMD_SERVE " <options> [<acl address>*]\n\n"
"Serve FILE from ADDR:PORT, with an optional control socket at SOCK.\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 <FILE>\tThe file to serve.\n" "\t--" OPT_FILE ",-f <FILE>\tThe file to serve.\n"
"\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n" "\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n"
SOCK_LINE SOCK_LINE
VERBOSE_LINE; VERBOSE_LINE
QUIET_LINE;
static struct option listen_options[] = { static struct option listen_options[] = {
@@ -39,10 +41,11 @@ static struct option listen_options[] = {
GETOPT_FILE, GETOPT_FILE,
GETOPT_SOCK, GETOPT_SOCK,
GETOPT_DENY, GETOPT_DENY,
GETOPT_QUIET,
GETOPT_VERBOSE, GETOPT_VERBOSE,
{0} {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[] = static char listen_help_text[] =
"Usage: flexnbd " CMD_LISTEN " <options> [<acl_address>*]\n\n" "Usage: flexnbd " CMD_LISTEN " <options> [<acl_address>*]\n\n"
"Listen for an incoming migration on ADDR:PORT, " "Listen for an incoming migration on ADDR:PORT, "
@@ -56,7 +59,8 @@ static char listen_help_text[] =
"\t--" OPT_FILE ",-f <FILE>\tThe file to serve.\n" "\t--" OPT_FILE ",-f <FILE>\tThe file to serve.\n"
"\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n" "\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n"
SOCK_LINE SOCK_LINE
VERBOSE_LINE; VERBOSE_LINE
QUIET_LINE;
static struct option read_options[] = { static struct option read_options[] = {
@@ -66,10 +70,11 @@ static struct option read_options[] = {
GETOPT_FROM, GETOPT_FROM,
GETOPT_SIZE, GETOPT_SIZE,
GETOPT_BIND, GETOPT_BIND,
GETOPT_QUIET,
GETOPT_VERBOSE, GETOPT_VERBOSE,
{0} {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[] = static char read_help_text[] =
"Usage: flexnbd " CMD_READ " <options>\n\n" "Usage: flexnbd " CMD_READ " <options>\n\n"
"Read SIZE bytes from a server at ADDR:PORT to stdout, starting at OFFSET.\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 <OFFSET>\tByte offset to read from.\n" "\t--" OPT_FROM ",-F <OFFSET>\tByte offset to read from.\n"
"\t--" OPT_SIZE ",-S <SIZE>\tBytes to read.\n" "\t--" OPT_SIZE ",-S <SIZE>\tBytes to read.\n"
BIND_LINE BIND_LINE
VERBOSE_LINE; VERBOSE_LINE
QUIET_LINE;
static struct option *write_options = read_options; static struct option *write_options = read_options;
@@ -93,21 +99,24 @@ static char write_help_text[] =
"\t--" OPT_FROM ",-F <OFFSET>\tByte offset to write from.\n" "\t--" OPT_FROM ",-F <OFFSET>\tByte offset to write from.\n"
"\t--" OPT_SIZE ",-S <SIZE>\tBytes to write.\n" "\t--" OPT_SIZE ",-S <SIZE>\tBytes to write.\n"
BIND_LINE BIND_LINE
VERBOSE_LINE; VERBOSE_LINE
QUIET_LINE;
static struct option acl_options[] = { static struct option acl_options[] = {
GETOPT_HELP, GETOPT_HELP,
GETOPT_SOCK, GETOPT_SOCK,
GETOPT_QUIET,
GETOPT_VERBOSE, GETOPT_VERBOSE,
{0} {0}
}; };
static char acl_short_options[] = "hs:" SOPT_VERBOSE; static char acl_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
static char acl_help_text[] = static char acl_help_text[] =
"Usage: flexnbd " CMD_ACL " <options> [<acl address>+]\n\n" "Usage: flexnbd " CMD_ACL " <options> [<acl address>+]\n\n"
"Set the access control list for a server with control socket SOCK.\n\n" "Set the access control list for a server with control socket SOCK.\n\n"
HELP_LINE HELP_LINE
SOCK_LINE SOCK_LINE
VERBOSE_LINE; VERBOSE_LINE
QUIET_LINE;
static struct option mirror_options[] = { static struct option mirror_options[] = {
GETOPT_HELP, GETOPT_HELP,
@@ -115,10 +124,11 @@ static struct option mirror_options[] = {
GETOPT_ADDR, GETOPT_ADDR,
GETOPT_PORT, GETOPT_PORT,
GETOPT_BIND, GETOPT_BIND,
GETOPT_QUIET,
GETOPT_VERBOSE, GETOPT_VERBOSE,
{0} {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[] = static char mirror_help_text[] =
"Usage: flexnbd " CMD_MIRROR " <options>\n\n" "Usage: flexnbd " CMD_MIRROR " <options>\n\n"
"Start mirroring from the server with control socket SOCK to one at ADDR:PORT.\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 <PORT>\tThe port to mirror to.\n" "\t--" OPT_PORT ",-p <PORT>\tThe port to mirror to.\n"
SOCK_LINE SOCK_LINE
BIND_LINE BIND_LINE
VERBOSE_LINE; VERBOSE_LINE
QUIET_LINE;
static struct option status_options[] = { static struct option status_options[] = {
GETOPT_HELP, GETOPT_HELP,
GETOPT_SOCK, GETOPT_SOCK,
GETOPT_QUIET,
GETOPT_VERBOSE, GETOPT_VERBOSE,
{0} {0}
}; };
static char status_short_options[] = "hs:" SOPT_VERBOSE; static char status_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
static char status_help_text[] = static char status_help_text[] =
"Usage: flexnbd " CMD_STATUS " <options>\n\n" "Usage: flexnbd " CMD_STATUS " <options>\n\n"
"Get the status for a server with control socket SOCK.\n\n" "Get the status for a server with control socket SOCK.\n\n"
HELP_LINE HELP_LINE
SOCK_LINE SOCK_LINE
VERBOSE_LINE; VERBOSE_LINE
QUIET_LINE;
char help_help_text_arr[] = char help_help_text_arr[] =
"Usage: flexnbd <cmd> [cmd options]\n\n" "Usage: flexnbd <cmd> [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': case 'd':
*default_deny = 1; *default_deny = 1;
break; break;
case 'q':
log_level = 4;
break;
case 'v': case 'v':
log_level = 0; log_level = 0;
break; break;
@@ -235,6 +251,9 @@ void read_listen_param( int c,
case 'd': case 'd':
*default_deny = 1; *default_deny = 1;
break; break;
case 'q':
log_level = 4;
break;
case 'v': case 'v':
log_level = 0; log_level = 0;
break; break;
@@ -266,6 +285,9 @@ void read_readwrite_param( int c, char **ip_addr, char **ip_port, char **bind_ad
case 'b': case 'b':
*bind_addr = optarg; *bind_addr = optarg;
break; break;
case 'q':
log_level = 4;
break;
case 'v': case 'v':
log_level = 0; log_level = 0;
break; break;
@@ -285,6 +307,9 @@ void read_sock_param( int c, char **sock, char *help_text )
case 's': case 's':
*sock = optarg; *sock = optarg;
break; break;
case 'q':
log_level = 4;
break;
case 'v': case 'v':
log_level = 0; log_level = 0;
break; break;
@@ -317,6 +342,10 @@ void read_mirror_param( int c, char **sock, char **ip_addr, char **ip_port, char
break; break;
case 'b': case 'b':
*bind_addr = optarg; *bind_addr = optarg;
break;
case 'q':
log_level = 4;
break;
case 'v': case 'v':
log_level = 0; log_level = 0;
break; break;

View File

@@ -63,6 +63,13 @@ void mode(char* mode, int argc, char **argv);
# define SOPT_VERBOSE "" # define SOPT_VERBOSE ""
#endif #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 \ #define HELP_LINE \
"\t--" OPT_HELP ",-h \tThis text.\n" "\t--" OPT_HELP ",-h \tThis text.\n"
#define SOCK_LINE \ #define SOCK_LINE \

View File

@@ -187,9 +187,22 @@ class FlexNBD
end 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) def initialize(bin, ip, port, rebind_ip = ip, rebind_port = port)
@bin = bin @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) raise "#{bin} not executable" unless File.executable?(bin)
@executor = pick_executor.new @executor = pick_executor.new
@ctrl = "/tmp/.flexnbd.ctrl.#{Time.now.to_i}.#{rand}" @ctrl = "/tmp/.flexnbd.ctrl.#{Time.now.to_i}.#{rand}"
@@ -202,7 +215,7 @@ class FlexNBD
def debug? def debug?
!@debug.empty? || ENV['DEBUG'] !!@do_debug
end end
def debug( msg ) def debug( msg )