]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
fabrics: remove log.c in favour of libnvme routines
authorHannes Reinecke <hare@suse.de>
Mon, 14 Jun 2021 14:26:45 +0000 (16:26 +0200)
committerHannes Reinecke <hare@suse.de>
Wed, 16 Jun 2021 17:52:55 +0000 (19:52 +0200)
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 <hare@suse.de>
Makefile
fabrics.c
util/argconfig.h
util/log.c [deleted file]
util/log.h [deleted file]

index bc9a94e52b5cc44b68d741ccf4e59cd52bdea9d2..5fbdfd0bf697e37fabfb7f4bdf3923e754d121f5 100644 (file)
--- 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
index 8eb91224dd093b7e9555d108934cc5bf1854a4dd..5925e75d26fe3dc605f05894fea8708a996ed15d 100644 (file)
--- 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",
index 3147277a3dc18a5b4bbc96cd5ac5889239cb1830..1a5c693676cea235fb32a1433b748ab613e5f5ac 100644 (file)
@@ -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 (file)
index 4a22354..0000000
+++ /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 <sys/types.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <stdbool.h>
-#include <syslog.h>
-#include <unistd.h>
-#include <time.h>
-#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 : "<error>",
-               message ? message : "<error>");
-
-}
diff --git a/util/log.h b/util/log.h
deleted file mode 100644 (file)
index 15107a5..0000000
+++ /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 */