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
+13 -5
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();