[libcamera-devel] [PATCH 3/8] libcamera: log: Add a LogInvalid entry to LogSeverity

Laurent Pinchart laurent.pinchart at ideasonboard.com
Fri Apr 26 17:01:50 CEST 2019


enum LogSeverity values are assigned or compared to -1 to flag invalid
log severities. This generates compilation warnings with clang. Fix it
by adding an explicit LogInvalid entry to the enumeration.

Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
 src/libcamera/include/log.h |  3 ++-
 src/libcamera/log.cpp       | 14 +++++++-------
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/libcamera/include/log.h b/src/libcamera/include/log.h
index 35a6fc105448..802836d23bf2 100644
--- a/src/libcamera/include/log.h
+++ b/src/libcamera/include/log.h
@@ -12,7 +12,8 @@
 namespace libcamera {
 
 enum LogSeverity {
-	LogDebug,
+	LogInvalid = -1,
+	LogDebug = 0,
 	LogInfo,
 	LogWarning,
 	LogError,
diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp
index ebf553300f5b..0ba276e5707f 100644
--- a/src/libcamera/log.cpp
+++ b/src/libcamera/log.cpp
@@ -174,7 +174,7 @@ void Logger::parseLogLevels()
 			continue;
 
 		LogSeverity severity = parseLogLevel(level);
-		if (severity == -1)
+		if (severity == LogInvalid)
 			continue;
 
 		levels_.push_back({ category, severity });
@@ -189,7 +189,7 @@ void Logger::parseLogLevels()
  * LogFatal, or as a string corresponding to the severity name in uppercase. Any
  * other value is invalid.
  *
- * \return The log severity, or -1 if the string is invalid
+ * \return The log severity, or LogInvalid if the string is invalid
  */
 LogSeverity Logger::parseLogLevel(const std::string &level)
 {
@@ -207,9 +207,9 @@ LogSeverity Logger::parseLogLevel(const std::string &level)
 		char *endptr;
 		severity = strtoul(level.c_str(), &endptr, 10);
 		if (*endptr != '\0' || severity > LogFatal)
-			severity = -1;
+			severity = LogInvalid;
 	} else {
-		severity = -1;
+		severity = LogInvalid;
 		for (unsigned int i = 0; i < ARRAY_SIZE(names); ++i) {
 			if (names[i] == level) {
 				severity = i;
@@ -416,13 +416,13 @@ LogMessage::LogMessage(const char *fileName, unsigned int line,
  * on the compiler type and version, and optimization level, the move
  * constructor is defined even if it will likely never be called, and ensures
  * that the destructor of the \a other message will not output anything to the
- * log by setting the severity to -1.
+ * log by setting the severity to LogInvalid.
  */
 LogMessage::LogMessage(LogMessage &&other)
 	: msgStream_(std::move(other.msgStream_)), category_(other.category_),
 	  severity_(other.severity_)
 {
-	other.severity_ = static_cast<LogSeverity>(-1);
+	other.severity_ = LogInvalid;
 }
 
 void LogMessage::init(const char *fileName, unsigned int line)
@@ -445,7 +445,7 @@ void LogMessage::init(const char *fileName, unsigned int line)
 LogMessage::~LogMessage()
 {
 	/* Don't print anything if we have been moved to another LogMessage. */
-	if (severity_ == -1)
+	if (severity_ == LogInvalid)
 		return;
 
 	msgStream_ << std::endl;
-- 
Regards,

Laurent Pinchart



More information about the libcamera-devel mailing list