From 1c4a2b4f9af3df4308979230fa5a90f35dcc49cd Mon Sep 17 00:00:00 2001 From: Hannes Reinecke Date: Mon, 14 Jun 2021 16:26:45 +0200 Subject: [PATCH] fabrics: remove log.c in favour of libnvme routines The logging functions have been ported to libnvme, so remove our copy and replace the 'quiet' parameter with the logging equivalent. Signed-off-by: Hannes Reinecke --- Makefile | 2 +- fabrics.c | 78 ++++++++++++++++++++++++++++++++++++++--- util/argconfig.h | 3 ++ util/log.c | 90 ------------------------------------------------ util/log.h | 34 ------------------ 5 files changed, 78 insertions(+), 129 deletions(-) delete mode 100644 util/log.c delete mode 100644 util/log.h diff --git a/Makefile b/Makefile index bc9a94e5..5fbdfd0b 100644 --- a/Makefile +++ b/Makefile @@ -70,7 +70,7 @@ OBJS := nvme-print.o nvme-rpmb.o \ fabrics.o nvme-models.o plugin.o UTIL_OBJS := util/argconfig.o util/suffix.o util/parser.o \ - util/cleanup.o util/log.o + util/cleanup.o ifneq ($(LIBJSONC), 0) override UTIL_OBJS += util/json.o endif diff --git a/fabrics.c b/fabrics.c index 8eb91224..5925e75d 100644 --- a/fabrics.c +++ b/fabrics.c @@ -228,7 +228,7 @@ static void save_discovery_log(char *raw, struct nvmf_discovery_log *log) } static int __discover(nvme_ctrl_t c, const struct nvme_fabrics_config *defcfg, - char *raw, bool connect, bool persistent, bool quiet, + char *raw, bool connect, bool persistent, enum nvme_print_flags flags) { struct nvmf_discovery_log *log = NULL; @@ -270,7 +270,7 @@ static int __discover(nvme_ctrl_t c, const struct nvme_fabrics_config *defcfg, if (child) { if (discover) __discover(child, defcfg, raw, - persistent, quiet, + persistent, true, flags); if (!persistent) { nvme_ctrl_disconnect(child); @@ -351,7 +351,7 @@ static int discover_from_conf_file(nvme_host_t h, const char *desc, errno = 0; ret = nvmf_add_ctrl(h, c, &cfg, false); if (!ret) { - __discover(c, defcfg, NULL, persistent, quiet, + __discover(c, defcfg, NULL, persistent, connect, 0); return 0; if (!persistent) @@ -378,6 +378,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect) enum nvme_print_flags flags; nvme_root_t r; nvme_host_t h; + unsigned int verbose = 0; int ret; char *format = "normal"; const char *tmp_device; @@ -396,6 +397,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect) OPT_FLAG("persistent", 'p', &persistent, "persistent discovery connection"), OPT_FLAG("quiet", 'S', &quiet, "suppress already connected errors"), OPT_STRING("config", 'C', "FILE", &config_file, nvmf_config_file), + OPT_INCR("verbose", 'v', &verbose, "Increase logging verbosity"), OPT_END() }; @@ -407,6 +409,23 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect) if (ret < 0) return ret; + switch (verbose) { + case 0: + nvme_log_level = LOG_WARNING; + break; + case 1: + nvme_log_level = LOG_NOTICE; + break; + case 2: + nvme_log_level = LOG_INFO; + break; + default: + nvme_log_level = LOG_DEBUG; + break; + } + if (quiet) + nvme_log_level = LOG_ERR; + if (!strcmp(config_file, "none")) config_file = NULL; r = nvme_scan(config_file); @@ -446,7 +465,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect) if (!ret) { ret = __discover(c, &cfg, raw, connect, - persistent, quiet, flags); + persistent, flags); if (!device && !persistent) nvme_ctrl_disconnect(c); nvme_free_ctrl(c); @@ -473,6 +492,7 @@ int nvmf_connect(const char *desc, int argc, char **argv) char *host_traddr = NULL, *host_iface = NULL; char *trsvcid = NULL, *hostnqn = NULL, *hostid = NULL; char *config_file = PATH_NVMF_CONFIG; + unsigned int verbose = 0; nvme_root_t r; nvme_host_t h; nvme_ctrl_t c; @@ -487,6 +507,7 @@ int nvmf_connect(const char *desc, int argc, char **argv) OPT_STRING("nqn", 'n', "NAME", &subsysnqn, nvmf_nqn), NVMF_OPTS(cfg), OPT_STRING("config", 'C', "FILE", &config_file, nvmf_config_file), + OPT_INCR("verbose", 'v', &verbose, "Increase logging verbosity"), OPT_END() }; @@ -494,6 +515,21 @@ int nvmf_connect(const char *desc, int argc, char **argv) if (ret) return ret; + switch (verbose) { + case 0: + nvme_log_level = LOG_WARNING; + break; + case 1: + nvme_log_level = LOG_NOTICE; + break; + case 2: + nvme_log_level = LOG_INFO; + break; + default: + nvme_log_level = LOG_DEBUG; + break; + } + if (!subsysnqn) { nvme_msg(LOG_ERR, "required argument [--nqn | -n] not specified\n"); @@ -560,6 +596,7 @@ int nvmf_disconnect(const char *desc, int argc, char **argv) struct config { char *nqn; char *device; + unsigned int verbose; }; struct config cfg = { 0 }; @@ -567,6 +604,7 @@ int nvmf_disconnect(const char *desc, int argc, char **argv) OPT_ARGS(opts) = { OPT_STRING("nqn", 'n', "NAME", &cfg.nqn, nvmf_nqn), OPT_STRING("device", 'd', "DEV", &cfg.device, device), + OPT_INCR("verbose", 'v', &cfg.verbose, "Increase logging verbosity"), OPT_END() }; @@ -574,6 +612,21 @@ int nvmf_disconnect(const char *desc, int argc, char **argv) if (ret) return ret; + switch (cfg.verbose) { + case 0: + nvme_log_level = LOG_WARNING; + break; + case 1: + nvme_log_level = LOG_NOTICE; + break; + case 2: + nvme_log_level = LOG_INFO; + break; + default: + nvme_log_level = LOG_DEBUG; + break; + } + if (!cfg.nqn && !cfg.device) { nvme_msg(LOG_ERR, "Neither device name [--device | -d] nor NQN [--nqn | -n] provided\n"); @@ -638,12 +691,14 @@ int nvmf_disconnect_all(const char *desc, int argc, char **argv) struct config { char *transport; + unsigned int verbose; }; struct config cfg = { 0 }; OPT_ARGS(opts) = { OPT_STRING("transport", 'r', "STR", (char *)&cfg.transport, nvmf_tport), + OPT_INCR("verbose", 'v', &cfg.verbose, "Increase logging verbosity"), OPT_END() }; @@ -651,6 +706,21 @@ int nvmf_disconnect_all(const char *desc, int argc, char **argv) if (ret) return ret; + switch (cfg.verbose) { + case 0: + nvme_log_level = LOG_WARNING; + break; + case 1: + nvme_log_level = LOG_NOTICE; + break; + case 2: + nvme_log_level = LOG_INFO; + break; + default: + nvme_log_level = LOG_DEBUG; + break; + } + r = nvme_scan(NULL); if (!r) { nvme_msg(LOG_ERR, "Failed to scan nvme subsystem: %s\n", diff --git a/util/argconfig.h b/util/argconfig.h index 3147277a..1a5c6936 100644 --- a/util/argconfig.h +++ b/util/argconfig.h @@ -91,6 +91,9 @@ enum argconfig_types { #define OPT_SHRT(l, s, v, d) \ {l, s, "NUM", CFG_SHORT, v, required_argument, d} +#define OPT_INCR(l, s, v, d) \ + {l, s, "NUM", CFG_INCREMENT, v, no_argument, d} + #define OPT_STRING(l, s, m, v, d) \ {l, s, m, CFG_STRING, v, required_argument, d} diff --git a/util/log.c b/util/log.c deleted file mode 100644 index 4a223546..00000000 --- a/util/log.c +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2021 SUSE LLC - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License version - * 2 as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * - * This file implements basic logging functionality. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#define LOG_FUNCNAME 1 -#include "log.h" -#include "cleanup.h" - -#ifndef LOG_CLOCK -#define LOG_CLOCK CLOCK_MONOTONIC -#endif - -int log_level = DEFAULT_LOGLEVEL; -bool log_timestamp; -bool log_pid; - -void __attribute__((format(printf, 3, 4))) -__msg(int lvl, const char *func, const char *format, ...) -{ - va_list ap; - char pidbuf[16]; - char timebuf[32]; - static const char *const formats[] = { - "%s%s%s", - "%s%s%s: ", - "%s<%s>%s ", - "%s<%s> %s: ", - "[%s] %s%s ", - "[%s]%s %s: ", - "[%s] <%s>%s ", - "[%s] <%s> %s: ", - }; - char *header __cleanup__(cleanup_charp) = NULL; - char *message __cleanup__(cleanup_charp) = NULL; - int idx; - - if (lvl > log_level) - return; - - if (log_timestamp) { - struct timespec now; - - clock_gettime(LOG_CLOCK, &now); - snprintf(timebuf, sizeof(timebuf), "%6ld.%06ld", - (long)now.tv_sec, now.tv_nsec / 1000); - } else - *timebuf = '\0'; - - if (log_pid) - snprintf(pidbuf, sizeof(pidbuf), "%ld", (long)getpid()); - else - *pidbuf = '\0'; - - idx = ((log_timestamp ? 1 : 0) << 2) | - ((log_pid ? 1 : 0) << 1) | (func ? 1 : 0); - - if (asprintf(&header, formats[idx], timebuf, pidbuf, func ? func : "") - == -1) - header = NULL; - - va_start(ap, format); - if (vasprintf(&message, format, ap) == -1) - message = NULL; - va_end(ap); - - fprintf(stderr, "%s%s", header ? header : "", - message ? message : ""); - -} diff --git a/util/log.h b/util/log.h deleted file mode 100644 index 15107a54..00000000 --- a/util/log.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2021 Martin Wilck, SUSE LLC - * SPDX-License-Identifier: LGPL-2.1-or-newer - */ -#ifndef _LOG_H -#define _LOG_H - -#ifndef MAX_LOGLEVEL -# define MAX_LOGLEVEL LOG_DEBUG -#endif -#ifndef DEFAULT_LOGLEVEL -# define DEFAULT_LOGLEVEL LOG_NOTICE -#endif - -#if (LOG_FUNCNAME == 1) -#define _log_func __func__ -#else -#define _log_func NULL -#endif - -extern int log_level; -extern bool log_timestamp; -extern bool log_pid; - -void __attribute__((format(printf, 3, 4))) -__msg(int lvl, const char *func, const char *format, ...); - -#define msg(lvl, format, ...) \ - do { \ - if ((lvl) <= MAX_LOGLEVEL) \ - __msg(lvl, _log_func, format, ##__VA_ARGS__); \ - } while (0) - -#endif /* _LOG_H */ -- 2.50.1