libcamera: base: log: Add LIBCAMERA_LOG_COLOR env var

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 <barnabas.pocze@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Barnabás Pőcze
2025-12-18 18:42:11 +01:00
parent 049bacc267
commit c63f2c72cd
2 changed files with 20 additions and 10 deletions

View File

@@ -98,8 +98,8 @@ LIBCAMERA_LOG_LEVELS
Example value: ``*:DEBUG``
LIBCAMERA_LOG_NO_COLOR
Disable coloring of log messages (`more <Notes about debugging_>`__).
LIBCAMERA_LOG_COLOR
Control the coloring of log messages (`more <Notes about debugging_>`__).
LIBCAMERA_IPA_CONFIG_PATH, ipa.config_paths
Define custom search locations for IPA configurations (`more <IPA configuration_>`__).
@@ -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

View File

@@ -19,6 +19,7 @@
#include <string_view>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <unordered_set>
#include <libcamera/logging.h>
@@ -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();