From patchwork Sun Dec 15 23:02:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 22324 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 29C42C32F6 for ; Sun, 15 Dec 2024 23:02:37 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9B38C67F3F; Mon, 16 Dec 2024 00:02:36 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="uus22VyD"; dkim-atps=neutral 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 509D267F35 for ; Mon, 16 Dec 2024 00:02:28 +0100 (CET) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id F0514675; Mon, 16 Dec 2024 00:01:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1734303712; bh=H3eOHpCnviI3I3rNvPcS0PHYuFpNPh0DjayX9XAj4ms=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uus22VyD5WhCikXQ6j41GHVav7SaQPces2j10Zfdx5MQlMd7LGBikstZl5lTRgxPr Rl+8TyvA7FANafU1zo2YA1x7fR34qOgZYq6G7Gau63DbwjIswu2p+NN7wJKNiBr6IB tocDTdVxMzUh30GaN6QPySTLuT53ePsJO/9QH1II= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [RFC PATCH 4/8] libcamera: base: log: Use std::string_view Date: Mon, 16 Dec 2024 01:02:02 +0200 Message-ID: <20241215230206.11002-5-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241215230206.11002-1-laurent.pinchart@ideasonboard.com> References: <20241215230206.11002-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 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" Replace usage of const std::string references passed to class member functions with std::string_view. This allows using static C string literals in the callers without the overhead of constructing a std::string instance. As string views are not guaranteed to be zero-terminated, two usages need to be modified: - The call to syslog() is changed to use "%.*s" instead of "%s" - The strtoul() call is replaced with std::from_chars() Signed-off-by: Laurent Pinchart --- include/libcamera/base/log.h | 3 ++- src/libcamera/base/log.cpp | 31 ++++++++++++++++--------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 620930125f2c1fa2..c22bafbd5b8019a2 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include @@ -60,7 +61,7 @@ class LogMessage public: LogMessage(const char *fileName, unsigned int line, const LogCategory &category, LogSeverity severity, - const std::string &prefix = std::string()); + std::string_view prefix = {}); LogMessage(LogMessage &&); ~LogMessage(); diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 3a656b8f099f673f..a2a59fbf26195340 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -8,11 +8,11 @@ #include #include +#include #include #include #include #include -#include #include #include #include @@ -112,11 +112,11 @@ public: bool isValid() const; void write(const LogMessage &msg); - void write(const std::string &msg); + void write(std::string_view msg); private: - void writeSyslog(LogSeverity severity, const std::string &msg); - void writeStream(const std::string &msg); + void writeSyslog(LogSeverity severity, std::string_view msg); + void writeStream(std::string_view msg); std::ostream *stream_; LoggingTarget target_; @@ -260,7 +260,7 @@ void LogOutput::write(const LogMessage &msg) * \brief Write string to log output * \param[in] str String to write */ -void LogOutput::write(const std::string &str) +void LogOutput::write(std::string_view str) { switch (target_) { case LoggingTargetSyslog: @@ -275,14 +275,15 @@ void LogOutput::write(const std::string &str) } } -void LogOutput::writeSyslog(LogSeverity severity, const std::string &str) +void LogOutput::writeSyslog(LogSeverity severity, std::string_view str) { - syslog(log_severity_to_syslog(severity), "%s", str.c_str()); + syslog(log_severity_to_syslog(severity), "%.*s", + static_cast(str.size()), str.data()); } -void LogOutput::writeStream(const std::string &str) +void LogOutput::writeStream(std::string_view str) { - stream_->write(str.c_str(), str.size()); + stream_->write(str.data(), str.size()); stream_->flush(); } @@ -311,7 +312,7 @@ private: void parseLogFile(); void parseLogLevels(); - static LogSeverity parseLogLevel(const std::string &level); + static LogSeverity parseLogLevel(std::string_view level); friend LogCategory; void registerCategory(LogCategory *category); @@ -675,7 +676,7 @@ void Logger::parseLogLevels() * * \return The log severity, or LogInvalid if the string is invalid */ -LogSeverity Logger::parseLogLevel(const std::string &level) +LogSeverity Logger::parseLogLevel(std::string_view level) { static const char *const names[] = { "DEBUG", @@ -688,9 +689,9 @@ LogSeverity Logger::parseLogLevel(const std::string &level) int severity; if (std::isdigit(level[0])) { - char *endptr; - severity = strtoul(level.c_str(), &endptr, 10); - if (*endptr != '\0' || severity > LogFatal) + std::from_chars_result res; + res = std::from_chars(level.begin(), level.end(), severity); + if (res.ptr != level.end() || severity > LogFatal) severity = LogInvalid; } else { severity = LogInvalid; @@ -874,7 +875,7 @@ const LogCategory &LogCategory::defaultCategory() */ LogMessage::LogMessage(const char *fileName, unsigned int line, const LogCategory &category, LogSeverity severity, - const std::string &prefix) + std::string_view prefix) : category_(category), severity_(severity), prefix_(prefix) { init(fileName, line);