From patchwork Mon Mar 3 15:48:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 22910 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 ED6D0BD808 for ; Mon, 3 Mar 2025 15:48:53 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 03594687EB; Mon, 3 Mar 2025 16:48:51 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ZmyOS/dB"; 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 595C168777 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 D017E8DB for ; Mon, 3 Mar 2025 16:47:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1741016836; bh=R4zYeWhgQ8GXqDxHddFIi9PKdM0ZVS9Hx/9PUBXa/WM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ZmyOS/dBSezu7OPNNB8cQPJV62ejHXwZkNVqOlH8Ocv1qnEymxqrJv8ZMcki59KoU z7kVBzKRXDwaDXAg7b4vDxRtJYK8AEtsIkw/6n4gKA32rQN3NBUwCu9iN7aPT5pSX+ O9GDf7BaJUjItkRrUYDS+uGhdlQ2s/Gvr++D7uvE= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 1/3] libcamera: base: log: Take `LogCategory` by reference Date: Mon, 3 Mar 2025 16:48:42 +0100 Message-ID: <20250303154844.745574-2-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" When no log category is specified, `nullptr` is passed, and then the `_log()` function implementations replace that with `LogCategory::defaultCategory()`. But since the call site always knows the log category, this condition can be removed and the `_LOG1()` macro can use `LogCategory::defaultCategory()`. So remove the condition from the `_log()` implementations and use references to refer to log categories. Reviewed-by: Laurent Pinchart --- include/libcamera/base/log.h | 8 ++++---- src/libcamera/base/log.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 8af74b59d..958cb488d 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -96,12 +96,12 @@ public: protected: virtual std::string logPrefix() const = 0; - LogMessage _log(const LogCategory *category, LogSeverity severity, + LogMessage _log(const LogCategory &category, LogSeverity severity, const char *fileName = __builtin_FILE(), unsigned int line = __builtin_LINE()) const; }; -LogMessage _log(const LogCategory *category, LogSeverity severity, +LogMessage _log(const LogCategory &category, LogSeverity severity, const char *fileName = __builtin_FILE(), unsigned int line = __builtin_LINE()); @@ -109,9 +109,9 @@ LogMessage _log(const LogCategory *category, LogSeverity severity, #define _LOG_CATEGORY(name) logCategory##name #define _LOG1(severity) \ - _log(nullptr, Log##severity).stream() + _log(LogCategory::defaultCategory(), Log##severity).stream() #define _LOG2(category, severity) \ - _log(&_LOG_CATEGORY(category)(), Log##severity).stream() + _log(_LOG_CATEGORY(category)(), Log##severity).stream() /* * Expand the LOG() macro to _LOG1() or _LOG2() based on the number of diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 6a040b592..ba57ad8f1 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -952,11 +952,11 @@ Loggable::~Loggable() * * \return A log message */ -LogMessage Loggable::_log(const LogCategory *category, LogSeverity severity, +LogMessage Loggable::_log(const LogCategory &category, LogSeverity severity, const char *fileName, unsigned int line) const { return LogMessage(fileName, line, - category ? *category : LogCategory::defaultCategory(), + category, severity, logPrefix()); } @@ -972,11 +972,11 @@ LogMessage Loggable::_log(const LogCategory *category, LogSeverity severity, * * \return A log message */ -LogMessage _log(const LogCategory *category, LogSeverity severity, +LogMessage _log(const LogCategory &category, LogSeverity severity, const char *fileName, unsigned int line) { return LogMessage(fileName, line, - category ? *category : LogCategory::defaultCategory(), + category, severity); } From patchwork Mon Mar 3 15:48:43 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: 22911 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 ADD74BD808 for ; Mon, 3 Mar 2025 15:48:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 07F4E68779; Mon, 3 Mar 2025 16:48:52 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="NsKDcTKB"; 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 8AE0A68779 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 19F4F1189 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=jKd6XXbCYcF3fkanfHSCd7vIcIVch53IiPtdnN/oBek=; h=From:To:Subject:Date:In-Reply-To:References:From; b=NsKDcTKBRSWdSHZatBupdZ/fppyBPAT/7WioFhPGgssjC+hyP9oPt9Sxo0/ebkJCe m/M5TOsHPC7CAwTUSjWzxW8NvIJdybL6zb7OBOmJ1q/+jyhKO4kUfy6L1laV48oxJx HBL3D7UFCo5QaIFKa+gQc26tGQea/QWfSHUXf1/U= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 2/3] treewide: Remove `libcamera::LOG(...)` occurences Date: Mon, 3 Mar 2025 16:48:43 +0100 Message-ID: <20250303154844.745574-3-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" When a class inherits from `Loggable`, it will have a protected `_log()` method and that will be used instead of the global `_log()` method in the expansion of the `LOG()` macro. However, if such a class has static member functions, then simply writing `LOG()` will not work because name lookup will find the non-static member function and no the global function, resulting in a compiler error because the non-static member cannot be invoked without an object, and there is no object in a static member function. This can be avoided by using `using libcamera::_log;`, thereby bringing the global declaration into the current scope. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart --- src/libcamera/sensor/camera_sensor_raw.cpp | 8 +++++--- src/libcamera/v4l2_device.cpp | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/libcamera/sensor/camera_sensor_raw.cpp b/src/libcamera/sensor/camera_sensor_raw.cpp index ab75b1f82..b6f16f58a 100644 --- a/src/libcamera/sensor/camera_sensor_raw.cpp +++ b/src/libcamera/sensor/camera_sensor_raw.cpp @@ -171,10 +171,12 @@ CameraSensorRaw::~CameraSensorRaw() = default; std::variant, int> CameraSensorRaw::match(MediaEntity *entity) { + using libcamera::_log; + /* Check the entity type. */ if (entity->type() != MediaEntity::Type::V4L2Subdevice || entity->function() != MEDIA_ENT_F_CAM_SENSOR) { - libcamera::LOG(CameraSensor, Debug) + LOG(CameraSensor, Debug) << entity->name() << ": unsupported entity type (" << utils::to_underlying(entity->type()) << ") or function (" << utils::hex(entity->function()) << ")"; @@ -199,7 +201,7 @@ CameraSensorRaw::match(MediaEntity *entity) break; default: - libcamera::LOG(CameraSensor, Debug) + LOG(CameraSensor, Debug) << entity->name() << ": unsupported pad " << pad->index() << " type " << utils::hex(pad->flags()); return { 0 }; @@ -207,7 +209,7 @@ CameraSensorRaw::match(MediaEntity *entity) } if (numSinks < 1 || numSinks > 2 || numSources != 1) { - libcamera::LOG(CameraSensor, Debug) + LOG(CameraSensor, Debug) << entity->name() << ": unsupported number of sinks (" << numSinks << ") or sources (" << numSources << ")"; return { 0 }; diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp index 2f65a43a0..8fda38949 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -989,6 +989,8 @@ template std::optional V4L2Device::toColorSpace(const struct v4l2_mb template int V4L2Device::fromColorSpace(const std::optional &colorSpace, T &v4l2Format) { + using libcamera::_log; + v4l2Format.colorspace = V4L2_COLORSPACE_DEFAULT; v4l2Format.xfer_func = V4L2_XFER_FUNC_DEFAULT; v4l2Format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; @@ -1017,7 +1019,7 @@ int V4L2Device::fromColorSpace(const std::optional &colorSpace, T &v if (itPrimaries != primariesToV4l2.end()) { v4l2Format.colorspace = itPrimaries->second; } else { - libcamera::LOG(V4L2, Warning) + LOG(V4L2, Warning) << "Unrecognised primaries in " << ColorSpace::toString(colorSpace); ret = -EINVAL; @@ -1027,7 +1029,7 @@ int V4L2Device::fromColorSpace(const std::optional &colorSpace, T &v if (itTransfer != transferFunctionToV4l2.end()) { v4l2Format.xfer_func = itTransfer->second; } else { - libcamera::LOG(V4L2, Warning) + LOG(V4L2, Warning) << "Unrecognised transfer function in " << ColorSpace::toString(colorSpace); ret = -EINVAL; @@ -1037,7 +1039,7 @@ int V4L2Device::fromColorSpace(const std::optional &colorSpace, T &v if (itYcbcrEncoding != ycbcrEncodingToV4l2.end()) { v4l2Format.ycbcr_enc = itYcbcrEncoding->second; } else { - libcamera::LOG(V4L2, Warning) + LOG(V4L2, Warning) << "Unrecognised YCbCr encoding in " << ColorSpace::toString(colorSpace); ret = -EINVAL; @@ -1047,7 +1049,7 @@ int V4L2Device::fromColorSpace(const std::optional &colorSpace, T &v if (itRange != rangeToV4l2.end()) { v4l2Format.quantization = itRange->second; } else { - libcamera::LOG(V4L2, Warning) + LOG(V4L2, Warning) << "Unrecognised quantization in " << ColorSpace::toString(colorSpace); ret = -EINVAL; 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