@@ -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;
@@ -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.
*
@@ -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, ',');
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(-)