From patchwork Mon Jan 19 11:31:00 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: 25847 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 D4EFEC327D for ; Mon, 19 Jan 2026 11:31:14 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B39F861FC6; Mon, 19 Jan 2026 12:31:10 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="aGG5poCg"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3787961FB9 for ; Mon, 19 Jan 2026 12:31:08 +0100 (CET) Received: from pb-laptop.local (185.221.143.114.nat.pool.zt.hu [185.221.143.114]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B6728C79 for ; Mon, 19 Jan 2026 12:30:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1768822237; bh=emvrmnnI4/cToii6BSjuUIOwHrDklh9jfJ5ZFeVMMdo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=aGG5poCgpTCFE2f9YITw3QM6OmsrlA8a7sbsYpEh7oPHTGl3zhibvdWMx6AR7lPKA +XvCJOjkLKF6CbIBPWwq/Cq2DP0r6DxH+FMKAzhfDsYCRShRyo51NFvGRa9H8qMGk+ 1odfw46yQMBnLBLFiart0CTgxPu0zmPzhwi/qi/k= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v2 2/5] libcamera: base: log: Do not instantiate disabled `LogMessage`s Date: Mon, 19 Jan 2026 12:31:00 +0100 Message-ID: <20260119113104.3560802-3-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260119113104.3560802-1-barnabas.pocze@ideasonboard.com> References: <20260119113104.3560802-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 it actually checked whether the given `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 done 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 Reviewed-by: Kieran Bingham --- include/libcamera/base/log.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index d8338d8df..dbd14951c 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