summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-01-04 17:46:35 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-01-08 16:23:16 +0200
commitce24c052de30fc19341685a3eb38485b2e9587e8 (patch)
treee5cb4ff05fde414e673860149061be2d824b353a
parent99a3e7bcfb38fea95092dcfff99a6b84f42f456c (diff)
libcamera: log: Add an ASSERT macro
The ASSERT() macro is similar to the assert() macro defined by the C standard, but uses the libcamera logging infrastructure. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
-rw-r--r--src/libcamera/include/log.h9
-rw-r--r--src/libcamera/log.cpp16
2 files changed, 25 insertions, 0 deletions
diff --git a/src/libcamera/include/log.h b/src/libcamera/include/log.h
index 03842be0..c1af3741 100644
--- a/src/libcamera/include/log.h
+++ b/src/libcamera/include/log.h
@@ -36,6 +36,15 @@ private:
#define LOG(severity) LogMessage(__FILE__, __LINE__, Log##severity).stream()
+#ifndef NDEBUG
+#define ASSERT(condition) static_cast<void>(({ \
+ if (!(condition)) \
+ LOG(Fatal) << "assertion \"" #condition "\" failed"; \
+}))
+#else
+#define ASSERT(condition) static_cast<void>(false && (condition))
+#endif
+
} /* namespace libcamera */
#endif /* __LIBCAMERA_LOG_H__ */
diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp
index a5823c64..6785d371 100644
--- a/src/libcamera/log.cpp
+++ b/src/libcamera/log.cpp
@@ -48,6 +48,22 @@ namespace libcamera {
* terminates immediately after printing the message.
*/
+/**
+ * \def ASSERT(condition)
+ * \brief Abort program execution if assertion fails
+ *
+ * If \a condition is false, ASSERT() logs an error message with the Fatal log
+ * level and aborts program execution.
+ *
+ * If the macro NDEBUG is defined before including log.h, ASSERT() generates no
+ * code.
+ *
+ * Using conditions that have side effects with ASSERT() is not recommended, as
+ * these effects would depend on whether NDEBUG is defined or not. Similarly,
+ * ASSERT() should not be used to check for errors that can occur under normal
+ * conditions as those checks would then be removed when compiling with NDEBUG.
+ */
+
static const char *log_severity_name(LogSeverity severity)
{
static const char * const names[] = {