From patchwork Wed May 25 22:24:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16044 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 6A2C8BD161 for ; Wed, 25 May 2022 22:25:16 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1F48E65671; Thu, 26 May 2022 00:25:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653517516; bh=INnkauI5QY7zzg0eLFg/dvCTVOoF5Vo5N0zb8WkSeA4=; 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=1ayhZPuEUQIEi3hkINpDdVg9iZtYfmJloYrv8drPryQVGQyc9uvO6fZj5xKJNgRPp 53XlWezzX2O4WfvmvJ167VQM14YBsaawYXzsKhosgzxf+oVYVbi5yDVlx7pKCoyUew QQLvPK/4DL7EaB4uVthhzgqcRedO4qYbo9yH+kDDyK5SwxqMGxGZOx9v2e1Wc3mCBX M/rUU3ZOpqJBfTEdxIRdfvlNdYiLLftToF0zhEYlYFfTJ/qp+6Bn75xsrwBr2MFrD+ NKcU//45RYMixMivGDSlQpCKzUkLoPXJljmfS0Js4XKXo52Smz6eVcp8/8oJ2G3btV 7RiiCrUIi3AbA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 18D1060422 for ; Thu, 26 May 2022 00:25:12 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="bdTA7vL+"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (ip-109-40-242-63.web.vodafone.de [109.40.242.63]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 8CDC2AFD; Thu, 26 May 2022 00:25:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1653517511; bh=INnkauI5QY7zzg0eLFg/dvCTVOoF5Vo5N0zb8WkSeA4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bdTA7vL+0NsOaQYeBJxX9fw7bO3axL3m1m8hwZ4hwHaqrpEx8usVX4dHoEfwGJY03 xdVymxGPdrLAaiEe5TUxYHUHY7t/WcwWqEMJkejNt4VT1tvmD3Y5kQVq80KkRj48U/ nP2FgaLdmwV6J2n8PyEO3z6NJxW/dqe4yoz5pp5A= To: libcamera-devel@lists.libcamera.org Date: Thu, 26 May 2022 01:24:59 +0300 Message-Id: <20220525222503.6460-2-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 Subject: [libcamera-devel] [PATCH 1/5] libcamera: base: log: Remove unnecessary local variable 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" The output local variable in Logger::logSetTarget() isn't necessary. Remove it. Signed-off-by: Laurent Pinchart Reviewed-by: Umang Jain Reviewed-by: Paul Elder --- src/libcamera/base/log.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 64813b6607c5..df84ba4e8328 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -477,15 +477,11 @@ int Logger::logSetStream(std::ostream *stream) */ int Logger::logSetTarget(enum LoggingTarget target) { - std::shared_ptr output; - switch (target) { case LoggingTargetSyslog: - output = std::make_shared(); - std::atomic_store(&output_, output); + std::atomic_store(&output_, std::make_shared()); break; case LoggingTargetNone: - output = nullptr; std::atomic_store(&output_, std::shared_ptr()); break; default: From patchwork Wed May 25 22:25:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16045 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 8AC0CC3256 for ; Wed, 25 May 2022 22:25:17 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B7FFD6566C; Thu, 26 May 2022 00:25:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653517516; bh=PoqoB8GftOOmh7JVMbF/JdLM/JKTkM537VnYpQ2xOhI=; 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=cx9COkJn/fXljPwwhIq1kgfzjSzg+X1PXezMWEKMz8kUUsC2uHJyvKar0ixWVgBYd uc7N1mPrAjiR/+60XsakoNcZpg5CxZadsNBlvU7qB/0hvMcpdEDvK4Sq8ANB5Faq8s 0BpSRTrKCd/JULmX3bLQFOpyMnhbFPMgZHq1ZADmemcEEGVyzw3KU/BIE2gAdw2ghN GSv8349gfzzeUjPgK/kSvZRr7QjsNQ4EHKaMV2e18XyiK+34W1LDheAiUe1Kh11phL LOIZvbpOf3imZuZbqj9vY65vILpa0WTcxcvZA0n7P0RoJf84VXrHse9ydBpGL5HrUj iCQbJNHasQ1Wg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8CBFD60422 for ; Thu, 26 May 2022 00:25:12 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="YB5Va64/"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (ip-109-40-242-63.web.vodafone.de [109.40.242.63]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 17BF7BB3; Thu, 26 May 2022 00:25:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1653517512; bh=PoqoB8GftOOmh7JVMbF/JdLM/JKTkM537VnYpQ2xOhI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YB5Va64/h0kR1sD3ydLkCHb9yflLBOOFTB9BMbNIyJmt6Hy115mHrx7wrQ0ZxMUxz ULtZEmrpE51HvGtUywvOxJjMxMqFMpqa8bfiIlKoKiQmjl5qhaZ4BMUErCv4wF+Smx uVpMeACUhBTwyzvjO5yNQ/An/wiAIX1+Zx0ZQTd4= To: libcamera-devel@lists.libcamera.org Date: Thu, 26 May 2022 01:25:00 +0300 Message-Id: <20220525222503.6460-3-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 Subject: [libcamera-devel] [PATCH 2/5] libcamera: base: log: Replace stderr with std::cerr in documentation 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" While std::cerr and stderr both target the same file by default, this may be overridden by applications. Update the documentation to use std::cerr instead of stderr to be accurate. Signed-off-by: Laurent Pinchart Reviewed-by: Umang Jain Reviewed-by: Paul Elder --- src/libcamera/base/log.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index df84ba4e8328..9ae62368a54d 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -46,11 +46,11 @@ * their category are output to the log, while other messages are silently * discarded. * - * By default log messages are output to stderr. They can be redirected to a log - * file by setting the LIBCAMERA_LOG_FILE environment variable to the name of - * the file. The file must be writable and is truncated if it exists. If any + * By default log messages are output to std::cerr. They can be redirected to a + * log file by setting the LIBCAMERA_LOG_FILE environment variable to the name + * of the file. The file must be writable and is truncated if it exists. If any * error occurs when opening the file, the file is ignored and the log is output - * to stderr. + * to std::cerr. */ /** @@ -527,7 +527,8 @@ Logger::Logger() * If the LIBCAMERA_LOG_FILE environment variable is set, open the file it * points to and redirect the logger output to it. If the environment variable * is set to "syslog", then the logger output will be directed to syslog. Errors - * are silently ignored and don't affect the logger output (set to stderr). + * are silently ignored and don't affect the logger output (set to std::cerr by + * default). */ void Logger::parseLogFile() { From patchwork Wed May 25 22:25:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16046 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 BEEADC326D for ; Wed, 25 May 2022 22:25:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C5E0465663; Thu, 26 May 2022 00:25:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653517517; bh=BnBM4ZHYsqKV4XZBmPL+ckPipLUUlQcsT4unuHQEajY=; 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=eRv37vVQA4iDoNRkiifAaPUcjjM00Q140mfoIqfsn/sRTizYpHFi39FOLxM673CDN JKNke38qcUWc5qt0/F6gjtswrAtMatw2JWmrcZy5mSsu+mt/BjI1hEJHw8rR1bqBf2 A650sbjDVUBzdWof+caucwC+eSCJpe4SHgkO9WIdSl5rP1Fb04QexjFuMLJP7SXtaQ kCY3lrL2gIycA6gxWT1JiRZTtdjBZzc7XKx1SSxGJO66BIS/nVaZ8lfTLDHaB//bIQ rD9wj6lJoAO21O9NlHc8WWgd8qvmJnQGSdP4klMXMqsQGvATUx+BacgPQRK4XZSywk QzYUyTPju72hg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1683B60422 for ; Thu, 26 May 2022 00:25:13 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="B8ietL3X"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (ip-109-40-242-63.web.vodafone.de [109.40.242.63]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 96473D80; Thu, 26 May 2022 00:25:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1653517512; bh=BnBM4ZHYsqKV4XZBmPL+ckPipLUUlQcsT4unuHQEajY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B8ietL3XnfLtYpBggndBf/GHfbeVizxjENYwsfvjAbdN3sFcuBzVTadgcJ/cx8x8U 2CJtjspmsnfi/92IXa1PBOhwlFx/cVbREHZmy3GonNEs8TE2QJcg1P7P7oce34molm tmHEjEUCepb87t+hh0Pi/2C7+S1WAosBoL5OJcnY= To: libcamera-devel@lists.libcamera.org Date: Thu, 26 May 2022 01:25:01 +0300 Message-Id: <20220525222503.6460-4-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 Subject: [libcamera-devel] [PATCH 3/5] libcamera: base: log: Log to std::cerr if log file can't be opened 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" If the log file specified through LIBCAMERA_LOG_FILE can't be opened, logging is currently completely disabled. This doesn't match the documented behaviour that tells std::cerr is used instead. Fix it to match the documentation. Signed-off-by: Laurent Pinchart Reviewed-by: Umang Jain Reviewed-by: Paul Elder --- src/libcamera/base/log.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 9ae62368a54d..26f1420703b9 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -517,6 +517,8 @@ void Logger::logSetLevel(const char *category, const char *level) */ Logger::Logger() { + logSetStream(&std::cerr); + parseLogFile(); parseLogLevels(); } @@ -533,10 +535,8 @@ Logger::Logger() void Logger::parseLogFile() { const char *file = utils::secure_getenv("LIBCAMERA_LOG_FILE"); - if (!file) { - logSetStream(&std::cerr); + if (!file) return; - } if (!strcmp(file, "syslog")) { logSetTarget(LoggingTargetSyslog); From patchwork Wed May 25 22:25:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16047 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 47669C326E for ; Wed, 25 May 2022 22:25:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A85BB6566E; Thu, 26 May 2022 00:25:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653517518; bh=i8rER45dnLUM13zU+IdBjK7JbbAwNIWJ0aoOU0RlxAk=; 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=3R7u6JgRVRJy6VqJSRCI73qiCRkrBIgNuVZ0pEbigk8hQfFEX/4O0oybBKjuhDFbH 6PA2Ge/vybUjdp6pEMC7/+bTcrJd9miB9UBM9uOwjuTImFUdKoP6baeFU8qYE8KEry Slr9hJYzp0lNcFCVErnd5sr/GmrIcqqym2xpN0Gma5WR1f0QMBFXC4ffSJY7rC7Axf Zs8LYZicoB11GBsukTSMqzLZ5R9I3ABqKizViM9CqE1Mp6qikaji0nbOZEEVHwy8+z uZQNyfJPymGwTHDiPSwvKZjSXVLlZfoFDdXlOuwRjYHQ00cSgFtV3nHuXW698y6BT2 viFklJi7FQylg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E2A2165662 for ; Thu, 26 May 2022 00:25:13 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Z8hQXS47"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (ip-109-40-242-63.web.vodafone.de [109.40.242.63]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 36B2312F3; Thu, 26 May 2022 00:25:13 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1653517513; bh=i8rER45dnLUM13zU+IdBjK7JbbAwNIWJ0aoOU0RlxAk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z8hQXS47Dzip7S7fLrLNdFe/oyL394T2n41nM2SNGimAVmzxxRvebnJ2UvDeKkb+N 6z31CI+cguZDhl4jV+SSFXFpEansu1zLJSR7HclkFCF4HkuzWi8aDI9Ts3Zaddtxik oF1zrBt5qs7JNBQZ8O8Je8Of9uC/GDb8srTcxqAg= 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 Subject: [libcamera-devel] [PATCH 4/5] libcamera: base: log: Add coloring to the log output 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" Extend the logger to support coloring messages. The log level is colorized with per-level colors, and the category with a fixed color. This makes the log output more readable. Coloring is enabled by default when logging to std::cerr, and can be disabled by setting the LIBCAMERA_LOG_NO_COLOR environment variable. When logging to a file with LIBCAMERA_LOG_FILE, coloring is disabled. It can be enabled for file logging using the logSetFile() function. Signed-off-by: Laurent Pinchart Reviewed-by: Umang Jain --- Documentation/environment_variables.rst | 21 +++-- include/libcamera/logging.h | 4 +- src/libcamera/base/log.cpp | 114 +++++++++++++++++++----- src/libcamera/camera_manager.cpp | 2 +- 4 files changed, 109 insertions(+), 32 deletions(-) diff --git a/Documentation/environment_variables.rst b/Documentation/environment_variables.rst index fa703a726845..7028c024b14e 100644 --- a/Documentation/environment_variables.rst +++ b/Documentation/environment_variables.rst @@ -19,6 +19,9 @@ LIBCAMERA_LOG_LEVELS Example value: ``*:DEBUG`` +LIBCAMERA_LOG_NO_COLOR + Disable coloring of log messages (`more `__). + LIBCAMERA_IPA_CONFIG_PATH Define custom search locations for IPA configurations (`more `__). @@ -40,12 +43,20 @@ Further details Notes about debugging ~~~~~~~~~~~~~~~~~~~~~ -The environment variables ``LIBCAMERA_LOG_FILE`` and ``LIBCAMERA_LOG_LEVELS`` -are used to modify the destination and verbosity of messages provided by -libcamera. +The environment variables ``LIBCAMERA_LOG_FILE``, ``LIBCAMERA_LOG_LEVELS`` and +``LIBCAMERA_LOG_NO_COLOR`` are used to modify the degault configuration of the +libcamera logger. -The ``LIBCAMERA_LOG_LEVELS`` variable accepts a comma-separated list of -'category:level' pairs. +By default, libcamera logs all messages to the standard error (std::cerr). +Messages are colored by default depending on the log level. Coloring can be +disabled by setting the ``LIBCAMERA_LOG_NO_COLOR`` environment variable. + +The default log destination can also be directed to a file by setting the +``LIBCAMERA_LOG_FILE`` environment variable to the log file name. This also +disables coloring. + +Log levels are controlled through the ``LIBCAMERA_LOG_LEVELS`` variable, which +accepts a comma-separated list of 'category:level' pairs. The `level `__ part is mandatory and can either be specified by name or by numerical index associated with each level. diff --git a/include/libcamera/logging.h b/include/libcamera/logging.h index c36882b91974..cd842f67d553 100644 --- a/include/libcamera/logging.h +++ b/include/libcamera/logging.h @@ -16,8 +16,8 @@ enum LoggingTarget { LoggingTargetStream, }; -int logSetFile(const char *path); -int logSetStream(std::ostream *stream); +int logSetFile(const char *path, bool color = false); +int logSetStream(std::ostream *stream, bool color = false); int logSetTarget(LoggingTarget target); void logSetLevel(const char *category, const char *level); diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 26f1420703b9..a9f5bbbd36f7 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -104,8 +104,8 @@ static const char *log_severity_name(LogSeverity severity) class LogOutput { public: - LogOutput(const char *path); - LogOutput(std::ostream *stream); + LogOutput(const char *path, bool color); + LogOutput(std::ostream *stream, bool color); LogOutput(); ~LogOutput(); @@ -119,14 +119,16 @@ private: std::ostream *stream_; LoggingTarget target_; + bool color_; }; /** * \brief Construct a log output based on a file * \param[in] path Full path to log file + * \param[in] color True to output colored messages */ -LogOutput::LogOutput(const char *path) - : target_(LoggingTargetFile) +LogOutput::LogOutput(const char *path, bool color) + : target_(LoggingTargetFile), color_(color) { stream_ = new std::ofstream(path); } @@ -134,9 +136,10 @@ LogOutput::LogOutput(const char *path) /** * \brief Construct a log output based on a stream * \param[in] stream Stream to send log output to + * \param[in] color True to output colored messages */ -LogOutput::LogOutput(std::ostream *stream) - : stream_(stream), target_(LoggingTargetStream) +LogOutput::LogOutput(std::ostream *stream, bool color) + : stream_(stream), target_(LoggingTargetStream), color_(color) { } @@ -144,7 +147,7 @@ LogOutput::LogOutput(std::ostream *stream) * \brief Construct a log output to syslog */ LogOutput::LogOutput() - : stream_(nullptr), target_(LoggingTargetSyslog) + : stream_(nullptr), target_(LoggingTargetSyslog), color_(false) { openlog("libcamera", LOG_PID, 0); } @@ -179,28 +182,70 @@ bool LogOutput::isValid() const } } +namespace { + +constexpr const char *kColorReset = "\033[0m"; +constexpr const char *kColorRed = "\033[31m"; +constexpr const char *kColorGreen = "\033[32m"; +constexpr const char *kColorYellow = "\033[33m"; +constexpr const char *kColorBlue = "\033[34m"; +constexpr const char *kColorMagenta = "\033[35m"; +constexpr const char *kColorCyan = "\033[36m"; +constexpr const char *kColorWhite = "\033[37m"; +constexpr const char *kColorGrey = "\033[90m"; +constexpr const char *kColorBrightRed = "\033[91m"; +constexpr const char *kColorBrightGreen = "\033[92m"; +constexpr const char *kColorBrightYellow = "\033[93m"; +constexpr const char *kColorBrightBlue = "\033[94m"; +constexpr const char *kColorBrightMagenta = "\033[95m"; +constexpr const char *kColorBrightCyan = "\033[96m"; +constexpr const char *kColorBrightWhite = "\033[97m"; + +} /* namespace */ + /** * \brief Write message to log output * \param[in] msg Message to write */ void LogOutput::write(const LogMessage &msg) { + static const char *const severityColors[] = { + kColorBrightCyan, + kColorBrightGreen, + kColorBrightYellow, + kColorBrightRed, + kColorBrightMagenta, + }; + + const char *categoryColor = color_ ? kColorBrightWhite : ""; + const char *fileColor = color_ ? kColorBrightBlue : ""; + const char *msgColor = color_ ? kColorReset : ""; + const char *severityColor = ""; + LogSeverity severity = msg.severity(); std::string str; + if (color_) { + if (static_cast(severity) < std::size(severityColors)) + severityColor = severityColors[severity]; + else + severityColor = kColorBrightWhite; + } + switch (target_) { case LoggingTargetSyslog: - str = std::string(log_severity_name(msg.severity())) + " " + str = std::string(log_severity_name(severity)) + " " + msg.category().name() + " " + msg.fileInfo() + " " + msg.msg(); - writeSyslog(msg.severity(), str); + writeSyslog(severity, str); break; case LoggingTargetStream: case LoggingTargetFile: str = "[" + utils::time_point_to_string(msg.timestamp()) + "] [" + std::to_string(Thread::currentId()) + "] " - + log_severity_name(msg.severity()) + " " - + msg.category().name() + " " + msg.fileInfo() + " " - + msg.msg(); + + severityColor + log_severity_name(severity) + " " + + categoryColor + msg.category().name() + " " + + fileColor + msg.fileInfo() + " " + + msgColor + msg.msg(); writeStream(str); break; default: @@ -253,8 +298,8 @@ public: void write(const LogMessage &msg); void backtrace(); - int logSetFile(const char *path); - int logSetStream(std::ostream *stream); + int logSetFile(const char *path, bool color); + int logSetStream(std::ostream *stream, bool color); int logSetTarget(LoggingTarget target); void logSetLevel(const char *category, const char *level); @@ -298,35 +343,47 @@ bool Logger::destroyed_ = false; /** * \brief Direct logging to a file * \param[in] path Full path to the log file + * \param[in] color True to output colored messages * * This function directs the log output to the file identified by \a path. The * previous log target, if any, is closed, and all new log messages will be * written to the new log file. * + * \a color controls whether or not the messages will be colored with standard + * ANSI escape codes. This is done regardless of whether \a path refers to a + * standard file or a TTY, the caller is responsible for disabling coloring when + * not suitable for the log target. + * * If the function returns an error, the log target is not changed. * * \return Zero on success, or a negative error code otherwise */ -int logSetFile(const char *path) +int logSetFile(const char *path, bool color) { - return Logger::instance()->logSetFile(path); + return Logger::instance()->logSetFile(path, color); } /** * \brief Direct logging to a stream * \param[in] stream Stream to send log output to + * \param[in] color True to output colored messages * * This function directs the log output to \a stream. The previous log target, * if any, is closed, and all new log messages will be written to the new log * stream. * + * \a color controls whether or not the messages will be colored with standard + * ANSI escape codes. This is done regardless of whether \a stream refers to a + * standard file or a TTY, the caller is responsible for disabling coloring when + * not suitable for the log target. + * * If the function returns an error, the log file is not changed * * \return Zero on success, or a negative error code otherwise. */ -int logSetStream(std::ostream *stream) +int logSetStream(std::ostream *stream, bool color) { - return Logger::instance()->logSetStream(stream); + return Logger::instance()->logSetStream(stream, color); } /** @@ -437,14 +494,16 @@ void Logger::backtrace() /** * \brief Set the log file * \param[in] path Full path to the log file + * \param[in] color True to output colored messages * * \sa libcamera::logSetFile() * * \return Zero on success, or a negative error code otherwise. */ -int Logger::logSetFile(const char *path) +int Logger::logSetFile(const char *path, bool color) { - std::shared_ptr output = std::make_shared(path); + std::shared_ptr output = + std::make_shared(path, color); if (!output->isValid()) return -EINVAL; @@ -455,14 +514,16 @@ int Logger::logSetFile(const char *path) /** * \brief Set the log stream * \param[in] stream Stream to send log output to + * \param[in] color True to output colored messages * * \sa libcamera::logSetStream() * * \return Zero on success, or a negative error code otherwise. */ -int Logger::logSetStream(std::ostream *stream) +int Logger::logSetStream(std::ostream *stream, bool color) { - std::shared_ptr output = std::make_shared(stream); + std::shared_ptr output = + std::make_shared(stream, color); std::atomic_store(&output_, output); return 0; } @@ -514,10 +575,15 @@ 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 disabled coloring. */ Logger::Logger() { - logSetStream(&std::cerr); + bool color = !utils::secure_getenv("LIBCAMERA_LOG_NO_COLOR"); + logSetStream(&std::cerr, color); parseLogFile(); parseLogLevels(); @@ -543,7 +609,7 @@ void Logger::parseLogFile() return; } - logSetFile(file); + logSetFile(file, false); } /** diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp index 70d73822193b..d934596e4145 100644 --- a/src/libcamera/camera_manager.cpp +++ b/src/libcamera/camera_manager.cpp @@ -290,7 +290,7 @@ CameraManager::~CameraManager() */ int CameraManager::start() { - LOG(Camera, Info) << "libcamera " << version_; + LOG(Camera, Debug) << "libcamera " << version_; int ret = _d()->start(); if (ret) From patchwork Wed May 25 22:25:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16048 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 D003DC326F for ; Wed, 25 May 2022 22:25:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 621616566F; Thu, 26 May 2022 00:25:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653517519; bh=kuB568dBMjp61dX6X9khHhh4gpoISWt6Zsvpbz9V1wk=; 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=k1JbBwVHLk37NAOqfOcoqN8+9UM7/4BiVmWp/UYjsmxYGD8TEBKax/KFSFANOSPHx UVcoButgcIaOfRfueSoeaIHIUU0Xc59gMYV0HP3+t1ypUH20Nil9ugEZT2jiC30UMs 0eNqaI64VqxsFPBv51LQlYUVK/ykYfXbLk8qjbjFyaWl19VzP/isHs83MW5nDqAWh7 U8wuKogZpLS9VdP26ERX/eSLjZOnQFdz6agF2QlqRFS532iMpWY6N3g6JTL1ObPV24 SCm9Q9Rrz2/NdZw73zOlSeFOkvcbRNpe2iK07z1iPTEpl/gBDL8QzkcmNJ3n/Q6Ewf rMTIvTvJbshmA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 34B1E65663 for ; Thu, 26 May 2022 00:25:14 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="kVEGYEpQ"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (ip-109-40-242-63.web.vodafone.de [109.40.242.63]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B53FE130D; Thu, 26 May 2022 00:25:13 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1653517514; bh=kuB568dBMjp61dX6X9khHhh4gpoISWt6Zsvpbz9V1wk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kVEGYEpQovaxO9Vi5xOEE6SJTMeU2V8e2k/RWaM+si+JTzdPPH6EjFntSPbiX3AXX Lyn12KdtnRPZTDfnyQv5v+3m0eSJa2bW1NbVZmv2biKYkVdFEEnnVcSBhLi52RufJ7 LKVmGQB/cHUGHjvEN2nplLXVFbMk3cjbW5NEg3VU= To: libcamera-devel@lists.libcamera.org Date: Thu, 26 May 2022 01:25:03 +0300 Message-Id: <20220525222503.6460-6-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 Subject: [libcamera-devel] [PATCH 5/5] 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 | 37 +++++++++++++++++++++++------------- 2 files changed, 28 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 a9f5bbbd36f7..6620a50a5b6f 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -219,6 +219,7 @@ void LogOutput::write(const LogMessage &msg) const char *categoryColor = color_ ? kColorBrightWhite : ""; const char *fileColor = color_ ? kColorBrightBlue : ""; + const char *prefixColor = color_ ? kColorGreen : ""; const char *msgColor = color_ ? kColorReset : ""; const char *severityColor = ""; LogSeverity severity = msg.severity(); @@ -234,8 +235,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: @@ -244,8 +247,10 @@ void LogOutput::write(const LogMessage &msg) + std::to_string(Thread::currentId()) + "] " + severityColor + log_severity_name(severity) + " " + categoryColor + msg.category().name() + " " - + fileColor + msg.fileInfo() + " " - + msgColor + msg.msg(); + + fileColor + msg.fileInfo() + " "; + if (!msg.prefix().empty()) + str += prefixColor + msg.prefix() + ": "; + str += msgColor + msg.msg(); writeStream(str); break; default: @@ -823,14 +828,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); } @@ -919,6 +927,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 @@ -966,12 +980,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()); } /**