[libcamera-devel,01/11] libcamera: log: Add a LogFatal log level

Message ID 20190106023328.10989-1-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • [libcamera-devel,01/11] libcamera: log: Add a LogFatal log level
Related show

Commit Message

Laurent Pinchart Jan. 6, 2019, 2:33 a.m. UTC
The LogFatal log level is similar to the LogError level, but
additionally abort program execution. This is useful to implement
assertion handlers.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/libcamera/include/log.h |  2 ++
 src/libcamera/log.cpp       | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)

Comments

Niklas Söderlund Jan. 6, 2019, 11:05 a.m. UTC | #1
Hi Laurent,

Thanks for your work.

On 2019-01-06 04:33:18 +0200, Laurent Pinchart wrote:
> The LogFatal log level is similar to the LogError level, but
> additionally abort program execution. This is useful to implement
> assertion handlers.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  src/libcamera/include/log.h |  2 ++
>  src/libcamera/log.cpp       | 19 +++++++++++++++----
>  2 files changed, 17 insertions(+), 4 deletions(-)
> 
> diff --git a/src/libcamera/include/log.h b/src/libcamera/include/log.h
> index 74439848c6ca..03842be02d0e 100644
> --- a/src/libcamera/include/log.h
> +++ b/src/libcamera/include/log.h
> @@ -16,6 +16,7 @@ enum LogSeverity {
>  	LogInfo,
>  	LogWarning,
>  	LogError,
> +	LogFatal,
>  };
>  
>  class LogMessage
> @@ -30,6 +31,7 @@ public:
>  
>  private:
>  	std::ostringstream msgStream;
> +	LogSeverity severity_;
>  };
>  
>  #define LOG(severity) LogMessage(__FILE__, __LINE__, Log##severity).stream()
> diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp
> index 44b3a5bbe279..a5823c64eaa6 100644
> --- a/src/libcamera/log.cpp
> +++ b/src/libcamera/log.cpp
> @@ -6,6 +6,7 @@
>   */
>  
>  #include <cstdio>
> +#include <cstdlib>
>  #include <ctime>
>  #include <iomanip>
>  #include <string.h>
> @@ -31,6 +32,8 @@ namespace libcamera {
>   * Warning message, signals a potential issue
>   * \var LogError
>   * Error message, signals an unrecoverable issue
> + * \var LogFatal
> + * Fatal message, signals an unrecoverable issue and aborts execution
>   */
>  
>  /**
> @@ -40,15 +43,19 @@ namespace libcamera {
>   * Return an std::ostream reference to which a message can be logged using the
>   * iostream API. The \a severity controls whether the message is printed or
>   * dropped, depending on the global log level.
> + *
> + * If the severity is set to Fatal, execution is aborted and the program
> + * terminates immediately after printing the message.
>   */
>  
>  static const char *log_severity_name(LogSeverity severity)
>  {
>  	static const char * const names[] = {
> -		" DBG",
> -		"INFO",
> -		"WARN",
> -		" ERR",
> +		"  DBG",
> +		" INFO",
> +		" WARN",
> +		"  ERR",
> +		"FATAL",
>  	};
>  
>  	if (static_cast<unsigned int>(severity) < ARRAY_SIZE(names))
> @@ -73,6 +80,7 @@ static const char *log_severity_name(LogSeverity severity)
>   */
>  LogMessage::LogMessage(const char *fileName, unsigned int line,
>  		       LogSeverity severity)
> +	: severity_(severity)
>  {
>  	/* Log the timestamp, severity and file information. */
>  	struct timespec timestamp;
> @@ -93,6 +101,9 @@ LogMessage::~LogMessage()
>  	std::string msg(msgStream.str());
>  	fwrite(msg.data(), msg.size(), 1, stderr);
>  	fflush(stderr);
> +
> +	if (severity_ == LogSeverity::LogFatal)
> +		std::abort();
>  }
>  
>  /**
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/src/libcamera/include/log.h b/src/libcamera/include/log.h
index 74439848c6ca..03842be02d0e 100644
--- a/src/libcamera/include/log.h
+++ b/src/libcamera/include/log.h
@@ -16,6 +16,7 @@  enum LogSeverity {
 	LogInfo,
 	LogWarning,
 	LogError,
+	LogFatal,
 };
 
 class LogMessage
@@ -30,6 +31,7 @@  public:
 
 private:
 	std::ostringstream msgStream;
+	LogSeverity severity_;
 };
 
 #define LOG(severity) LogMessage(__FILE__, __LINE__, Log##severity).stream()
diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp
index 44b3a5bbe279..a5823c64eaa6 100644
--- a/src/libcamera/log.cpp
+++ b/src/libcamera/log.cpp
@@ -6,6 +6,7 @@ 
  */
 
 #include <cstdio>
+#include <cstdlib>
 #include <ctime>
 #include <iomanip>
 #include <string.h>
@@ -31,6 +32,8 @@  namespace libcamera {
  * Warning message, signals a potential issue
  * \var LogError
  * Error message, signals an unrecoverable issue
+ * \var LogFatal
+ * Fatal message, signals an unrecoverable issue and aborts execution
  */
 
 /**
@@ -40,15 +43,19 @@  namespace libcamera {
  * Return an std::ostream reference to which a message can be logged using the
  * iostream API. The \a severity controls whether the message is printed or
  * dropped, depending on the global log level.
+ *
+ * If the severity is set to Fatal, execution is aborted and the program
+ * terminates immediately after printing the message.
  */
 
 static const char *log_severity_name(LogSeverity severity)
 {
 	static const char * const names[] = {
-		" DBG",
-		"INFO",
-		"WARN",
-		" ERR",
+		"  DBG",
+		" INFO",
+		" WARN",
+		"  ERR",
+		"FATAL",
 	};
 
 	if (static_cast<unsigned int>(severity) < ARRAY_SIZE(names))
@@ -73,6 +80,7 @@  static const char *log_severity_name(LogSeverity severity)
  */
 LogMessage::LogMessage(const char *fileName, unsigned int line,
 		       LogSeverity severity)
+	: severity_(severity)
 {
 	/* Log the timestamp, severity and file information. */
 	struct timespec timestamp;
@@ -93,6 +101,9 @@  LogMessage::~LogMessage()
 	std::string msg(msgStream.str());
 	fwrite(msg.data(), msg.size(), 1, stderr);
 	fflush(stderr);
+
+	if (severity_ == LogSeverity::LogFatal)
+		std::abort();
 }
 
 /**