From c63f2c72cd7b278584dcc70b8294063e89b01ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Thu, 18 Dec 2025 18:42:11 +0100 Subject: [PATCH] libcamera: base: log: Add `LIBCAMERA_LOG_COLOR` env var MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the `LIBCAMERA_NO_LOG_COLOR` env variable with another environment variable that recognizes the "auto", "yes", "no" values. When set to "auto", the messages are only colored if the standard error is a tty (as determined by `isatty()`). "auto" is the default value. This ensures that the ansi escape codes won't litter the output if the stderr is redirected to a file, `tee`, etc. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- Documentation/runtime_configuration.rst | 12 +++++++----- src/libcamera/base/log.cpp | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Documentation/runtime_configuration.rst b/Documentation/runtime_configuration.rst index ce81ff25..e99ef2fb 100644 --- a/Documentation/runtime_configuration.rst +++ b/Documentation/runtime_configuration.rst @@ -98,8 +98,8 @@ LIBCAMERA_LOG_LEVELS Example value: ``*:DEBUG`` -LIBCAMERA_LOG_NO_COLOR - Disable coloring of log messages (`more `__). +LIBCAMERA_LOG_COLOR + Control the coloring of log messages (`more `__). LIBCAMERA_IPA_CONFIG_PATH, ipa.config_paths Define custom search locations for IPA configurations (`more `__). @@ -174,12 +174,14 @@ Notes about debugging ~~~~~~~~~~~~~~~~~~~~~ The environment variables ``LIBCAMERA_LOG_FILE``, ``LIBCAMERA_LOG_LEVELS`` and -``LIBCAMERA_LOG_NO_COLOR`` are used to modify the default configuration of the +``LIBCAMERA_LOG_COLOR`` are used to modify the default configuration of the libcamera logger. By default, libcamera logs all messages to the standard error (std::cerr). -Messages are colored by default depending on the log level. Coloring can be -disabled by setting the ``LIBCAMERA_LOG_NO_COLOR`` environment variable. +The ``LIBCAMERA_LOG_COLOR`` environment variable can be used to control whether +the messages will be colored or not. The possible values are: ``auto``, ``yes``, +and ``no``. The default value is ``auto``, which enables coloring if the standard +error is connected to a TTY. The default log destination can also be directed to a file by setting the ``LIBCAMERA_LOG_FILE`` environment variable to the log file name. This also diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 81b550e2..6e6d2887 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -582,14 +583,21 @@ void Logger::logSetLevel(const char *category, const char *level) /** * \brief Construct a logger * - * If the environment variable is not set, log to std::cerr. The log messages - * are then colored by default. This can be overridden by setting the - * LIBCAMERA_LOG_NO_COLOR environment variable to disable coloring. + * If the environment variable is not set, log to std::cerr. The coloring + * of log messages can be controlled using the LIBCAMERA_LOG_COLOR environment + * variable. */ Logger::Logger() { - bool color = !utils::secure_getenv("LIBCAMERA_LOG_NO_COLOR"); - logSetStream(&std::cerr, color); + const char *color = utils::secure_getenv("LIBCAMERA_LOG_COLOR"); + bool wantColor = false; + + if (!color || strcmp(color, "auto") == 0) + wantColor = isatty(STDERR_FILENO) == 1; + else if (strcmp(color, "yes") == 0) + wantColor = true; + + logSetStream(&std::cerr, wantColor); parseLogFile(); parseLogLevels();