{"id":16133,"url":"https://patchwork.libcamera.org/api/patches/16133/?format=json","web_url":"https://patchwork.libcamera.org/patch/16133/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220602073405.28168-2-laurent.pinchart@ideasonboard.com>","date":"2022-06-02T07:34:04","name":"[libcamera-devel,v2,1/2] libcamera: base: log: Add coloring to the log output","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"bee45bae21344db6df0a5918a9ceb38d42bd9a61","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16133/mbox/","series":[{"id":3153,"url":"https://patchwork.libcamera.org/api/series/3153/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3153","date":"2022-06-02T07:34:03","name":"libcamera: Add colors to the log","version":2,"mbox":"https://patchwork.libcamera.org/series/3153/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16133/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16133/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 90092BD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  2 Jun 2022 07:34:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 34D546040E;\n\tThu,  2 Jun 2022 09:34:15 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 229A265631\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  2 Jun 2022 09:34:13 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(lmontsouris-659-1-41-236.w92-154.abo.wanadoo.fr [92.154.76.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id CB07387B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  2 Jun 2022 09:34:12 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1654155255;\n\tbh=t9WmefzwggcMGUs4YDZN5/1KYuEqcLuZpQG/Pctbw9s=;\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=kMxtTinH2dn6wre7eQNOGp74pQZtb67NblwzRBX2tvOHU0WJmUxjd3FvNmygahVR/\n\te8xXphTxgwxonOYyiIPMhvZlEV+tLvymPxFB6DoKrU8UQ/rRvf0Va7jGLUwRcJtGmq\n\tzSTu1OM1DOS1vTaMWCISPIZ2E7DaDiYt/Fao6jFQ4OarZ5/vuub6Cg/45OX2R6xnet\n\t/lwePFvWicTeVbIp7M2C0TxvBu8zxIxfvlZb3IX52ki1SMBSur5Bh0q1O1Qj9cnmaM\n\tgkDTYX8CYHMxPvNYCaAX+L5dOk6ySCsAZKS/4gtpCHmtx+rzgVlpkwRF7Synga+7hC\n\tykprLVKYNhGRA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1654155252;\n\tbh=t9WmefzwggcMGUs4YDZN5/1KYuEqcLuZpQG/Pctbw9s=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=Bu3bxrboJ1642B5tL5aDf7+8Un5VccL++XSEt2NddRmJc4i8owQwJszAe6+eKbcud\n\t/NkcMjMB4Zz2Okv1VviQ6dbx9YzePu8CofQq9Ck7gpWJXROUTvHJkyGrA7Nxm0EPP5\n\tyYCB5p5zg9xNqUenb/8323ueclilDhal1jgom3Go="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Bu3bxrbo\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Thu,  2 Jun 2022 10:34:04 +0300","Message-Id":"<20220602073405.28168-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20220602073405.28168-1-laurent.pinchart@ideasonboard.com>","References":"<20220602073405.28168-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 1/2] libcamera: base: log: Add coloring\n\tto the 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>\nReviewed-by: Umang Jain <umang.jain@ideasonboard.com>\n---\nChanges since v1:\n\n- Use '[1;3xm' instead of '[9xm' for bright colors\n- Rename msgColor to resetColor\n- Add link to https://en.wikipedia.org/wiki/ANSI_escape_code#Colors\n- Drop debug leftovers\n- Fix typo\n---\n Documentation/environment_variables.rst |  21 +++--\n include/libcamera/logging.h             |   4 +-\n src/libcamera/base/log.cpp              | 110 ++++++++++++++++++------\n 3 files changed, 104 insertions(+), 31 deletions(-)","diff":"diff --git a/Documentation/environment_variables.rst b/Documentation/environment_variables.rst\nindex fa703a726845..0a7760cbea42 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 default 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..eefdda4ba177 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,66 @@ bool LogOutput::isValid() const\n \t}\n }\n \n+namespace {\n+\n+/*\n+ * For more information about ANSI escape codes, see\n+ * https://en.wikipedia.org/wiki/ANSI_escape_code#Colors.\n+ */\n+constexpr const char *kColorReset = \"\\033[0m\";\n+constexpr const char *kColorBrightRed = \"\\033[1;31m\";\n+constexpr const char *kColorBrightGreen = \"\\033[1;32m\";\n+constexpr const char *kColorBrightYellow = \"\\033[1;33m\";\n+constexpr const char *kColorBrightBlue = \"\\033[1;34m\";\n+constexpr const char *kColorBrightMagenta = \"\\033[1;35m\";\n+constexpr const char *kColorBrightCyan = \"\\033[1;36m\";\n+constexpr const char *kColorBrightWhite = \"\\033[1;37m\";\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 *resetColor = 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    + resetColor + msg.msg();\n \t\twriteStream(str);\n \t\tbreak;\n \tdefault:\n@@ -253,8 +294,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 +339,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 +490,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 +510,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 +571,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 disable 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 +605,7 @@ void Logger::parseLogFile()\n \t\treturn;\n \t}\n \n-\tlogSetFile(file);\n+\tlogSetFile(file, false);\n }\n \n /**\n","prefixes":["libcamera-devel","v2","1/2"]}