[libcamera-devel] [PATCH 5/5] libcamera: base: log: Color the log prefix

paul.elder at ideasonboard.com paul.elder at ideasonboard.com
Tue May 31 06:52:01 CEST 2022


Hi Laurent,

On Thu, May 26, 2022 at 01:25:03AM +0300, Laurent Pinchart via libcamera-devel wrote:
> Add coloring to the log prefix to increase log readability.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>

Reviewed-by: Paul Elder <paul.elder at ideasonboard.com>

> ---
>  include/libcamera/base/log.h |  5 ++++-
>  src/libcamera/base/log.cpp   | 37 +++++++++++++++++++++++-------------
>  2 files changed, 28 insertions(+), 14 deletions(-)
> 
> diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h
> index 3f065267c914..3fc5ced38a6a 100644
> --- a/include/libcamera/base/log.h
> +++ b/include/libcamera/base/log.h
> @@ -57,7 +57,8 @@ class LogMessage
>  {
>  public:
>  	LogMessage(const char *fileName, unsigned int line,
> -		   const LogCategory &category, LogSeverity severity);
> +		   const LogCategory &category, LogSeverity severity,
> +		   const std::string &prefix = std::string());
>  
>  	LogMessage(LogMessage &&);
>  	~LogMessage();
> @@ -68,6 +69,7 @@ public:
>  	LogSeverity severity() const { return severity_; }
>  	const LogCategory &category() const { return category_; }
>  	const std::string &fileInfo() const { return fileInfo_; }
> +	const std::string &prefix() const { return prefix_; }
>  	const std::string msg() const { return msgStream_.str(); }
>  
>  private:
> @@ -80,6 +82,7 @@ private:
>  	LogSeverity severity_;
>  	utils::time_point timestamp_;
>  	std::string fileInfo_;
> +	std::string prefix_;
>  };
>  
>  class Loggable
> diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp
> index a9f5bbbd36f7..6620a50a5b6f 100644
> --- a/src/libcamera/base/log.cpp
> +++ b/src/libcamera/base/log.cpp
> @@ -219,6 +219,7 @@ void LogOutput::write(const LogMessage &msg)
>  
>  	const char *categoryColor = color_ ? kColorBrightWhite : "";
>  	const char *fileColor = color_ ? kColorBrightBlue : "";
> +	const char *prefixColor = color_ ? kColorGreen : "";
>  	const char *msgColor = color_ ? kColorReset : "";
>  	const char *severityColor = "";
>  	LogSeverity severity = msg.severity();
> @@ -234,8 +235,10 @@ void LogOutput::write(const LogMessage &msg)
>  	switch (target_) {
>  	case LoggingTargetSyslog:
>  		str = std::string(log_severity_name(severity)) + " "
> -		    + msg.category().name() + " " + msg.fileInfo() + " "
> -		    + msg.msg();
> +		    + msg.category().name() + " " + msg.fileInfo() + " ";
> +		if (!msg.prefix().empty())
> +			str += msg.prefix() + ": ";
> +		str += msg.msg();
>  		writeSyslog(severity, str);
>  		break;
>  	case LoggingTargetStream:
> @@ -244,8 +247,10 @@ void LogOutput::write(const LogMessage &msg)
>  		    + std::to_string(Thread::currentId()) + "] "
>  		    + severityColor + log_severity_name(severity) + " "
>  		    + categoryColor + msg.category().name() + " "
> -		    + fileColor + msg.fileInfo() + " "
> -		    + msgColor + msg.msg();
> +		    + fileColor + msg.fileInfo() + " ";
> +		if (!msg.prefix().empty())
> +			str += prefixColor + msg.prefix() + ": ";
> +		str += msgColor + msg.msg();
>  		writeStream(str);
>  		break;
>  	default:
> @@ -823,14 +828,17 @@ const LogCategory &LogCategory::defaultCategory()
>   * will be displayed
>   * \param[in] severity The log message severity, controlling how the message
>   * will be displayed
> + * \param[in] prefix The log message prefix
>   *
>   * Create a log message pertaining to line \a line of file \a fileName. The
>   * \a severity argument sets the message severity to control whether it will be
> - * output or dropped.
> + * output or dropped. The \a prefix optionally identifies the object instance
> + * logging the message.
>   */
>  LogMessage::LogMessage(const char *fileName, unsigned int line,
> -		       const LogCategory &category, LogSeverity severity)
> -	: category_(category), severity_(severity)
> +		       const LogCategory &category, LogSeverity severity,
> +		       const std::string &prefix)
> +	: category_(category), severity_(severity), prefix_(prefix)
>  {
>  	init(fileName, line);
>  }
> @@ -919,6 +927,12 @@ LogMessage::~LogMessage()
>   * \return The file info of the message
>   */
>  
> +/**
> + * \fn LogMessage::prefix()
> + * \brief Retrieve the prefix of the log message
> + * \return The prefix of the message
> + */
> +
>  /**
>   * \fn LogMessage::msg()
>   * \brief Retrieve the message text of the log message
> @@ -966,12 +980,9 @@ Loggable::~Loggable()
>  LogMessage Loggable::_log(const LogCategory *category, LogSeverity severity,
>  			  const char *fileName, unsigned int line) const
>  {
> -	LogMessage msg(fileName, line,
> -		       category ? *category : LogCategory::defaultCategory(),
> -		       severity);
> -
> -	msg.stream() << logPrefix() << ": ";
> -	return msg;
> +	return LogMessage(fileName, line,
> +			  category ? *category : LogCategory::defaultCategory(),
> +			  severity, logPrefix());
>  }
>  
>  /**
> -- 
> Regards,
> 
> Laurent Pinchart
> 


More information about the libcamera-devel mailing list