From patchwork Mon Jan 7 23:11:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 165 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1C51A60B2F for ; Tue, 8 Jan 2019 00:10:48 +0100 (CET) Received: from avalon.bb.dnainternet.fi (dfj612ybrt5fhg77mgycy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:2e86:4862:ef6a:2804]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A91BCE51 for ; Tue, 8 Jan 2019 00:10:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1546902647; bh=1iJndoZVAjgGU+M99GCZ2vKTWSQf5zRnJfjSlaFkzvE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ceChrHHLXVXW7nm0G4+VbJS/pkSox5zFm4mfxuXhx7YolfJ+I/hFlZ0nsQI7Fetbi +hCZ8bLQON9n4e+6m4/mSfrGaL9/tI6+raAU+2GmfPF26cM9csufai38sEETPLDupc 1INHcZJf9GfIZjFdxdFv8WFbtdcw5DMmbaagTTI0= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 8 Jan 2019 01:11:42 +0200 Message-Id: <20190107231151.23291-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190107231151.23291-1-laurent.pinchart@ideasonboard.com> References: <20190107231151.23291-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 02/11] libcamera: log: Add an ASSERT macro X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Jan 2019 23:10:48 -0000 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 Reviewed-by: Niklas Söderlund Reviewed-by: Kieran Bingham --- Changes since v1: - Fix syntax error in ASSERT() macro - Fix NDEBUG conditional compilation - Fix typo in documentation - Fix ASSERT() condition check --- src/libcamera/include/log.h | 9 +++++++++ src/libcamera/log.cpp | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/libcamera/include/log.h b/src/libcamera/include/log.h index 03842be02d0e..c1af3741ffee 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(({ \ + if (!(condition)) \ + LOG(Fatal) << "assertion \"" #condition "\" failed"; \ +})) +#else +#define ASSERT(condition) static_cast(false && (condition)) +#endif + } /* namespace libcamera */ #endif /* __LIBCAMERA_LOG_H__ */ diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp index a5823c64eaa6..6785d371449e 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[] = {