[v1,2/2] libcamera: base: log: Move abort logic into separate type
diff mbox series

Message ID 20260507135019.231615-2-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • [v1,1/2] libcamera: base: log: Remove log level check for "Fatal" messages
Related show

Commit Message

Barnabás Pőcze May 7, 2026, 1:50 p.m. UTC
Add a `LogMessageAbortGuard` type that is templated on the severity of
the log message. If it is not fatal, the type is empty and does nothing.
However, if it is `LogFatal`, then its destructor will print the backtrace
and abort. This type is instantiated for each log message and takes care
of aborting the processing if necessary.

This allows the removal of the abort logic from `~LogMessage()` and also
makes it visible to static analysis tools that execution will not proceed
beyond a `Fatal` message.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 include/libcamera/base/log.h | 16 +++++++++++++++-
 src/libcamera/base/log.cpp   | 16 +++++++++++-----
 2 files changed, 26 insertions(+), 6 deletions(-)

Comments

Laurent Pinchart May 7, 2026, 2:05 p.m. UTC | #1
On Thu, May 07, 2026 at 03:50:19PM +0200, Barnabás Pőcze wrote:
> Add a `LogMessageAbortGuard` type that is templated on the severity of
> the log message. If it is not fatal, the type is empty and does nothing.
> However, if it is `LogFatal`, then its destructor will print the backtrace
> and abort. This type is instantiated for each log message and takes care
> of aborting the processing if necessary.
> 
> This allows the removal of the abort logic from `~LogMessage()` and also
> makes it visible to static analysis tools that execution will not proceed
> beyond a `Fatal` message.
> 
> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>

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

> ---
>  include/libcamera/base/log.h | 16 +++++++++++++++-
>  src/libcamera/base/log.cpp   | 16 +++++++++++-----
>  2 files changed, 26 insertions(+), 6 deletions(-)
> 
> diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h
> index 5cb14311e..7bd43a423 100644
> --- a/include/libcamera/base/log.h
> +++ b/include/libcamera/base/log.h
> @@ -88,6 +88,19 @@ private:
>  	std::string prefix_;
>  };
>  
> +#ifndef __DOXYGEN__
> +template<LogSeverity>
> +struct LogMessageAbortGuard {
> +};
> +
> +template<>
> +struct LogMessageAbortGuard<LogFatal> {
> +	LogMessageAbortGuard() = default;
> +	LIBCAMERA_DISABLE_COPY_AND_MOVE(LogMessageAbortGuard)
> +	[[noreturn]] ~LogMessageAbortGuard();
> +};
> +#endif
> +
>  class Loggable
>  {
>  public:
> @@ -124,7 +137,8 @@ constexpr int isLogSeverityEnabled(const LogCategory &category)
>  	switch (const auto &_logCategory = (cat);             \
>  		isLogSeverityEnabled<Log##sev>(_logCategory)) \
>  	case 1:                                               \
> -		_log(_logCategory, Log##sev).stream()
> +		(LogMessageAbortGuard<Log##sev>(),            \
> +		 _log(_logCategory, Log##sev).stream())
>  
>  #define _LOG1(severity) \
>  	_LOG(LogCategory::defaultCategory(), severity)
> diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp
> index 0921e5485..3404471fc 100644
> --- a/src/libcamera/base/log.cpp
> +++ b/src/libcamera/base/log.cpp
> @@ -869,11 +869,6 @@ LogMessage::~LogMessage()
>  	msgStream_ << std::endl;
>  
>  	logger->write(*this);
> -
> -	if (severity_ == LogSeverity::LogFatal) {
> -		logger->backtrace();
> -		std::abort();
> -	}
>  }
>  
>  /**
> @@ -922,6 +917,17 @@ LogMessage::~LogMessage()
>   * \return The message text of the message, as a string
>   */
>  
> +#ifndef __DOXYGEN__
> +LogMessageAbortGuard<LogFatal>::~LogMessageAbortGuard()
> +{
> +	Logger *logger = Logger::instance();
> +	if (logger)
> +		logger->backtrace();
> +
> +	std::abort();
> +}
> +#endif
> +
>  /**
>   * \class Loggable
>   * \brief Base class to support log message extensions

Patch
diff mbox series

diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h
index 5cb14311e..7bd43a423 100644
--- a/include/libcamera/base/log.h
+++ b/include/libcamera/base/log.h
@@ -88,6 +88,19 @@  private:
 	std::string prefix_;
 };
 
+#ifndef __DOXYGEN__
+template<LogSeverity>
+struct LogMessageAbortGuard {
+};
+
+template<>
+struct LogMessageAbortGuard<LogFatal> {
+	LogMessageAbortGuard() = default;
+	LIBCAMERA_DISABLE_COPY_AND_MOVE(LogMessageAbortGuard)
+	[[noreturn]] ~LogMessageAbortGuard();
+};
+#endif
+
 class Loggable
 {
 public:
@@ -124,7 +137,8 @@  constexpr int isLogSeverityEnabled(const LogCategory &category)
 	switch (const auto &_logCategory = (cat);             \
 		isLogSeverityEnabled<Log##sev>(_logCategory)) \
 	case 1:                                               \
-		_log(_logCategory, Log##sev).stream()
+		(LogMessageAbortGuard<Log##sev>(),            \
+		 _log(_logCategory, Log##sev).stream())
 
 #define _LOG1(severity) \
 	_LOG(LogCategory::defaultCategory(), severity)
diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp
index 0921e5485..3404471fc 100644
--- a/src/libcamera/base/log.cpp
+++ b/src/libcamera/base/log.cpp
@@ -869,11 +869,6 @@  LogMessage::~LogMessage()
 	msgStream_ << std::endl;
 
 	logger->write(*this);
-
-	if (severity_ == LogSeverity::LogFatal) {
-		logger->backtrace();
-		std::abort();
-	}
 }
 
 /**
@@ -922,6 +917,17 @@  LogMessage::~LogMessage()
  * \return The message text of the message, as a string
  */
 
+#ifndef __DOXYGEN__
+LogMessageAbortGuard<LogFatal>::~LogMessageAbortGuard()
+{
+	Logger *logger = Logger::instance();
+	if (logger)
+		logger->backtrace();
+
+	std::abort();
+}
+#endif
+
 /**
  * \class Loggable
  * \brief Base class to support log message extensions