From patchwork Tue Oct 1 10:27:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 21452 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 BCF0BBD80A for ; Tue, 1 Oct 2024 10:28:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A46AC63533; Tue, 1 Oct 2024 12:28:39 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="Hklvi6qF"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C91C863530 for ; Tue, 1 Oct 2024 12:28:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727778510; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jUjWJyW+HM8KenmiFKalKbIfn6FwfQTP4TUMvWIDauQ=; b=Hklvi6qFPJGjqpA7Jp5wL/SBjzpPr0Z3P4nay5LykcGbaJnBwY2NmoEAUCfV/xlEgUH/Wt 2fVQFdecoxkxIZ95paulXDEI5UrSfnjm6Cv+QgLjEuxMZ8tJAaJ9w4X6b6le273Doz3pHL D1XE/T7CUE/KW5xj9ZcIFS6Ud6rfYk4= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-457-05vfZo2nPNO1ysIh4so7rQ-1; Tue, 01 Oct 2024 06:28:27 -0400 X-MC-Unique: 05vfZo2nPNO1ysIh4so7rQ-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 917351955DD7; Tue, 1 Oct 2024 10:28:26 +0000 (UTC) Received: from nuthatch.brq.redhat.com (unknown [10.43.17.37]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 53E1F1956054; Tue, 1 Oct 2024 10:28:25 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , Kieran Bingham , Naushir Patuck Subject: [PATCH v5 05/15] config: Add configuration retrieval helpers Date: Tue, 1 Oct 2024 12:27:57 +0200 Message-ID: <20241001102810.479285-6-mzamazal@redhat.com> In-Reply-To: <20241001102810.479285-1-mzamazal@redhat.com> References: <20241001102810.479285-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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" They make accessing simple configuration values simpler. Due to the restrictions of YamlObject class, ugly type casting dance is needed. Also, GlobalConfiguration::option must check for the key presence before retrieving the value using operator[] because if the key is not present then YamlObject::empty may be returned as a regular value. Signed-off-by: Milan Zamazal --- .../libcamera/internal/global_configuration.h | 5 ++ src/libcamera/base/global_configuration.cpp | 53 ++++++++++++++++++- src/libcamera/base/log.cpp | 13 ++--- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/include/libcamera/internal/global_configuration.h b/include/libcamera/internal/global_configuration.h index 8058fd8e2..b1529f392 100644 --- a/include/libcamera/internal/global_configuration.h +++ b/include/libcamera/internal/global_configuration.h @@ -8,6 +8,8 @@ #pragma once #include +#include +#include #include #include "libcamera/internal/yaml_parser.h" @@ -25,6 +27,9 @@ public: static unsigned int version(); static Configuration configuration(); + static std::optional option(const std::string &confPath); + static std::optional envOption(const char *const envVariable, + const std::string &confPath); private: static const std::vector globalConfigurationFiles; diff --git a/src/libcamera/base/global_configuration.cpp b/src/libcamera/base/global_configuration.cpp index c6f4ebd7b..f184d4bab 100644 --- a/src/libcamera/base/global_configuration.cpp +++ b/src/libcamera/base/global_configuration.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -60,8 +61,10 @@ void GlobalConfiguration::initialize() * configuration files exist then only the first one found is used and no * configuration merging is performed. * - * The class is used as a private singleton and the configuration can be - * accessed using GlobalConfiguration::configuration(). + * The class is used as a private singleton accessed by the provided + * helpers. Namely GlobalConfiguration::option or GlobalConfiguration::envOption + * to access individual options or GlobalConfiguration::configuration() to + * access the whole configuration. */ /** @@ -145,6 +148,52 @@ GlobalConfiguration::Configuration GlobalConfiguration::get() return *instance_->configuration_; } +/** + * \brief Return value of the configuration option identified by \a confPath + * \param[in] confPath Sequence of the YAML section names (excluding + * `configuration') leading to the requested option separated by dots + * \return A value if an item corresponding to \a confPath exists in the + * configuration file, no value otherwise + */ +std::optional GlobalConfiguration::option( + const std::string &confPath) +{ + YamlObject *c = &const_cast(configuration()); + for (auto part : utils::details::StringSplitter(confPath, ".")) + if (c->contains(part)) + c = &const_cast((*c)[part]); + else + return std::optional(); + return c->get(); +} + +/** + * \brief Return value of the configuration option from a file or environment + * \param[in] envVariable Environment variable to get the value from + * \param[in] confPath The same as in GlobalConfiguration::option + * + * This helper looks first at the given environment variable and if it is + * defined then it returns its value (even if it is empty). Otherwise it looks + * for \a confPath the same way as in GlobalConfiguration::option. Only string + * values are supported. + * + * \note Support for using environment variables to configure libcamera behavior + * is provided here mostly for backward compatibility reasons. Introducing new + * configuration environment variables is discouraged. + * + * \return A value retrieved from the given environment option or configuration + * file or no value if not found + */ +std::optional GlobalConfiguration::envOption( + const char *const envVariable, + const std::string &confPath) +{ + const char *envValue = utils::secure_getenv(envVariable); + if (envValue) + return std::optional{ std::string{ envValue } }; + return option(confPath); +} + /** * \brief Return configuration version * diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 0f6767107..891b4ae9d 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -628,14 +628,11 @@ void Logger::parseLogFile() */ void Logger::parseLogLevels() { - const char *debug = utils::secure_getenv("LIBCAMERA_LOG_LEVELS"); - if (!debug) { - const std::optional confDebug = - GlobalConfiguration::configuration()["log"]["levels"].get(); - if (!confDebug.has_value()) - return; - debug = confDebug.value().c_str(); - } + const std::optional confDebug = + GlobalConfiguration::envOption("LIBCAMERA_LOG_LEVELS", "log.levels"); + if (!confDebug.has_value()) + return; + const char *debug = confDebug.value().c_str(); for (const char *pair = debug; *debug != '\0'; pair = debug) { const char *comma = strchrnul(debug, ',');