Patch Detail
Show a patch.
GET /api/patches/16047/?format=api
{ "id": 16047, "url": "https://patchwork.libcamera.org/api/patches/16047/?format=api", "web_url": "https://patchwork.libcamera.org/patch/16047/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20220525222503.6460-5-laurent.pinchart@ideasonboard.com>", "date": "2022-05-25T22:25:02", "name": "[libcamera-devel,4/5] libcamera: base: log: Add coloring to the log output", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "5187572de62e939ff0e1b32f0ac616fdc570710f", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/16047/mbox/", "series": [ { "id": 3140, "url": "https://patchwork.libcamera.org/api/series/3140/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3140", "date": "2022-05-25T22:24:58", "name": "libcamera: Add colors to the log", "version": 1, "mbox": "https://patchwork.libcamera.org/series/3140/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/16047/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/16047/checks/", "tags": {}, "headers": { "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>", "X-Original-To": "parsemail@patchwork.libcamera.org", "Delivered-To": "parsemail@patchwork.libcamera.org", "Received": [ "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 47669C326E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 25 May 2022 22:25:19 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A85BB6566E;\n\tThu, 26 May 2022 00:25:18 +0200 (CEST)", "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E2A2165662\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 26 May 2022 00:25:13 +0200 (CEST)", "from pendragon.ideasonboard.com (ip-109-40-242-63.web.vodafone.de\n\t[109.40.242.63])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 36B2312F3;\n\tThu, 26 May 2022 00:25:13 +0200 (CEST)" ], "DKIM-Signature": [ "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1653517518;\n\tbh=i8rER45dnLUM13zU+IdBjK7JbbAwNIWJ0aoOU0RlxAk=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=3R7u6JgRVRJy6VqJSRCI73qiCRkrBIgNuVZ0pEbigk8hQfFEX/4O0oybBKjuhDFbH\n\t6PA2Ge/vybUjdp6pEMC7/+bTcrJd9miB9UBM9uOwjuTImFUdKoP6baeFU8qYE8KEry\n\tSlr9hJYzp0lNcFCVErnd5sr/GmrIcqqym2xpN0Gma5WR1f0QMBFXC4ffSJY7rC7Axf\n\tZs8LYZicoB11GBsukTSMqzLZ5R9I3ABqKizViM9CqE1Mp6qikaji0nbOZEEVHwy8+z\n\tuZQNyfJPymGwTHDiPSwvKZjSXVLlZfoFDdXlOuwRjYHQ00cSgFtV3nHuXW698y6BT2\n\tviFklJi7FQylg==", "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1653517513;\n\tbh=i8rER45dnLUM13zU+IdBjK7JbbAwNIWJ0aoOU0RlxAk=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=Z8hQXS47Dzip7S7fLrLNdFe/oyL394T2n41nM2SNGimAVmzxxRvebnJ2UvDeKkb+N\n\t6z31CI+cguZDhl4jV+SSFXFpEansu1zLJSR7HclkFCF4HkuzWi8aDI9Ts3Zaddtxik\n\toF1zrBt5qs7JNBQZ8O8Je8Of9uC/GDb8srTcxqAg=" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Z8hQXS47\"; dkim-atps=neutral", "To": "libcamera-devel@lists.libcamera.org", "Date": "Thu, 26 May 2022 01:25:02 +0300", "Message-Id": "<20220525222503.6460-5-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.35.1", "In-Reply-To": "<20220525222503.6460-1-laurent.pinchart@ideasonboard.com>", "References": "<20220525222503.6460-1-laurent.pinchart@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH 4/5] libcamera: base: log: Add coloring to\n\tthe log output", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.29", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "From": "Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>", "Reply-To": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "Extend the logger to support coloring messages. The log level is\ncolorized with per-level colors, and the category with a fixed color.\nThis makes the log output more readable.\n\nColoring is enabled by default when logging to std::cerr, and can be\ndisabled by setting the LIBCAMERA_LOG_NO_COLOR environment variable.\nWhen logging to a file with LIBCAMERA_LOG_FILE, coloring is disabled. It\ncan be enabled for file logging using the logSetFile() function.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n Documentation/environment_variables.rst | 21 +++--\n include/libcamera/logging.h | 4 +-\n src/libcamera/base/log.cpp | 114 +++++++++++++++++++-----\n src/libcamera/camera_manager.cpp | 2 +-\n 4 files changed, 109 insertions(+), 32 deletions(-)", "diff": "diff --git a/Documentation/environment_variables.rst b/Documentation/environment_variables.rst\nindex fa703a726845..7028c024b14e 100644\n--- a/Documentation/environment_variables.rst\n+++ b/Documentation/environment_variables.rst\n@@ -19,6 +19,9 @@ LIBCAMERA_LOG_LEVELS\n \n Example value: ``*:DEBUG``\n \n+LIBCAMERA_LOG_NO_COLOR\n+ Disable coloring of log messages (`more <Notes about debugging_>`__).\n+\n LIBCAMERA_IPA_CONFIG_PATH\n Define custom search locations for IPA configurations (`more <IPA configuration_>`__).\n \n@@ -40,12 +43,20 @@ Further details\n Notes about debugging\n ~~~~~~~~~~~~~~~~~~~~~\n \n-The environment variables ``LIBCAMERA_LOG_FILE`` and ``LIBCAMERA_LOG_LEVELS``\n-are used to modify the destination and verbosity of messages provided by\n-libcamera.\n+The environment variables ``LIBCAMERA_LOG_FILE``, ``LIBCAMERA_LOG_LEVELS`` and\n+``LIBCAMERA_LOG_NO_COLOR`` are used to modify the degault configuration of the\n+libcamera logger.\n \n-The ``LIBCAMERA_LOG_LEVELS`` variable accepts a comma-separated list of\n-'category:level' pairs.\n+By default, libcamera logs all messages to the standard error (std::cerr).\n+Messages are colored by default depending on the log level. Coloring can be\n+disabled by setting the ``LIBCAMERA_LOG_NO_COLOR`` environment variable.\n+\n+The default log destination can also be directed to a file by setting the\n+``LIBCAMERA_LOG_FILE`` environment variable to the log file name. This also\n+disables coloring.\n+\n+Log levels are controlled through the ``LIBCAMERA_LOG_LEVELS`` variable, which\n+accepts a comma-separated list of 'category:level' pairs.\n \n The `level <Log levels_>`__ part is mandatory and can either be specified by\n name or by numerical index associated with each level.\ndiff --git a/include/libcamera/logging.h b/include/libcamera/logging.h\nindex c36882b91974..cd842f67d553 100644\n--- a/include/libcamera/logging.h\n+++ b/include/libcamera/logging.h\n@@ -16,8 +16,8 @@ enum LoggingTarget {\n \tLoggingTargetStream,\n };\n \n-int logSetFile(const char *path);\n-int logSetStream(std::ostream *stream);\n+int logSetFile(const char *path, bool color = false);\n+int logSetStream(std::ostream *stream, bool color = false);\n int logSetTarget(LoggingTarget target);\n void logSetLevel(const char *category, const char *level);\n \ndiff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\nindex 26f1420703b9..a9f5bbbd36f7 100644\n--- a/src/libcamera/base/log.cpp\n+++ b/src/libcamera/base/log.cpp\n@@ -104,8 +104,8 @@ static const char *log_severity_name(LogSeverity severity)\n class LogOutput\n {\n public:\n-\tLogOutput(const char *path);\n-\tLogOutput(std::ostream *stream);\n+\tLogOutput(const char *path, bool color);\n+\tLogOutput(std::ostream *stream, bool color);\n \tLogOutput();\n \t~LogOutput();\n \n@@ -119,14 +119,16 @@ private:\n \n \tstd::ostream *stream_;\n \tLoggingTarget target_;\n+\tbool color_;\n };\n \n /**\n * \\brief Construct a log output based on a file\n * \\param[in] path Full path to log file\n+ * \\param[in] color True to output colored messages\n */\n-LogOutput::LogOutput(const char *path)\n-\t: target_(LoggingTargetFile)\n+LogOutput::LogOutput(const char *path, bool color)\n+\t: target_(LoggingTargetFile), color_(color)\n {\n \tstream_ = new std::ofstream(path);\n }\n@@ -134,9 +136,10 @@ LogOutput::LogOutput(const char *path)\n /**\n * \\brief Construct a log output based on a stream\n * \\param[in] stream Stream to send log output to\n+ * \\param[in] color True to output colored messages\n */\n-LogOutput::LogOutput(std::ostream *stream)\n-\t: stream_(stream), target_(LoggingTargetStream)\n+LogOutput::LogOutput(std::ostream *stream, bool color)\n+\t: stream_(stream), target_(LoggingTargetStream), color_(color)\n {\n }\n \n@@ -144,7 +147,7 @@ LogOutput::LogOutput(std::ostream *stream)\n * \\brief Construct a log output to syslog\n */\n LogOutput::LogOutput()\n-\t: stream_(nullptr), target_(LoggingTargetSyslog)\n+\t: stream_(nullptr), target_(LoggingTargetSyslog), color_(false)\n {\n \topenlog(\"libcamera\", LOG_PID, 0);\n }\n@@ -179,28 +182,70 @@ bool LogOutput::isValid() const\n \t}\n }\n \n+namespace {\n+\n+constexpr const char *kColorReset = \"\\033[0m\";\n+constexpr const char *kColorRed = \"\\033[31m\";\n+constexpr const char *kColorGreen = \"\\033[32m\";\n+constexpr const char *kColorYellow = \"\\033[33m\";\n+constexpr const char *kColorBlue = \"\\033[34m\";\n+constexpr const char *kColorMagenta = \"\\033[35m\";\n+constexpr const char *kColorCyan = \"\\033[36m\";\n+constexpr const char *kColorWhite = \"\\033[37m\";\n+constexpr const char *kColorGrey = \"\\033[90m\";\n+constexpr const char *kColorBrightRed = \"\\033[91m\";\n+constexpr const char *kColorBrightGreen = \"\\033[92m\";\n+constexpr const char *kColorBrightYellow = \"\\033[93m\";\n+constexpr const char *kColorBrightBlue = \"\\033[94m\";\n+constexpr const char *kColorBrightMagenta = \"\\033[95m\";\n+constexpr const char *kColorBrightCyan = \"\\033[96m\";\n+constexpr const char *kColorBrightWhite = \"\\033[97m\";\n+\n+} /* namespace */\n+\n /**\n * \\brief Write message to log output\n * \\param[in] msg Message to write\n */\n void LogOutput::write(const LogMessage &msg)\n {\n+\tstatic const char *const severityColors[] = {\n+\t\tkColorBrightCyan,\n+\t\tkColorBrightGreen,\n+\t\tkColorBrightYellow,\n+\t\tkColorBrightRed,\n+\t\tkColorBrightMagenta,\n+\t};\n+\n+\tconst char *categoryColor = color_ ? kColorBrightWhite : \"\";\n+\tconst char *fileColor = color_ ? kColorBrightBlue : \"\";\n+\tconst char *msgColor = color_ ? kColorReset : \"\";\n+\tconst char *severityColor = \"\";\n+\tLogSeverity severity = msg.severity();\n \tstd::string str;\n \n+\tif (color_) {\n+\t\tif (static_cast<unsigned int>(severity) < std::size(severityColors))\n+\t\t\tseverityColor = severityColors[severity];\n+\t\telse\n+\t\t\tseverityColor = kColorBrightWhite;\n+\t}\n+\n \tswitch (target_) {\n \tcase LoggingTargetSyslog:\n-\t\tstr = std::string(log_severity_name(msg.severity())) + \" \"\n+\t\tstr = std::string(log_severity_name(severity)) + \" \"\n \t\t + msg.category().name() + \" \" + msg.fileInfo() + \" \"\n \t\t + msg.msg();\n-\t\twriteSyslog(msg.severity(), str);\n+\t\twriteSyslog(severity, str);\n \t\tbreak;\n \tcase LoggingTargetStream:\n \tcase LoggingTargetFile:\n \t\tstr = \"[\" + utils::time_point_to_string(msg.timestamp()) + \"] [\"\n \t\t + std::to_string(Thread::currentId()) + \"] \"\n-\t\t + log_severity_name(msg.severity()) + \" \"\n-\t\t + msg.category().name() + \" \" + msg.fileInfo() + \" \"\n-\t\t + msg.msg();\n+\t\t + severityColor + log_severity_name(severity) + \" \"\n+\t\t + categoryColor + msg.category().name() + \" \"\n+\t\t + fileColor + msg.fileInfo() + \" \"\n+\t\t + msgColor + msg.msg();\n \t\twriteStream(str);\n \t\tbreak;\n \tdefault:\n@@ -253,8 +298,8 @@ public:\n \tvoid write(const LogMessage &msg);\n \tvoid backtrace();\n \n-\tint logSetFile(const char *path);\n-\tint logSetStream(std::ostream *stream);\n+\tint logSetFile(const char *path, bool color);\n+\tint logSetStream(std::ostream *stream, bool color);\n \tint logSetTarget(LoggingTarget target);\n \tvoid logSetLevel(const char *category, const char *level);\n \n@@ -298,35 +343,47 @@ bool Logger::destroyed_ = false;\n /**\n * \\brief Direct logging to a file\n * \\param[in] path Full path to the log file\n+ * \\param[in] color True to output colored messages\n *\n * This function directs the log output to the file identified by \\a path. The\n * previous log target, if any, is closed, and all new log messages will be\n * written to the new log file.\n *\n+ * \\a color controls whether or not the messages will be colored with standard\n+ * ANSI escape codes. This is done regardless of whether \\a path refers to a\n+ * standard file or a TTY, the caller is responsible for disabling coloring when\n+ * not suitable for the log target.\n+ *\n * If the function returns an error, the log target is not changed.\n *\n * \\return Zero on success, or a negative error code otherwise\n */\n-int logSetFile(const char *path)\n+int logSetFile(const char *path, bool color)\n {\n-\treturn Logger::instance()->logSetFile(path);\n+\treturn Logger::instance()->logSetFile(path, color);\n }\n \n /**\n * \\brief Direct logging to a stream\n * \\param[in] stream Stream to send log output to\n+ * \\param[in] color True to output colored messages\n *\n * This function directs the log output to \\a stream. The previous log target,\n * if any, is closed, and all new log messages will be written to the new log\n * stream.\n *\n+ * \\a color controls whether or not the messages will be colored with standard\n+ * ANSI escape codes. This is done regardless of whether \\a stream refers to a\n+ * standard file or a TTY, the caller is responsible for disabling coloring when\n+ * not suitable for the log target.\n+ *\n * If the function returns an error, the log file is not changed\n *\n * \\return Zero on success, or a negative error code otherwise.\n */\n-int logSetStream(std::ostream *stream)\n+int logSetStream(std::ostream *stream, bool color)\n {\n-\treturn Logger::instance()->logSetStream(stream);\n+\treturn Logger::instance()->logSetStream(stream, color);\n }\n \n /**\n@@ -437,14 +494,16 @@ void Logger::backtrace()\n /**\n * \\brief Set the log file\n * \\param[in] path Full path to the log file\n+ * \\param[in] color True to output colored messages\n *\n * \\sa libcamera::logSetFile()\n *\n * \\return Zero on success, or a negative error code otherwise.\n */\n-int Logger::logSetFile(const char *path)\n+int Logger::logSetFile(const char *path, bool color)\n {\n-\tstd::shared_ptr<LogOutput> output = std::make_shared<LogOutput>(path);\n+\tstd::shared_ptr<LogOutput> output =\n+\t\tstd::make_shared<LogOutput>(path, color);\n \tif (!output->isValid())\n \t\treturn -EINVAL;\n \n@@ -455,14 +514,16 @@ int Logger::logSetFile(const char *path)\n /**\n * \\brief Set the log stream\n * \\param[in] stream Stream to send log output to\n+ * \\param[in] color True to output colored messages\n *\n * \\sa libcamera::logSetStream()\n *\n * \\return Zero on success, or a negative error code otherwise.\n */\n-int Logger::logSetStream(std::ostream *stream)\n+int Logger::logSetStream(std::ostream *stream, bool color)\n {\n-\tstd::shared_ptr<LogOutput> output = std::make_shared<LogOutput>(stream);\n+\tstd::shared_ptr<LogOutput> output =\n+\t\tstd::make_shared<LogOutput>(stream, color);\n \tstd::atomic_store(&output_, output);\n \treturn 0;\n }\n@@ -514,10 +575,15 @@ void Logger::logSetLevel(const char *category, const char *level)\n \n /**\n * \\brief Construct a logger\n+ *\n+ * If the environment variable is not set, log to std::cerr. The log messages\n+ * are then colored by default. This can be overridden by setting the\n+ * LIBCAMERA_LOG_NO_COLOR environment variable to disabled coloring.\n */\n Logger::Logger()\n {\n-\tlogSetStream(&std::cerr);\n+\tbool color = !utils::secure_getenv(\"LIBCAMERA_LOG_NO_COLOR\");\n+\tlogSetStream(&std::cerr, color);\n \n \tparseLogFile();\n \tparseLogLevels();\n@@ -543,7 +609,7 @@ void Logger::parseLogFile()\n \t\treturn;\n \t}\n \n-\tlogSetFile(file);\n+\tlogSetFile(file, false);\n }\n \n /**\ndiff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp\nindex 70d73822193b..d934596e4145 100644\n--- a/src/libcamera/camera_manager.cpp\n+++ b/src/libcamera/camera_manager.cpp\n@@ -290,7 +290,7 @@ CameraManager::~CameraManager()\n */\n int CameraManager::start()\n {\n-\tLOG(Camera, Info) << \"libcamera \" << version_;\n+\tLOG(Camera, Debug) << \"libcamera \" << version_;\n \n \tint ret = _d()->start();\n \tif (ret)\n", "prefixes": [ "libcamera-devel", "4/5" ] }