[v4,06/15] config: Look up log file in configuration file
diff mbox series

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

Commit Message

Milan Zamazal Sept. 25, 2024, 9:58 a.m. UTC
This requires change in LIBCAMERA_LOG_FILE interpretation so that we can
disable the log file in isolated processes.  If LIBCAMERA_LOG_FILE is
defined but empty, it means there is no log file, even if defined in the
configuration file.  This should cause no compatibility trouble since
opening a file with an empty name makes little sense.

The configuration snippet:

  configuration:
    log:
      file: FILENAME

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 src/libcamera/base/global_configuration.cpp |  1 +
 src/libcamera/base/log.cpp                  | 13 +++++++++----
 src/libcamera/process.cpp                   | 11 ++++++++---
 3 files changed, 18 insertions(+), 7 deletions(-)

Patch
diff mbox series

diff --git a/src/libcamera/base/global_configuration.cpp b/src/libcamera/base/global_configuration.cpp
index b202cef9b..b64bf28e2 100644
--- a/src/libcamera/base/global_configuration.cpp
+++ b/src/libcamera/base/global_configuration.cpp
@@ -34,6 +34,7 @@  LOG_DEFINE_CATEGORY(Configuration)
  * \code{.yaml}
  * configuration:
  *   log:
+ *     file: syslog
  *     levels: 'IPAManager:DEBUG'
  * \endcode
  *
diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp
index 891b4ae9d..07352f6c1 100644
--- a/src/libcamera/base/log.cpp
+++ b/src/libcamera/base/log.cpp
@@ -607,16 +607,21 @@  Logger::Logger()
  */
 void Logger::parseLogFile()
 {
-	const char *file = utils::secure_getenv("LIBCAMERA_LOG_FILE");
-	if (!file)
+	std::optional<std::string> file =
+		GlobalConfiguration::envOption("LIBCAMERA_LOG_FILE", "log.file");
+	if (!file.has_value())
 		return;
 
-	if (!strcmp(file, "syslog")) {
+	auto fileValue = file.value();
+	if (fileValue == "")
+		return;
+
+	if (fileValue == "syslog") {
 		logSetTarget(LoggingTargetSyslog);
 		return;
 	}
 
-	logSetFile(file, false);
+	logSetFile(fileValue.c_str(), false);
 }
 
 /**
diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp
index bc9833f41..c034044cf 100644
--- a/src/libcamera/process.cpp
+++ b/src/libcamera/process.cpp
@@ -11,7 +11,9 @@ 
 #include <dirent.h>
 #include <fcntl.h>
 #include <list>
+#include <optional>
 #include <signal.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
 #include <sys/types.h>
@@ -23,6 +25,8 @@ 
 #include <libcamera/base/log.h>
 #include <libcamera/base/utils.h>
 
+#include "libcamera/internal/global_configuration.h"
+
 /**
  * \file process.h
  * \brief Process object
@@ -261,9 +265,10 @@  int Process::start(const std::string &path,
 
 		closeAllFdsExcept(fds);
 
-		const char *file = utils::secure_getenv("LIBCAMERA_LOG_FILE");
-		if (file && strcmp(file, "syslog"))
-			unsetenv("LIBCAMERA_LOG_FILE");
+		std::optional<std::string> file =
+			GlobalConfiguration::envOption("LIBCAMERA_LOG_FILE", "log.file");
+		if (file.has_value() && file.value() != "syslog")
+			setenv("LIBCAMERA_LOG_FILE", "", 1);
 
 		const char **argv = new const char *[args.size() + 2];
 		unsigned int len = args.size();