[RFC,04/11] config: Add configuration retrieval helpers
diff mbox series

Message ID 20240326112419.503286-5-mzamazal@redhat.com
State Superseded
Headers show
Series
  • Add global configuration file
Related show

Commit Message

Milan Zamazal March 26, 2024, 11:24 a.m. UTC
Due to the restrictions of YamlObject class, ugly type casting dance is needed.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 .../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(-)

Patch
diff mbox series

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 <filesystem>
+#include <optional>
 #include <vector>
 
 #include "libcamera/internal/yaml_parser.h"
@@ -27,6 +28,9 @@  public:
 
 	static unsigned int version();
 	static Configuration configuration();
+	static std::optional<std::string> option(const char *const confPath);
+	static std::optional<std::string> envOption(const char *const envVariable,
+						    const char *const confPath);
 
 private:
 	static const std::vector<std::filesystem::path> 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 <cstdint>
 #include <filesystem>
 #include <memory>
+#include <string>
 #include <sys/types.h>
 
 #include <libcamera/base/file.h>
@@ -118,6 +119,33 @@  GlobalConfiguration::Configuration GlobalConfiguration::get()
 	return (*instance().configuration_);
 }
 
+/**
+ * \brief Retrieve string configuration identified by the given string
+ * ...
+ */
+std::optional<std::string> GlobalConfiguration::option(
+	const char *const confPath)
+{
+	YamlObject *c = &const_cast<YamlObject &>(configuration());
+	for (auto part : utils::details::StringSplitter(confPath, "."))
+		c = &const_cast<YamlObject &>((*c)[part]);
+	return c->get<std::string>();
+}
+
+/**
+ * \brief Retrieve string configuration from the given environment variable or configuration
+ * ...
+ */
+std::optional<std::string> 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<std::string> confDebug =
-			GlobalConfiguration::configuration()["log"]["levels"].get<std::string>();
-		if (!confDebug.has_value())
-			return;
-		debug = confDebug.value().c_str();
-	}
+	const std::optional<std::string> 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, ',');