{"id":22324,"url":"https://patchwork.libcamera.org/api/1.1/patches/22324/?format=json","web_url":"https://patchwork.libcamera.org/patch/22324/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20241215230206.11002-5-laurent.pinchart@ideasonboard.com>","date":"2024-12-15T23:02:02","name":"[RFC,4/8] libcamera: base: log: Use std::string_view","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"b0690ff1b13e01f94f44b3bd31b154a08b52d187","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22324/mbox/","series":[{"id":4889,"url":"https://patchwork.libcamera.org/api/1.1/series/4889/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4889","date":"2024-12-15T23:01:58","name":"libcamera: Use std::string_view","version":1,"mbox":"https://patchwork.libcamera.org/series/4889/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22324/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22324/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 29C42C32F6\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 15 Dec 2024 23:02:37 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9B38C67F3F;\n\tMon, 16 Dec 2024 00:02:36 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 509D267F35\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 16 Dec 2024 00:02:28 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id F0514675;\n\tMon, 16 Dec 2024 00:01:51 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"uus22VyD\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1734303712;\n\tbh=H3eOHpCnviI3I3rNvPcS0PHYuFpNPh0DjayX9XAj4ms=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=uus22VyD5WhCikXQ6j41GHVav7SaQPces2j10Zfdx5MQlMd7LGBikstZl5lTRgxPr\n\tRl+8TyvA7FANafU1zo2YA1x7fR34qOgZYq6G7Gau63DbwjIswu2p+NN7wJKNiBr6IB\n\ttocDTdVxMzUh30GaN6QPySTLuT53ePsJO/9QH1II=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","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","Content-Transfer-Encoding":"8bit","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Replace usage of const std::string references passed to class member\nfunctions with std::string_view. This allows using static C string\nliterals in the callers without the overhead of constructing a\nstd::string instance.\n\nAs string views are not guaranteed to be zero-terminated, two usages\nneed to be modified:\n\n- The call to syslog() is changed to use \"%.*s\" instead of \"%s\"\n\n- The strtoul() call is replaced with std::from_chars()\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/base/log.h |  3 ++-\n src/libcamera/base/log.cpp   | 31 ++++++++++++++++---------------\n 2 files changed, 18 insertions(+), 16 deletions(-)","diff":"diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h\nindex 620930125f2c1fa2..c22bafbd5b8019a2 100644\n--- a/include/libcamera/base/log.h\n+++ b/include/libcamera/base/log.h\n@@ -8,6 +8,7 @@\n #pragma once\n \n #include <sstream>\n+#include <string_view>\n \n #include <libcamera/base/private.h>\n \n@@ -60,7 +61,7 @@ class LogMessage\n public:\n \tLogMessage(const char *fileName, unsigned int line,\n \t\t   const LogCategory &category, LogSeverity severity,\n-\t\t   const std::string &prefix = std::string());\n+\t\t   std::string_view prefix = {});\n \n \tLogMessage(LogMessage &&);\n \t~LogMessage();\ndiff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\nindex 3a656b8f099f673f..a2a59fbf26195340 100644\n--- a/src/libcamera/base/log.cpp\n+++ b/src/libcamera/base/log.cpp\n@@ -8,11 +8,11 @@\n #include <libcamera/base/log.h>\n \n #include <array>\n+#include <charconv>\n #include <fstream>\n #include <iostream>\n #include <list>\n #include <stdio.h>\n-#include <stdlib.h>\n #include <string.h>\n #include <syslog.h>\n #include <time.h>\n@@ -112,11 +112,11 @@ public:\n \n \tbool isValid() const;\n \tvoid write(const LogMessage &msg);\n-\tvoid write(const std::string &msg);\n+\tvoid write(std::string_view msg);\n \n private:\n-\tvoid writeSyslog(LogSeverity severity, const std::string &msg);\n-\tvoid writeStream(const std::string &msg);\n+\tvoid writeSyslog(LogSeverity severity, std::string_view msg);\n+\tvoid writeStream(std::string_view msg);\n \n \tstd::ostream *stream_;\n \tLoggingTarget target_;\n@@ -260,7 +260,7 @@ void LogOutput::write(const LogMessage &msg)\n  * \\brief Write string to log output\n  * \\param[in] str String to write\n  */\n-void LogOutput::write(const std::string &str)\n+void LogOutput::write(std::string_view str)\n {\n \tswitch (target_) {\n \tcase LoggingTargetSyslog:\n@@ -275,14 +275,15 @@ void LogOutput::write(const std::string &str)\n \t}\n }\n \n-void LogOutput::writeSyslog(LogSeverity severity, const std::string &str)\n+void LogOutput::writeSyslog(LogSeverity severity, std::string_view str)\n {\n-\tsyslog(log_severity_to_syslog(severity), \"%s\", str.c_str());\n+\tsyslog(log_severity_to_syslog(severity), \"%.*s\",\n+\t       static_cast<int>(str.size()), str.data());\n }\n \n-void LogOutput::writeStream(const std::string &str)\n+void LogOutput::writeStream(std::string_view str)\n {\n-\tstream_->write(str.c_str(), str.size());\n+\tstream_->write(str.data(), str.size());\n \tstream_->flush();\n }\n \n@@ -311,7 +312,7 @@ private:\n \n \tvoid parseLogFile();\n \tvoid parseLogLevels();\n-\tstatic LogSeverity parseLogLevel(const std::string &level);\n+\tstatic LogSeverity parseLogLevel(std::string_view level);\n \n \tfriend LogCategory;\n \tvoid registerCategory(LogCategory *category);\n@@ -675,7 +676,7 @@ void Logger::parseLogLevels()\n  *\n  * \\return The log severity, or LogInvalid if the string is invalid\n  */\n-LogSeverity Logger::parseLogLevel(const std::string &level)\n+LogSeverity Logger::parseLogLevel(std::string_view level)\n {\n \tstatic const char *const names[] = {\n \t\t\"DEBUG\",\n@@ -688,9 +689,9 @@ LogSeverity Logger::parseLogLevel(const std::string &level)\n \tint severity;\n \n \tif (std::isdigit(level[0])) {\n-\t\tchar *endptr;\n-\t\tseverity = strtoul(level.c_str(), &endptr, 10);\n-\t\tif (*endptr != '\\0' || severity > LogFatal)\n+\t\tstd::from_chars_result res;\n+\t\tres = std::from_chars(level.begin(), level.end(), severity);\n+\t\tif (res.ptr != level.end() || severity > LogFatal)\n \t\t\tseverity = LogInvalid;\n \t} else {\n \t\tseverity = LogInvalid;\n@@ -874,7 +875,7 @@ const LogCategory &LogCategory::defaultCategory()\n  */\n LogMessage::LogMessage(const char *fileName, unsigned int line,\n \t\t       const LogCategory &category, LogSeverity severity,\n-\t\t       const std::string &prefix)\n+\t\t       std::string_view prefix)\n \t: category_(category), severity_(severity), prefix_(prefix)\n {\n \tinit(fileName, line);\n","prefixes":["RFC","4/8"]}