From patchwork Mon Mar 3 15:48:44 2025 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: 22912 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 0EC00BD808 for ; Mon, 3 Mar 2025 15:48:57 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EA1BD6888A; Mon, 3 Mar 2025 16:48:53 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="pPLkcd50"; 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 C6E0A687EB for ; Mon, 3 Mar 2025 16:48:48 +0100 (CET) Received: from pb-laptop.local (185.221.143.4.nat.pool.zt.hu [185.221.143.4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 595582D5 for ; Mon, 3 Mar 2025 16:47:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1741016837; bh=eNgn/rtqbfWTiwy4QYutsk8JaLfmp9Fb6qGLKYzOO4c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=pPLkcd50qrvJz3KwOKkZe775T4SLeC18zqFr+H22ARwRHPdmy9ecyRVgUt3iNM287 NHaFJ+Bg86VcvFG1TOONM29JSHjC34JduWVqb1/Ci/QnQuqvrPfJ4sF+EjXRMwv9S0 y7wNYtTre8DvsuzqCAl0iRcFv5HkL9QQ95+MoAII= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 3/3] libcamera: base: log: Do not instantiate disabled `LogMessage`s Date: Mon, 3 Mar 2025 16:48:44 +0100 Message-ID: <20250303154844.745574-4-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250303154844.745574-1-barnabas.pocze@ideasonboard.com> References: <20250303154844.745574-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" At the moment every `LOG()` macro invocation results in a `LogMessage` being created, the message serialized into an `std::stringstream`. Only in the destructor is is actually checked whether the corresponding `LogCategory` enables the given log level. This is not too efficient, it would be better to skip the log message construction and all the `operator<<()` invocations if the message will just be discarded. This could be easily if the `LOG()` macro accepted its arguments like a traditional function as in that case an appropriate `if` statement can be injected in a do-while loop. However, that is not the case, the `LOG()` macro should effectively "return" a stream. It is not possible inject an `if` statement directly as that would lead to issues: if (...) LOG(...) else ... The `else` would bind the to the `if` in the `LOG()` macro. This is diagnosed by `-Wdangling-else`. An alternative approach would be to use a `for` loop and force a single iteration using a boolean flag or similar. This is entirely doable but I think the implemented approach is easier to understand. This change implements the early log level checking using a `switch` statement as this avoids the dangling else related issues. One small issue arises because having a boolean controlling expression is diagnosed by clang (`-Wswitch-bool`); the result is cast to `int` to avoid the warning. Signed-off-by: Barnabás Pőcze --- One remaining questions is the handling of "Fatal" log messages. I think it would make sense to handle them separately because that way the compiler could be told that it is actually a `[[noreturn]]` function call. --- include/libcamera/base/log.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) -- 2.48.1 diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 958cb488d..1a14b8dbc 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -108,10 +108,15 @@ LogMessage _log(const LogCategory &category, LogSeverity severity, #ifndef __DOXYGEN__ #define _LOG_CATEGORY(name) logCategory##name +#define _LOG(cat, sev) \ + switch (const auto &_logCategory = (cat); int(_logCategory.severity() <= Log##sev)) \ + case 1: \ + _log(_logCategory, Log##sev).stream() + #define _LOG1(severity) \ - _log(LogCategory::defaultCategory(), Log##severity).stream() + _LOG(LogCategory::defaultCategory(), severity) #define _LOG2(category, severity) \ - _log(_LOG_CATEGORY(category)(), Log##severity).stream() + _LOG(_LOG_CATEGORY(category)(), severity) /* * Expand the LOG() macro to _LOG1() or _LOG2() based on the number of