From patchwork Tue Mar 26 11:24:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 19805 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 83957BD160 for ; Tue, 26 Mar 2024 11:26:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 35AD063334; Tue, 26 Mar 2024 12:26:23 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="KTivKatl"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D4D606331B for ; Tue, 26 Mar 2024 12:26:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711452379; 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=NGQUOluWVd3l2g+6aByZJMX68em3b48gIEq3iXmWcts=; b=KTivKatlF6Qp23Unugv4KYTWH7sRCirWGdY2VPpjY8povV97ZiY8gi9XV5OIxFXJVCBoe2 /cUdIuXzgrt2k5my4zm6qOwzhHZrmBdj7AGxcjjPH1EeLNPSLHSQm/Z2sGhE2p7ECmg1Hf ZWTTLZwZSplx3+9AvXn/98ZX0nJ4I4E= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-450-a4MzkkZcNGSRqN5bA13ezA-1; Tue, 26 Mar 2024 07:26:17 -0400 X-MC-Unique: a4MzkkZcNGSRqN5bA13ezA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id ADA7784B162 for ; Tue, 26 Mar 2024 11:26:17 +0000 (UTC) Received: from nuthatch.brq.redhat.com (unknown [10.43.17.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 357CC3C22; Tue, 26 Mar 2024 11:26:17 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal Subject: [RFC PATCH 04/11] config: Add configuration retrieval helpers Date: Tue, 26 Mar 2024 12:24:07 +0100 Message-ID: <20240326112419.503286-5-mzamazal@redhat.com> In-Reply-To: <20240326112419.503286-1-mzamazal@redhat.com> References: <20240326112419.503286-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 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" Due to the restrictions of YamlObject class, ugly type casting dance is needed. Signed-off-by: Milan Zamazal --- .../libcamera/internal/global_configuration.h | 4 +++ src/libcamera/base/global_configuration.cpp | 28 +++++++++++++++++++ src/libcamera/base/log.cpp | 13 ++++----- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/include/libcamera/internal/global_configuration.h b/include/libcamera/internal/global_configuration.h index 0a4f3c02..41ac57cf 100644 --- a/include/libcamera/internal/global_configuration.h +++ b/include/libcamera/internal/global_configuration.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include "libcamera/internal/yaml_parser.h" @@ -27,6 +28,9 @@ public: static unsigned int version(); static Configuration configuration(); + static std::optional option(const char *const confPath); + static std::optional envOption(const char *const envVariable, + const char *const confPath); private: static const std::vector globalConfigurationFiles; diff --git a/src/libcamera/base/global_configuration.cpp b/src/libcamera/base/global_configuration.cpp index e9c44057..b29f452b 100644 --- a/src/libcamera/base/global_configuration.cpp +++ b/src/libcamera/base/global_configuration.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -118,6 +119,33 @@ GlobalConfiguration::Configuration GlobalConfiguration::get() return (*instance().configuration_); } +/** + * \brief Retrieve string configuration identified by the given string + * ... + */ +std::optional GlobalConfiguration::option( + const char *const confPath) +{ + YamlObject *c = &const_cast(configuration()); + for (auto part : utils::details::StringSplitter(confPath, ".")) + c = &const_cast((*c)[part]); + return c->get(); +} + +/** + * \brief Retrieve string configuration from the given environment variable or configuration + * ... + */ +std::optional GlobalConfiguration::envOption( + const char *const envVariable, + const char *const 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 0564306d..7e58e4f9 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, ',');