[RFC,v1,2/7] libcamera: base: log: Use `std::from_chars()`
diff mbox series

Message ID 20250121185044.300816-3-pobrn@protonmail.com
State Superseded
Headers show
Series
  • libcamera: base: log: Misc. changes
Related show

Commit Message

Barnabás Pőcze Jan. 21, 2025, 6:50 p.m. UTC
Use the `std::from_chars()` function from `<charconv>` to
parse the integral log level instead of `strtoul` as it
provides an easier to use interface and better type safety.

Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>
---
 src/libcamera/base/log.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Laurent Pinchart Jan. 24, 2025, 4:57 p.m. UTC | #1
Hi Barnabás,

Thank you for the patch.

On Tue, Jan 21, 2025 at 06:50:58PM +0000, Barnabás Pőcze wrote:
> Use the `std::from_chars()` function from `<charconv>` to
> parse the integral log level instead of `strtoul` as it
> provides an easier to use interface and better type safety.

Nitpicking, you can reflow your commit messages up to 72 columns.

> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>
> ---
>  src/libcamera/base/log.cpp | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp
> index 61a43d525..1024fed7f 100644
> --- a/src/libcamera/base/log.cpp
> +++ b/src/libcamera/base/log.cpp
> @@ -8,6 +8,7 @@
>  #include <libcamera/base/log.h>
>  
>  #include <array>
> +#include <charconv>
>  #include <fstream>
>  #include <iostream>
>  #include <list>
> @@ -686,12 +687,11 @@ LogSeverity Logger::parseLogLevel(const std::string &level)
>  		"FATAL",
>  	};
>  
> -	int severity;
> +	unsigned int severity;
>  
>  	if (std::isdigit(level[0])) {
> -		char *endptr;
> -		severity = strtoul(level.c_str(), &endptr, 10);
> -		if (*endptr != '\0' || severity > LogFatal)
> +		auto [end, ec] = std::from_chars(level.data(), level.data() + level.size(), severity, 10);

Here it would be nice to shorten the string :-)

		auto [end, ec] = std::from_chars(level.data(), level.data() + level.size(),
						 severity, 10);

You can also drop the last argument, as it's the default.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> +		if (ec != std::errc() || *end != '\0' || severity > LogFatal)
>  			severity = LogInvalid;
>  	} else {
>  		severity = LogInvalid;
>

Patch
diff mbox series

diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp
index 61a43d525..1024fed7f 100644
--- a/src/libcamera/base/log.cpp
+++ b/src/libcamera/base/log.cpp
@@ -8,6 +8,7 @@ 
 #include <libcamera/base/log.h>
 
 #include <array>
+#include <charconv>
 #include <fstream>
 #include <iostream>
 #include <list>
@@ -686,12 +687,11 @@  LogSeverity Logger::parseLogLevel(const std::string &level)
 		"FATAL",
 	};
 
-	int severity;
+	unsigned int severity;
 
 	if (std::isdigit(level[0])) {
-		char *endptr;
-		severity = strtoul(level.c_str(), &endptr, 10);
-		if (*endptr != '\0' || severity > LogFatal)
+		auto [end, ec] = std::from_chars(level.data(), level.data() + level.size(), severity, 10);
+		if (ec != std::errc() || *end != '\0' || severity > LogFatal)
 			severity = LogInvalid;
 	} else {
 		severity = LogInvalid;