From: Alyssa Ross <hi@alyssa.is>
To: devel@spectrum-os.org
Subject: [PATCH ucspi-vsock] Factor out set_verbosity()
Date: Thu, 18 Mar 2021 00:35:30 +0000 [thread overview]
Message-ID: <20210318003530.13208-1-hi@alyssa.is> (raw)
This will save some cognitive load when reading main functions.
---
log.c | 20 +++++++++++++++++++-
log.h | 5 +++++
vsockclient.c | 6 +-----
vsockserver.c | 6 +-----
4 files changed, 26 insertions(+), 11 deletions(-)
diff --git a/log.c b/log.c
index 385d39f..cdfacd6 100644
--- a/log.c
+++ b/log.c
@@ -1,15 +1,33 @@
// SPDX-License-Identifier: GPL-2.0-or-later
-// SPDX-FileCopyrightText: 2020 Alyssa Ross <hi@alyssa.is>
+// SPDX-FileCopyrightText: 2020-2021 Alyssa Ross <hi@alyssa.is>
#define _GNU_SOURCE
#include "log.h"
#include <err.h>
+#include <stdbool.h>
#include <stdlib.h>
enum verbosity verbosity = errors;
+bool set_verbosity(int opt)
+{
+ switch (opt) {
+ case 'q':
+ verbosity = nothing;
+ return true;
+ case 'Q':
+ verbosity = errors;
+ return true;
+ case 'v':
+ verbosity = all;
+ return true;
+ }
+
+ return false;
+}
+
void veloge(const char *fmt, va_list args)
{
if (verbosity)
diff --git a/log.h b/log.h
index ea9e9cf..7b40e48 100644
--- a/log.h
+++ b/log.h
@@ -11,6 +11,11 @@ enum verbosity {
extern enum verbosity verbosity;
+// If opt is a character that matches a standard UCSPI command line
+// verbosity option, sets the verbosity appropriately and returns
+// true. Otherwise, returns false.
+_Bool set_verbosity(int opt);
+
// Log an error message, followed by strerrno(errno), then exit with
// status eval.
_Noreturn void diee(int eval, const char *fmt, ...);
diff --git a/vsockclient.c b/vsockclient.c
index 91e1320..d25ab13 100644
--- a/vsockclient.c
+++ b/vsockclient.c
@@ -35,13 +35,9 @@ int main(int argc, char *argv[])
while ((opt = getopt(argc, argv, "+qQv")) != -1) {
switch (opt) {
case 'q':
- verbosity = nothing;
- break;
case 'Q':
- verbosity = errors;
- break;
case 'v':
- verbosity = all;
+ set_verbosity(opt);
break;
default:
ex_usage();
diff --git a/vsockserver.c b/vsockserver.c
index 03d9838..4c251b0 100644
--- a/vsockserver.c
+++ b/vsockserver.c
@@ -43,13 +43,9 @@ int main(int argc, char *argv[])
notify = true;
break;
case 'q':
- verbosity = nothing;
- break;
case 'Q':
- verbosity = errors;
- break;
case 'v':
- verbosity = all;
+ set_verbosity(opt);
break;
default:
ex_usage();
--
2.30.0
next reply other threads:[~2021-03-18 0:35 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-18 0:35 Alyssa Ross [this message]
2021-03-18 6:55 ` [PATCH ucspi-vsock] Factor out set_verbosity() Cole Helbling
2021-03-18 13:12 ` Alyssa Ross
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=20210318003530.13208-1-hi@alyssa.is \
--to=hi@alyssa.is \
--cc=devel@spectrum-os.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.
Code repositories for project(s) associated with this public inbox
https://spectrum-os.org/git/crosvm
https://spectrum-os.org/git/doc
https://spectrum-os.org/git/mktuntap
https://spectrum-os.org/git/nixpkgs
https://spectrum-os.org/git/spectrum
https://spectrum-os.org/git/ucspi-vsock
https://spectrum-os.org/git/www
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).