[RFC,06/11] config: Look up log color configuration in configuration file
diff mbox series

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

Commit Message

Milan Zamazal March 26, 2024, 11:24 a.m. UTC
The configuration snippet:

  configuration:
    log:
      color: BOOL

In order to use the global configuration access helper for a boolean value, we
must make a template from it.  To make the template visible, we must implement
it in the header file, otherwise undefined reference error would be reported
when linking.

We don't implement envOption helper for boolean here, because we would have to
deal with a specific value interpretation in this particular case.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 .../libcamera/internal/global_configuration.h   | 17 ++++++++++++++++-
 src/libcamera/base/global_configuration.cpp     | 17 +----------------
 src/libcamera/base/log.cpp                      |  2 ++
 3 files changed, 19 insertions(+), 17 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/global_configuration.h b/include/libcamera/internal/global_configuration.h
index 41ac57cf..67684762 100644
--- a/include/libcamera/internal/global_configuration.h
+++ b/include/libcamera/internal/global_configuration.h
@@ -13,6 +13,8 @@ 
 
 #include "libcamera/internal/yaml_parser.h"
 
+#include "libcamera/base/utils.h"
+
 namespace libcamera {
 
 class GlobalConfiguration
@@ -28,7 +30,20 @@  public:
 
 	static unsigned int version();
 	static Configuration configuration();
-	static std::optional<std::string> option(const char *const confPath);
+
+	/**
+	 * \brief Retrieve string configuration identified by the given string
+	 * ...
+	 */
+	template<typename T>
+	static std::optional<T> 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<T>();
+	}
+
 	static std::optional<std::string> envOption(const char *const envVariable,
 						    const char *const confPath);
 
diff --git a/src/libcamera/base/global_configuration.cpp b/src/libcamera/base/global_configuration.cpp
index b29f452b..1e7a4caa 100644
--- a/src/libcamera/base/global_configuration.cpp
+++ b/src/libcamera/base/global_configuration.cpp
@@ -18,8 +18,6 @@ 
 
 #include "libcamera/internal/yaml_parser.h"
 
-#include "libcamera/base/utils.h"
-
 namespace libcamera {
 
 LOG_DEFINE_CATEGORY(Configuration)
@@ -119,19 +117,6 @@  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
  * ...
@@ -143,7 +128,7 @@  std::optional<std::string> GlobalConfiguration::envOption(
 	const char *envValue = utils::secure_getenv(envVariable);
 	if (envValue)
 		return std::optional{ std::string{ envValue } };
-	return option(confPath);
+	return option<std::string>(confPath);
 }
 
 /**
diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp
index a373d38b..19403378 100644
--- a/src/libcamera/base/log.cpp
+++ b/src/libcamera/base/log.cpp
@@ -590,6 +590,8 @@  void Logger::logSetLevel(const char *category, const char *level)
 Logger::Logger()
 {
 	bool color = !utils::secure_getenv("LIBCAMERA_LOG_NO_COLOR");
+	if (color)
+		color = GlobalConfiguration::option<bool>("log.color").value_or(true);
 	logSetStream(&std::cerr, color);
 
 	parseLogFile();