From patchwork Thu Jun 2 07:34:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16132 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id E42D8C3256 for ; Thu, 2 Jun 2022 07:34:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C0AF965633; Thu, 2 Jun 2022 09:34:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1654155254; bh=FlfL6DMQE70U8Vd6TwwD5DQnnz9cNY963YOtjCmwT3M=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=tU+mWVi4oipoPGTjtRGO89OqCfT3NkCr5gSd4+Ge+MmqjNuUfOL8Dx4AkFj1I8Sm2 uEAKLAhYG+V2le2IVNL6F/2SNcD6IW3wHSKMXtb6eX0ru6WxSKB2pV9sl1E0xHviFj CKoEyCp0lT7sO6H53gzAAttU1wxd6QDf6ifEkxuYF0yQutU16GAiR6o656Q1id36FX bBLRLb6Qjifh+ARA5NR6HEuPNbsquX/gyLeo1QSZQ147YKOPJuEyLtG3KMeppwb5V9 /dyUrtUo5WcAeD7uIHkCMzYuIlvEaN3GirpQwMVsRWO0asCi2nz16vyyy+OPgyrgGj m3BWtIS8ya/Zw== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 31D1B65633 for ; Thu, 2 Jun 2022 09:34:13 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="kbW7BHPV"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (lmontsouris-659-1-41-236.w92-154.abo.wanadoo.fr [92.154.76.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id EBFE2895 for ; Thu, 2 Jun 2022 09:34:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1654155253; bh=FlfL6DMQE70U8Vd6TwwD5DQnnz9cNY963YOtjCmwT3M=; h=From:To:Subject:Date:In-Reply-To:References:From; b=kbW7BHPVd8IzpljaPsgj5M7CVi0J9oXBJeRFoc+9IF/0VnGbw1XU7+sAb525NZjqK f6UrChfRi+vuRU1CrqPL4FAbRplxyIzBIBcA6lqIRWpNZqSLt5A2fL1AUxpuD841uG V6TEa/ZOxSwfsaUneDtszzHewPWQkRNqCSAz6YrY= To: libcamera-devel@lists.libcamera.org Date: Thu, 2 Jun 2022 10:34:05 +0300 Message-Id: <20220602073405.28168-3-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 Subject: [libcamera-devel] [PATCH v2 2/2] libcamera: base: log: Color the log prefix X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add coloring to the log prefix to increase log readability. Signed-off-by: Laurent Pinchart Reviewed-by: Umang Jain Reviewed-by: Paul Elder --- include/libcamera/base/log.h | 5 ++++- src/libcamera/base/log.cpp | 38 ++++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 3f065267c914..3fc5ced38a6a 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -57,7 +57,8 @@ class LogMessage { public: LogMessage(const char *fileName, unsigned int line, - const LogCategory &category, LogSeverity severity); + const LogCategory &category, LogSeverity severity, + const std::string &prefix = std::string()); LogMessage(LogMessage &&); ~LogMessage(); @@ -68,6 +69,7 @@ public: LogSeverity severity() const { return severity_; } const LogCategory &category() const { return category_; } const std::string &fileInfo() const { return fileInfo_; } + const std::string &prefix() const { return prefix_; } const std::string msg() const { return msgStream_.str(); } private: @@ -80,6 +82,7 @@ private: LogSeverity severity_; utils::time_point timestamp_; std::string fileInfo_; + std::string prefix_; }; class Loggable diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index eefdda4ba177..5c359a22d71b 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -189,6 +189,7 @@ namespace { * https://en.wikipedia.org/wiki/ANSI_escape_code#Colors. */ constexpr const char *kColorReset = "\033[0m"; +constexpr const char *kColorGreen = "\033[0;32m"; constexpr const char *kColorBrightRed = "\033[1;31m"; constexpr const char *kColorBrightGreen = "\033[1;32m"; constexpr const char *kColorBrightYellow = "\033[1;33m"; @@ -215,6 +216,7 @@ void LogOutput::write(const LogMessage &msg) const char *categoryColor = color_ ? kColorBrightWhite : ""; const char *fileColor = color_ ? kColorBrightBlue : ""; + const char *prefixColor = color_ ? kColorGreen : ""; const char *resetColor = color_ ? kColorReset : ""; const char *severityColor = ""; LogSeverity severity = msg.severity(); @@ -230,8 +232,10 @@ void LogOutput::write(const LogMessage &msg) switch (target_) { case LoggingTargetSyslog: str = std::string(log_severity_name(severity)) + " " - + msg.category().name() + " " + msg.fileInfo() + " " - + msg.msg(); + + msg.category().name() + " " + msg.fileInfo() + " "; + if (!msg.prefix().empty()) + str += msg.prefix() + ": "; + str += msg.msg(); writeSyslog(severity, str); break; case LoggingTargetStream: @@ -240,8 +244,10 @@ void LogOutput::write(const LogMessage &msg) + std::to_string(Thread::currentId()) + "] " + severityColor + log_severity_name(severity) + " " + categoryColor + msg.category().name() + " " - + fileColor + msg.fileInfo() + " " - + resetColor + msg.msg(); + + fileColor + msg.fileInfo() + " "; + if (!msg.prefix().empty()) + str += prefixColor + msg.prefix() + ": "; + str += resetColor + msg.msg(); writeStream(str); break; default: @@ -819,14 +825,17 @@ const LogCategory &LogCategory::defaultCategory() * will be displayed * \param[in] severity The log message severity, controlling how the message * will be displayed + * \param[in] prefix The log message prefix * * Create a log message pertaining to line \a line of file \a fileName. The * \a severity argument sets the message severity to control whether it will be - * output or dropped. + * output or dropped. The \a prefix optionally identifies the object instance + * logging the message. */ LogMessage::LogMessage(const char *fileName, unsigned int line, - const LogCategory &category, LogSeverity severity) - : category_(category), severity_(severity) + const LogCategory &category, LogSeverity severity, + const std::string &prefix) + : category_(category), severity_(severity), prefix_(prefix) { init(fileName, line); } @@ -915,6 +924,12 @@ LogMessage::~LogMessage() * \return The file info of the message */ +/** + * \fn LogMessage::prefix() + * \brief Retrieve the prefix of the log message + * \return The prefix of the message + */ + /** * \fn LogMessage::msg() * \brief Retrieve the message text of the log message @@ -962,12 +977,9 @@ Loggable::~Loggable() LogMessage Loggable::_log(const LogCategory *category, LogSeverity severity, const char *fileName, unsigned int line) const { - LogMessage msg(fileName, line, - category ? *category : LogCategory::defaultCategory(), - severity); - - msg.stream() << logPrefix() << ": "; - return msg; + return LogMessage(fileName, line, + category ? *category : LogCategory::defaultCategory(), + severity, logPrefix()); } /**