From patchwork Thu May 7 13:50:18 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 26675 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 82930BDCB5 for ; Thu, 7 May 2026 13:50:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2283062FE1; Thu, 7 May 2026 15:50:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="WHiaXJRj"; dkim-atps=neutral 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 2A16862010 for ; Thu, 7 May 2026 15:50:23 +0200 (CEST) Received: from pb-laptop.local (185.221.140.217.nat.pool.zt.hu [185.221.140.217]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id F257A9CE for ; Thu, 7 May 2026 15:50:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1778161819; bh=Cbz7nGOmKR6jtyclUktyqIFsQT2l1B7AgHO3NPLOyY0=; h=From:To:Subject:Date:From; b=WHiaXJRjkJfxiojNCTTkBknEf8Rs40uHZoR/hhrsjmzC//LY2o+nZ6BJhI2d8CCKJ PyhNr+GX3X9KUECzjbRr/ekMq7h4egA1pOrFrIVx9u1DoX/6W8jqMC45YDNlA1v6Up 29IEqM/cFj/hSc0zOfbQs8LbdtaP7sNV4UFaGIsM= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 1/2] libcamera: base: log: Remove log level check for "Fatal" messages Date: Thu, 7 May 2026 15:50:18 +0200 Message-ID: <20260507135019.231615-1-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" When a fatal log message is used, it is expected that it will be displayed and will in some way abort the execution. Since the log level of each message is known at compile time, the log level check can be short-circuited if a fatal message is intended. This also essentially reverts 2318a2863baa ("libcamera: base: log: Inline `LOG()` into `ASSERT()`") as there is no need to inline the `_log()` calls to avoid the runtime condition. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart --- include/libcamera/base/log.h | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 0751387a4..5cb14311e 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -108,10 +108,22 @@ LogMessage _log(const LogCategory &category, LogSeverity severity, #ifndef __DOXYGEN__ #define _LOG_CATEGORY(name) logCategory##name -#define _LOG(cat, sev) \ - switch (const auto &_logCategory = (cat); \ - static_cast(_logCategory.severity() <= Log##sev)) \ - case 1: \ +/* Returns `int` to avoid `-Wswitch-bool` below. */ +template +constexpr int isLogSeverityEnabled(const LogCategory &category) +{ + static_assert(LogDebug <= Severity && Severity <= LogFatal); + + if constexpr (Severity < LogFatal) + return static_cast(category.severity()) <= Severity; + else + return true; +} + +#define _LOG(cat, sev) \ + switch (const auto &_logCategory = (cat); \ + isLogSeverityEnabled(_logCategory)) \ + case 1: \ _log(_logCategory, Log##sev).stream() #define _LOG1(severity) \ @@ -130,11 +142,11 @@ LogMessage _log(const LogCategory &category, LogSeverity severity, #endif /* __DOXYGEN__ */ #ifndef NDEBUG -#define ASSERT(condition) static_cast(({ \ - if (!(condition)) \ - _log(LogCategory::defaultCategory(), LogFatal).stream() \ - << "assertion \"" #condition "\" failed in " \ - << __func__ << "()"; \ +#define ASSERT(condition) static_cast(({ \ + if (!(condition)) \ + LOG(Fatal) \ + << "assertion \"" #condition "\" failed in " \ + << __func__ << "()"; \ })) #else #define ASSERT(condition) static_cast(false && (condition)) From patchwork Thu May 7 13:50:19 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 26676 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 5A94DC32F6 for ; Thu, 7 May 2026 13:50:28 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D6BFF63022; Thu, 7 May 2026 15:50:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Rp++ssbl"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 48D0F62FE1 for ; Thu, 7 May 2026 15:50:23 +0200 (CEST) Received: from pb-laptop.local (185.221.140.217.nat.pool.zt.hu [185.221.140.217]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 3381AB63 for ; Thu, 7 May 2026 15:50:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1778161819; bh=sGI/a0lFvnPtfKM87kQIP6yxhvAg5n9A8DchwYDg6GY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Rp++ssblMngR/4Jnp+xIQT644CqlXVmTehvVuFwtlqWtAXQkaEyDjomt9sZlE7oHa TyLVKsXTyUVOmZHwS/HB3us5kUZhVxy42jLxhp8y9YXx0WyRqk+FfqoksTvX91NXOa kueA+E6KkODYHG7yZVzwIA+4BCDYB3po/SobpAig= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 2/2] libcamera: base: log: Move abort logic into separate type Date: Thu, 7 May 2026 15:50:19 +0200 Message-ID: <20260507135019.231615-2-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260507135019.231615-1-barnabas.pocze@ideasonboard.com> References: <20260507135019.231615-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a `LogMessageAbortGuard` type that is templated on the severity of the log message. If it is not fatal, the type is empty and does nothing. However, if it is `LogFatal`, then its destructor will print the backtrace and abort. This type is instantiated for each log message and takes care of aborting the processing if necessary. This allows the removal of the abort logic from `~LogMessage()` and also makes it visible to static analysis tools that execution will not proceed beyond a `Fatal` message. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart --- include/libcamera/base/log.h | 16 +++++++++++++++- src/libcamera/base/log.cpp | 16 +++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 5cb14311e..7bd43a423 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -88,6 +88,19 @@ private: std::string prefix_; }; +#ifndef __DOXYGEN__ +template +struct LogMessageAbortGuard { +}; + +template<> +struct LogMessageAbortGuard { + LogMessageAbortGuard() = default; + LIBCAMERA_DISABLE_COPY_AND_MOVE(LogMessageAbortGuard) + [[noreturn]] ~LogMessageAbortGuard(); +}; +#endif + class Loggable { public: @@ -124,7 +137,8 @@ constexpr int isLogSeverityEnabled(const LogCategory &category) switch (const auto &_logCategory = (cat); \ isLogSeverityEnabled(_logCategory)) \ case 1: \ - _log(_logCategory, Log##sev).stream() + (LogMessageAbortGuard(), \ + _log(_logCategory, Log##sev).stream()) #define _LOG1(severity) \ _LOG(LogCategory::defaultCategory(), severity) diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 0921e5485..3404471fc 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -869,11 +869,6 @@ LogMessage::~LogMessage() msgStream_ << std::endl; logger->write(*this); - - if (severity_ == LogSeverity::LogFatal) { - logger->backtrace(); - std::abort(); - } } /** @@ -922,6 +917,17 @@ LogMessage::~LogMessage() * \return The message text of the message, as a string */ +#ifndef __DOXYGEN__ +LogMessageAbortGuard::~LogMessageAbortGuard() +{ + Logger *logger = Logger::instance(); + if (logger) + logger->backtrace(); + + std::abort(); +} +#endif + /** * \class Loggable * \brief Base class to support log message extensions