anitazha / rpms / ndctl

Forked from rpms/ndctl 2 years ago
Clone
Jeff Moyer 2c91dc
From 5e1c1ab5bf6b6257552ad9fa242483ec1c1de006 Mon Sep 17 00:00:00 2001
Jeff Moyer 2c91dc
From: Dan Williams <dan.j.williams@intel.com>
Jeff Moyer 2c91dc
Date: Sun, 23 Jan 2022 16:53:55 -0800
Jeff Moyer 2c91dc
Subject: [PATCH 110/217] cxl/memdev: Use a local logger for debug
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
The "fail()" macro skips some of the nicer features of the centralized
Jeff Moyer 2c91dc
logger. Add one to supplement the library logger.
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
Link: https://lore.kernel.org/r/164298563547.3021641.16504008034705274247.stgit@dwillia2-desk3.amr.corp.intel.com
Jeff Moyer 2c91dc
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Jeff Moyer 2c91dc
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
Jeff Moyer 2c91dc
---
Jeff Moyer 2c91dc
 cxl/memdev.c | 48 ++++++++++++++++++++++++------------------------
Jeff Moyer 2c91dc
 1 file changed, 24 insertions(+), 24 deletions(-)
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
diff --git a/cxl/memdev.c b/cxl/memdev.c
Jeff Moyer 2c91dc
index b9141be..327c260 100644
Jeff Moyer 2c91dc
--- a/cxl/memdev.c
Jeff Moyer 2c91dc
+++ b/cxl/memdev.c
Jeff Moyer 2c91dc
@@ -26,11 +26,7 @@ static struct parameters {
Jeff Moyer 2c91dc
 	bool verbose;
Jeff Moyer 2c91dc
 } param;
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
-#define fail(fmt, ...) \
Jeff Moyer 2c91dc
-do { \
Jeff Moyer 2c91dc
-	fprintf(stderr, "cxl-%s:%s:%d: " fmt, \
Jeff Moyer 2c91dc
-			VERSION, __func__, __LINE__, ##__VA_ARGS__); \
Jeff Moyer 2c91dc
-} while (0)
Jeff Moyer 2c91dc
+static struct log_ctx ml;
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 #define BASE_OPTIONS() \
Jeff Moyer 2c91dc
 OPT_BOOLEAN('v',"verbose", &param.verbose, "turn on debug")
Jeff Moyer 2c91dc
@@ -79,7 +75,7 @@ static int action_zero(struct cxl_memdev *memdev, struct action_context *actx)
Jeff Moyer 2c91dc
 		size = cxl_memdev_get_label_size(memdev);
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	if (cxl_memdev_nvdimm_bridge_active(memdev)) {
Jeff Moyer 2c91dc
-		fprintf(stderr,
Jeff Moyer 2c91dc
+		log_err(&ml,
Jeff Moyer 2c91dc
 			"%s: has active nvdimm bridge, abort label write\n",
Jeff Moyer 2c91dc
 			cxl_memdev_get_devname(memdev));
Jeff Moyer 2c91dc
 		return -EBUSY;
Jeff Moyer 2c91dc
@@ -87,7 +83,7 @@ static int action_zero(struct cxl_memdev *memdev, struct action_context *actx)
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	rc = cxl_memdev_zero_label(memdev, size, param.offset);
Jeff Moyer 2c91dc
 	if (rc < 0)
Jeff Moyer 2c91dc
-		fprintf(stderr, "%s: label zeroing failed: %s\n",
Jeff Moyer 2c91dc
+		log_err(&ml, "%s: label zeroing failed: %s\n",
Jeff Moyer 2c91dc
 			cxl_memdev_get_devname(memdev), strerror(-rc));
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	return rc;
Jeff Moyer 2c91dc
@@ -100,7 +96,7 @@ static int action_write(struct cxl_memdev *memdev, struct action_context *actx)
Jeff Moyer 2c91dc
 	int rc;
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	if (cxl_memdev_nvdimm_bridge_active(memdev)) {
Jeff Moyer 2c91dc
-		fprintf(stderr,
Jeff Moyer 2c91dc
+		log_err(&ml,
Jeff Moyer 2c91dc
 			"%s: has active nvdimm bridge, abort label write\n",
Jeff Moyer 2c91dc
 			cxl_memdev_get_devname(memdev));
Jeff Moyer 2c91dc
 		return -EBUSY;
Jeff Moyer 2c91dc
@@ -114,7 +110,7 @@ static int action_write(struct cxl_memdev *memdev, struct action_context *actx)
Jeff Moyer 2c91dc
 		fseek(actx->f_in, 0L, SEEK_SET);
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 		if (size > label_size) {
Jeff Moyer 2c91dc
-			fprintf(stderr,
Jeff Moyer 2c91dc
+			log_err(&ml,
Jeff Moyer 2c91dc
 				"File size (%zu) greater than label area size (%zu), aborting\n",
Jeff Moyer 2c91dc
 				size, label_size);
Jeff Moyer 2c91dc
 			return -EINVAL;
Jeff Moyer 2c91dc
@@ -133,7 +129,7 @@ static int action_write(struct cxl_memdev *memdev, struct action_context *actx)
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	rc = cxl_memdev_write_label(memdev, buf, size, param.offset);
Jeff Moyer 2c91dc
 	if (rc < 0)
Jeff Moyer 2c91dc
-		fprintf(stderr, "%s: label write failed: %s\n",
Jeff Moyer 2c91dc
+		log_err(&ml, "%s: label write failed: %s\n",
Jeff Moyer 2c91dc
 			cxl_memdev_get_devname(memdev), strerror(-rc));
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 out:
Jeff Moyer 2c91dc
@@ -158,7 +154,7 @@ static int action_read(struct cxl_memdev *memdev, struct action_context *actx)
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	rc = cxl_memdev_read_label(memdev, buf, size, param.offset);
Jeff Moyer 2c91dc
 	if (rc < 0) {
Jeff Moyer 2c91dc
-		fprintf(stderr, "%s: label read failed: %s\n",
Jeff Moyer 2c91dc
+		log_err(&ml, "%s: label read failed: %s\n",
Jeff Moyer 2c91dc
 			cxl_memdev_get_devname(memdev), strerror(-rc));
Jeff Moyer 2c91dc
 		goto out;
Jeff Moyer 2c91dc
 	}
Jeff Moyer 2c91dc
@@ -188,6 +184,7 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
Jeff Moyer 2c91dc
 	};
Jeff Moyer 2c91dc
 	unsigned long id;
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
+	log_init(&ml, "cxl memdev", "CXL_MEMDEV_LOG");
Jeff Moyer 2c91dc
 	argc = parse_options(argc, argv, options, u, 0);
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	if (argc == 0)
Jeff Moyer 2c91dc
@@ -200,8 +197,8 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
Jeff Moyer 2c91dc
 		}
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 		if (sscanf(argv[i], "mem%lu", &id) != 1) {
Jeff Moyer 2c91dc
-			fprintf(stderr, "'%s' is not a valid memdev name\n",
Jeff Moyer 2c91dc
-					argv[i]);
Jeff Moyer 2c91dc
+			log_err(&ml, "'%s' is not a valid memdev name\n",
Jeff Moyer 2c91dc
+				argv[i]);
Jeff Moyer 2c91dc
 			err++;
Jeff Moyer 2c91dc
 		}
Jeff Moyer 2c91dc
 	}
Jeff Moyer 2c91dc
@@ -216,8 +213,8 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
Jeff Moyer 2c91dc
 	else {
Jeff Moyer 2c91dc
 		actx.f_out = fopen(param.outfile, "w+");
Jeff Moyer 2c91dc
 		if (!actx.f_out) {
Jeff Moyer 2c91dc
-			fprintf(stderr, "failed to open: %s: (%s)\n",
Jeff Moyer 2c91dc
-					param.outfile, strerror(errno));
Jeff Moyer 2c91dc
+			log_err(&ml, "failed to open: %s: (%s)\n",
Jeff Moyer 2c91dc
+				param.outfile, strerror(errno));
Jeff Moyer 2c91dc
 			rc = -errno;
Jeff Moyer 2c91dc
 			goto out;
Jeff Moyer 2c91dc
 		}
Jeff Moyer 2c91dc
@@ -228,15 +225,18 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
Jeff Moyer 2c91dc
 	} else {
Jeff Moyer 2c91dc
 		actx.f_in = fopen(param.infile, "r");
Jeff Moyer 2c91dc
 		if (!actx.f_in) {
Jeff Moyer 2c91dc
-			fprintf(stderr, "failed to open: %s: (%s)\n",
Jeff Moyer 2c91dc
-					param.infile, strerror(errno));
Jeff Moyer 2c91dc
+			log_err(&ml, "failed to open: %s: (%s)\n", param.infile,
Jeff Moyer 2c91dc
+				strerror(errno));
Jeff Moyer 2c91dc
 			rc = -errno;
Jeff Moyer 2c91dc
 			goto out_close_fout;
Jeff Moyer 2c91dc
 		}
Jeff Moyer 2c91dc
 	}
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
-	if (param.verbose)
Jeff Moyer 2c91dc
+	if (param.verbose) {
Jeff Moyer 2c91dc
 		cxl_set_log_priority(ctx, LOG_DEBUG);
Jeff Moyer 2c91dc
+		ml.log_priority = LOG_DEBUG;
Jeff Moyer 2c91dc
+	} else
Jeff Moyer 2c91dc
+		ml.log_priority = LOG_INFO;
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	rc = 0;
Jeff Moyer 2c91dc
 	err = 0;
Jeff Moyer 2c91dc
@@ -299,8 +299,8 @@ int cmd_write_labels(int argc, const char **argv, struct cxl_ctx *ctx)
Jeff Moyer 2c91dc
 	int count = memdev_action(argc, argv, ctx, action_write, write_options,
Jeff Moyer 2c91dc
 			"cxl write-labels <memdev> [-i <filename>]");
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
-	fprintf(stderr, "wrote %d mem%s\n", count >= 0 ? count : 0,
Jeff Moyer 2c91dc
-			count > 1 ? "s" : "");
Jeff Moyer 2c91dc
+	log_info(&ml, "wrote %d mem%s\n", count >= 0 ? count : 0,
Jeff Moyer 2c91dc
+		 count > 1 ? "s" : "");
Jeff Moyer 2c91dc
 	return count >= 0 ? 0 : EXIT_FAILURE;
Jeff Moyer 2c91dc
 }
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
@@ -309,8 +309,8 @@ int cmd_read_labels(int argc, const char **argv, struct cxl_ctx *ctx)
Jeff Moyer 2c91dc
 	int count = memdev_action(argc, argv, ctx, action_read, read_options,
Jeff Moyer 2c91dc
 			"cxl read-labels <mem0> [<mem1>..<memN>] [-o <filename>]");
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
-	fprintf(stderr, "read %d mem%s\n", count >= 0 ? count : 0,
Jeff Moyer 2c91dc
-			count > 1 ? "s" : "");
Jeff Moyer 2c91dc
+	log_info(&ml, "read %d mem%s\n", count >= 0 ? count : 0,
Jeff Moyer 2c91dc
+		 count > 1 ? "s" : "");
Jeff Moyer 2c91dc
 	return count >= 0 ? 0 : EXIT_FAILURE;
Jeff Moyer 2c91dc
 }
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
@@ -319,7 +319,7 @@ int cmd_zero_labels(int argc, const char **argv, struct cxl_ctx *ctx)
Jeff Moyer 2c91dc
 	int count = memdev_action(argc, argv, ctx, action_zero, zero_options,
Jeff Moyer 2c91dc
 			"cxl zero-labels <mem0> [<mem1>..<memN>] [<options>]");
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
-	fprintf(stderr, "zeroed %d mem%s\n", count >= 0 ? count : 0,
Jeff Moyer 2c91dc
-			count > 1 ? "s" : "");
Jeff Moyer 2c91dc
+	log_info(&ml, "zeroed %d mem%s\n", count >= 0 ? count : 0,
Jeff Moyer 2c91dc
+		 count > 1 ? "s" : "");
Jeff Moyer 2c91dc
 	return count >= 0 ? 0 : EXIT_FAILURE;
Jeff Moyer 2c91dc
 }
Jeff Moyer 2c91dc
-- 
Jeff Moyer 2c91dc
2.27.0
Jeff Moyer 2c91dc