summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2021-01-21 06:10:54 +0300
committerTom Rini <trini@konsulko.com>2021-03-13 01:41:35 +0300
commit7bd06587decafabb56f68de3ae87adb4c49ca8db (patch)
tree79186153ce0f6339b3f172bdffcd300e502e64db
parent9ad7a6c25c7142a46fe4b811c13bc3280c4bb27f (diff)
downloadu-boot-7bd06587decafabb56f68de3ae87adb4c49ca8db.tar.xz
log: Add return-checking macros for 0 being success
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>
-rw-r--r--doc/develop/logging.rst15
-rw-r--r--include/log.h20
2 files changed, 34 insertions, 1 deletions
diff --git a/doc/develop/logging.rst b/doc/develop/logging.rst
index 622ad6ad1a..f4e925048e 100644
--- a/doc/develop/logging.rst
+++ b/doc/develop/logging.rst
@@ -117,11 +117,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 c0453d2f97..6ef891d4d2 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 */