@@ -29,6 +29,13 @@ LOG_DEFINE_CATEGORY(Configuration)
* The configuration file is a YAML file and the configuration itself is stored
* under `configuration' top-level item.
*
+ * Example configuration file content:
+ * \code{.yaml}
+ * configuration:
+ * log:
+ * levels: 'IPAManager:DEBUG'
+ * \endcode
+ *
* The configuration file is looked up in user's home directory first and if it
* is not found then in system-wide configuration directories. If multiple
* configuration files exist then only the first one found is used and no
@@ -57,6 +64,11 @@ const std::vector<std::filesystem::path>
std::filesystem::path("/etc/libcamera/configuration.yaml"),
};
+/*
+ * Care is needed here to not log anything before the configuration is
+ * loaded otherwise the logger would be initialized with empty configuration.
+ */
+
void GlobalConfiguration::load()
{
std::filesystem::path userConfigurationDirectory;
@@ -87,10 +99,15 @@ void GlobalConfiguration::load()
bool GlobalConfiguration::loadFile(const std::filesystem::path &fileName)
{
+ /*
+ * initialized_ must be set to true before any logging is called!
+ */
+
File file(fileName);
if (!file.exists()) {
return false;
}
+ initialized_ = true;
if (!file.open(File::OpenModeFlag::ReadOnly)) {
LOG(Configuration, Warning)
@@ -105,7 +122,6 @@ bool GlobalConfiguration::loadFile(const std::filesystem::path &fileName)
return true;
}
configuration_ = std::move(root);
- initialized_ = true;
LOG(Configuration, Info) << "Configuration file " << fileName << "loaded";
return true;
@@ -14,6 +14,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <string>
#include <syslog.h>
#include <time.h>
#include <unordered_set>
@@ -25,6 +26,8 @@
#include <libcamera/base/thread.h>
#include <libcamera/base/utils.h>
+#include "libcamera/internal/global_configuration.h"
+
/**
* \file base/log.h
* \brief Logging infrastructure
@@ -626,8 +629,13 @@ void Logger::parseLogFile()
void Logger::parseLogLevels()
{
const char *debug = utils::secure_getenv("LIBCAMERA_LOG_LEVELS");
- if (!debug)
- return;
+ 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();
+ }
for (const char *pair = debug; *debug != '\0'; pair = debug) {
const char *comma = strchrnul(debug, ',');
@@ -15,6 +15,7 @@
#include "libcamera/internal/camera.h"
#include "libcamera/internal/device_enumerator.h"
+#include "libcamera/internal/global_configuration.h"
#include "libcamera/internal/ipa_manager.h"
#include "libcamera/internal/pipeline_handler.h"
@@ -41,6 +42,15 @@ CameraManager::Private::Private()
: initialized_(false)
{
ipaManager_ = std::make_unique<IPAManager>();
+
+ /*
+ * This is needed to initialize global configuration
+ * before any logging is called. Actually, IPAManager
+ * constructor is called before this, so the configuration
+ * call is superfluous here, but the initialization belongs
+ * here.
+ */
+ GlobalConfiguration::configuration();
}
int CameraManager::Private::start()
@@ -16,6 +16,7 @@
#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>
+#include "libcamera/internal/global_configuration.h"
#include "libcamera/internal/ipa_module.h"
#include "libcamera/internal/ipa_proxy.h"
#include "libcamera/internal/pipeline_handler.h"
@@ -103,6 +104,17 @@ LOG_DEFINE_CATEGORY(IPAManager)
*/
IPAManager::IPAManager()
{
+ /*
+ * Make sure configuration is loaded early.
+ * Otherwise there would be problems with mutual
+ * configuration-logging dependencies.
+ *
+ * This is needed to be here in addition to CameraManager constructor
+ * because logging is called before CameraManager constructor body is
+ * executed.
+ */
+ GlobalConfiguration::configuration();
+
#if HAVE_IPA_PUBKEY
if (!pubKey_.isValid())
LOG(IPAManager, Warning) << "Public key not valid";
In addition to LIBCAMERA_LOG_LEVELS environment variable, expose the same configuration in the configuration file. Because logging asks for configuration and configuration loading uses logging, extra care is needed. We must prevent calling any log function before configuration is loaded. Otherwise the log function would ask for configuration, it would load the configuration files and would try to log the results, which would cause mutex deadlock due to calling logging inside logging. Not logging in configuration is not an option, at least without introducing complicated mechanism such as delayed logging, because we need to log at least errors when reading the configuration. We must also prevent logging in configuration loading before the configuration is loaded and available (or there is an error in loading the configuration). Otherwise logging would see an empty configuration and wouldn't be configured as asked. Preventing logging before configuration is loaded is ensured by asking for configuration in CameraManager constructor and in IPAManager constructor (which is mysteriously loaded from CameraManager constructor before its body is executed). This seems to be sufficient at least for software ISP. The configuration snippet for logging looks as follows: configuration: log: levels: LIBCAMERA_LOG_LEVELS Signed-off-by: Milan Zamazal <mzamazal@redhat.com> --- src/libcamera/base/global_configuration.cpp | 18 +++++++++++++++++- src/libcamera/base/log.cpp | 12 ++++++++++-- src/libcamera/camera_manager.cpp | 10 ++++++++++ src/libcamera/ipa_manager.cpp | 12 ++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-)