@@ -8,6 +8,7 @@
#pragma once
#include <filesystem>
+#include <optional>
#include <vector>
#include "libcamera/internal/yaml_parser.h"
@@ -21,6 +22,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 <filesystem>
#include <memory>
#include <stdint.h>
+#include <string>
#include <sys/types.h>
#include <libcamera/base/file.h>
@@ -41,8 +42,10 @@ LOG_DEFINE_CATEGORY(Configuration)
* 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 usingGlobalConfiguration::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.
*/
/**
@@ -141,6 +144,49 @@ 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<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 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<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, ',');
They make accessing simple configuration values simpler. 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 | 50 ++++++++++++++++++- src/libcamera/base/log.cpp | 13 ++--- 3 files changed, 57 insertions(+), 10 deletions(-)