From patchwork Sun Jan 6 02:33:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 151 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4135260B2E for ; Sun, 6 Jan 2019 03:32:27 +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 C3E2198C for ; Sun, 6 Jan 2019 03:32:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1546741946; bh=0XeIsh/ltcYJ77DUd2giB2BsNTR17n8e+KhSX8engPE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=J7qOj624zfagj9Mcgl0NVk5dYO6Jjo6juyH33s8O7m09c/PSUHsAP7vxMJcnnWNEq tCPHnVKPQ56DWhlDtcu8rLsNjND+IUWmHfJ+qnW3VMMiWHgw+MPa1Z81TKNzIQuBG+ tH9UtAsBt4d6OOxv45fKMGu8sRP2BQ6MN/RReFvo= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sun, 6 Jan 2019 04:33:19 +0200 Message-Id: <20190106023328.10989-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190106023328.10989-1-laurent.pinchart@ideasonboard.com> References: <20190106023328.10989-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 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: Sun, 06 Jan 2019 02:32:27 -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 --- 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..774916f04274 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() +#ifdef 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..4165cbd654fc 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 is failed + * + * 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[] = {