All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [PATCH v2 3/6] log: Add return-checking macros for 0 being success
Date: Wed, 20 Jan 2021 20:10:54 -0700	[thread overview]
Message-ID: <20210121031057.559301-4-sjg@chromium.org> (raw)
In-Reply-To: <20210121031057.559301-1-sjg@chromium.org>

The existing log_ret() and log_msg_ret() macros consider an error to be
less than zero. But some function may return a positive number to indicate
a different kind of failure. Add macros to check for that also.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

(no changes since v1)

 doc/develop/logging.rst | 15 ++++++++++++++-
 include/log.h           | 20 ++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/doc/develop/logging.rst b/doc/develop/logging.rst
index 482c17f7800..b6c6b45049f 100644
--- a/doc/develop/logging.rst
+++ b/doc/develop/logging.rst
@@ -119,11 +119,24 @@ can be used whenever your function returns an error value:
 
 .. code-block:: c
 
-   return log_ret(uclass_first_device(UCLASS_MMC, &dev));
+   return log_ret(uclass_first_device_err(UCLASS_MMC, &dev));
 
 This will write a log record when an error code is detected (a value < 0). This
 can make it easier to trace errors that are generated deep in the call stack.
 
+The log_msg_ret() variant will print a short string if CONFIG_LOG_ERROR_RETURN
+is enabled. So long as the string is unique within the function you can normally
+determine exactly which call failed:
+
+.. code-block:: c
+
+   ret = gpio_request_by_name(dev, "cd-gpios", 0, &desc, GPIOD_IS_IN);
+   if (ret)
+      return log_msg_ret("gpio", ret);
+
+Some functions return 0 for success and any other value is an error. For these,
+log_retz() and log_msg_retz() are available.
+
 Convenience functions
 ~~~~~~~~~~~~~~~~~~~~~
 
diff --git a/include/log.h b/include/log.h
index c0453d2f97c..6ef891d4d2d 100644
--- a/include/log.h
+++ b/include/log.h
@@ -316,10 +316,30 @@ void __assert_fail(const char *assertion, const char *file, unsigned int line,
 		    __ret); \
 	__ret; \
 	})
+
+/*
+ * Similar to the above, but any non-zero value is consider an error, not just
+ * values less than 0.
+ */
+#define log_retz(_ret) ({ \
+	int __ret = (_ret); \
+	if (__ret) \
+		log(LOG_CATEGORY, LOGL_ERR, "returning err=%d\n", __ret); \
+	__ret; \
+	})
+#define log_msg_retz(_msg, _ret) ({ \
+	int __ret = (_ret); \
+	if (__ret) \
+		log(LOG_CATEGORY, LOGL_ERR, "%s: returning err=%d\n", _msg, \
+		    __ret); \
+	__ret; \
+	})
 #else
 /* Non-logging versions of the above which just return the error code */
 #define log_ret(_ret) (_ret)
 #define log_msg_ret(_msg, _ret) ((void)(_msg), _ret)
+#define log_retz(_ret) (_ret)
+#define log_msg_retz(_msg, _ret) ((void)(_msg), _ret)
 #endif
 
 /** * enum log_rec_flags - Flags for a log record */
-- 
2.30.0.296.g2bfb1c46d8-goog

  parent reply	other threads:[~2021-01-21  3:10 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-21  3:10 [PATCH v2 0/6] log: Allow multiple lines and conversion to printf() Simon Glass
2021-01-21  3:10 ` [PATCH v2 1/6] log: Set up a flag byte for log records Simon Glass
2021-03-15 15:52   ` Tom Rini
2021-01-21  3:10 ` [PATCH v2 2/6] log: Handle line continuation Simon Glass
2021-03-15 15:52   ` Tom Rini
2021-01-21  3:10 ` Simon Glass [this message]
2021-03-15 15:52   ` [PATCH v2 3/6] log: Add return-checking macros for 0 being success Tom Rini
2021-01-21  3:10 ` [PATCH v2 4/6] sandbox: log: Avoid build error with !CONFIG_LOG Simon Glass
2021-01-21  3:10 ` [PATCH v2 5/6] tpm: Don't select LOG Simon Glass
2021-03-15 15:52   ` Tom Rini
2021-01-21  3:10 ` [PATCH v2 6/6] log: Convert log values to printf() if not enabled Simon Glass
2021-02-05  1:28   ` Sean Anderson
2021-02-05  3:31     ` Simon Glass
2021-03-14 19:33   ` Tom Rini
2021-03-14 20:32     ` Simon Glass
2021-02-05  1:20 ` [PATCH v2 0/6] log: Allow multiple lines and conversion to printf() Simon Glass

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=20210121031057.559301-4-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.