{"id":22912,"url":"https://patchwork.libcamera.org/api/patches/22912/?format=json","web_url":"https://patchwork.libcamera.org/patch/22912/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20250303154844.745574-4-barnabas.pocze@ideasonboard.com>","date":"2025-03-03T15:48:44","name":"[v1,3/3] libcamera: base: log: Do not instantiate disabled `LogMessage`s","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"cc2509cb3562c57bd99bc6237e39c21e8c09fb5f","submitter":{"id":216,"url":"https://patchwork.libcamera.org/api/people/216/?format=json","name":"Barnabás Pőcze","email":"barnabas.pocze@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22912/mbox/","series":[{"id":5037,"url":"https://patchwork.libcamera.org/api/series/5037/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5037","date":"2025-03-03T15:48:41","name":"libcamera: base: log: Do not instantiate disabled `LogMessage`s","version":1,"mbox":"https://patchwork.libcamera.org/series/5037/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22912/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22912/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 0EC00BD808\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  3 Mar 2025 15:48:57 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EA1BD6888A;\n\tMon,  3 Mar 2025 16:48:53 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C6E0A687EB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  3 Mar 2025 16:48:48 +0100 (CET)","from pb-laptop.local (185.221.143.4.nat.pool.zt.hu [185.221.143.4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 595582D5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  3 Mar 2025 16:47:17 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"pPLkcd50\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1741016837;\n\tbh=eNgn/rtqbfWTiwy4QYutsk8JaLfmp9Fb6qGLKYzOO4c=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=pPLkcd50qrvJz3KwOKkZe775T4SLeC18zqFr+H22ARwRHPdmy9ecyRVgUt3iNM287\n\tNHaFJ+Bg86VcvFG1TOONM29JSHjC34JduWVqb1/Ci/QnQuqvrPfJ4sF+EjXRMwv9S0\n\ty7wNYtTre8DvsuzqCAl0iRcFv5HkL9QQ95+MoAII=","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Subject":"[PATCH v1 3/3] libcamera: base: log: Do not instantiate disabled\n\t`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","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"At the moment every `LOG()` macro invocation results in a `LogMessage`\nbeing created, the message serialized into an `std::stringstream`.\nOnly in the destructor is is actually checked whether the corresponding\n`LogCategory` enables the given log level.\n\nThis is not too efficient, it would be better to skip the log message\nconstruction and all the `operator<<()` invocations if the message will\njust be discarded.\n\nThis could be easily if the `LOG()` macro accepted its arguments\nlike a traditional function as in that case an appropriate `if`\nstatement can be injected in a do-while loop. However, that is\nnot the case, the `LOG()` macro should effectively \"return\"\na stream.\n\nIt is not possible inject an `if` statement directly as that would\nlead to issues:\n\n  if (...)\n    LOG(...)\n  else\n   ...\n\nThe `else` would bind the to the `if` in the `LOG()` macro. This is\ndiagnosed by `-Wdangling-else`.\n\nAn alternative approach would be to use a `for` loop and force\na single iteration using a boolean flag or similar. This is entirely\ndoable but I think the implemented approach is easier to understand.\n\nThis change implements the early log level checking using a `switch`\nstatement as this avoids the dangling else related issues. One small\nissue arises because having a boolean controlling expression is diagnosed\nby clang (`-Wswitch-bool`); the result is cast to `int` to avoid the warning.\n\nSigned-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n---\n\nOne remaining questions is the handling of \"Fatal\" log messages. I think\nit would make sense to handle them separately because that way the compiler\ncould be told that it is actually a `[[noreturn]]` function call.\n\n---\n include/libcamera/base/log.h | 9 +++++++--\n 1 file changed, 7 insertions(+), 2 deletions(-)\n\n--\n2.48.1","diff":"diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h\nindex 958cb488d..1a14b8dbc 100644\n--- a/include/libcamera/base/log.h\n+++ b/include/libcamera/base/log.h\n@@ -108,10 +108,15 @@ LogMessage _log(const LogCategory &category, LogSeverity severity,\n #ifndef __DOXYGEN__\n #define _LOG_CATEGORY(name) logCategory##name\n\n+#define _LOG(cat, sev) \\\n+\tswitch (const auto &_logCategory = (cat); int(_logCategory.severity() <= Log##sev)) \\\n+\tcase 1: \\\n+\t\t_log(_logCategory, Log##sev).stream()\n+\n #define _LOG1(severity) \\\n-\t_log(LogCategory::defaultCategory(), Log##severity).stream()\n+\t_LOG(LogCategory::defaultCategory(), severity)\n #define _LOG2(category, severity) \\\n-\t_log(_LOG_CATEGORY(category)(), Log##severity).stream()\n+\t_LOG(_LOG_CATEGORY(category)(), severity)\n\n /*\n  * Expand the LOG() macro to _LOG1() or _LOG2() based on the number of\n","prefixes":["v1","3/3"]}