From patchwork Wed May 5 15:33:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Felsch X-Patchwork-Id: 12198 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 12F2FBDE7C for ; Wed, 5 May 2021 15:33:29 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8836568909; Wed, 5 May 2021 17:33:28 +0200 (CEST) Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id F3F57602BD for ; Wed, 5 May 2021 17:33:26 +0200 (CEST) Received: from dude02.hi.pengutronix.de ([2001:67c:670:100:1d::28]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1leJWg-0004Rq-Ig for libcamera-devel@lists.libcamera.org; Wed, 05 May 2021 17:33:26 +0200 Received: from mfe by dude02.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1leJWg-0002Ns-9s for libcamera-devel@lists.libcamera.org; Wed, 05 May 2021 17:33:26 +0200 From: Marco Felsch To: libcamera-devel@lists.libcamera.org Date: Wed, 5 May 2021 17:33:18 +0200 Message-Id: <20210505153318.8519-1-m.felsch@pengutronix.de> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::28 X-SA-Exim-Mail-From: mfe@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH] libcamera: log: add colors to log levels 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add colored logs if the output belongs to a terminal. This makes it easier to identitify warnings and/or errors. Signed-off-by: Marco Felsch --- Hi all, this is an reworked version of [1]. I used by sob and auther since I did more changes than I kept from [1]. [1] 20210203181746.22028-1-m.cichy@pengutronix.de src/libcamera/log.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp index 94175ab3..3dffd004 100644 --- a/src/libcamera/log.cpp +++ b/src/libcamera/log.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -98,6 +99,26 @@ static const char *log_severity_name(LogSeverity severity) return "UNKWN"; } +static const char *log_severity_color_name(LogSeverity severity) +{ + static const char *const names[] = { + "\033[1m\033[37mDEBUG\033[0m", + "\033[1m\033[32m INFO\033[0m", + "\033[1m\033[33m WARN\033[0m", + "\033[1m\033[31mERROR\033[0m", + "\033[1m\033[35mFATAL\033[0m", + }; + + /* Only print colored output if output really belongs to a terminal */ + if (!isatty(fileno(stderr))) + return log_severity_name(severity); + + if (static_cast(severity) < std::size(names)) + return names[severity]; + else + return "\033[1m\033[32mUNKWN\033[0m"; +} + /** * \brief Log output * @@ -197,6 +218,13 @@ void LogOutput::write(const LogMessage &msg) writeSyslog(msg.severity(), str); break; case LoggingTargetStream: + str = "[" + utils::time_point_to_string(msg.timestamp()) + "] [" + + std::to_string(Thread::currentId()) + "] " + + log_severity_color_name(msg.severity()) + " " + + msg.category().name() + " " + msg.fileInfo() + " " + + msg.msg(); + writeStream(str); + break; case LoggingTargetFile: str = "[" + utils::time_point_to_string(msg.timestamp()) + "] [" + std::to_string(Thread::currentId()) + "] "