summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-02-03 19:17:46 +0100
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-06-06 15:09:49 +0300
commit6132521b47571d143ddb72da09c5b9896ee41925 (patch)
tree9821c3cb5ca74d95c9ab8d51ec9c85fac16a87b1
parent575703894624306950c0540182afc48ba3732f8c (diff)
libcamera: base: log: Color the log prefix
Add coloring to the log prefix to increase log readability. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Umang Jain <umang.jain@ideasonboard.com> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
-rw-r--r--include/libcamera/base/log.h5
-rw-r--r--src/libcamera/base/log.cpp38
2 files changed, 29 insertions, 14 deletions
diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h
index 3f065267..3fc5ced3 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 eefdda4b..5c359a22 100644
--- a/src/libcamera/base/log.cpp
+++ b/src/libcamera/base/log.cpp
@@ -189,6 +189,7 @@ namespace {
* https://en.wikipedia.org/wiki/ANSI_escape_code#Colors.
*/
constexpr const char *kColorReset = "\033[0m";
+constexpr const char *kColorGreen = "\033[0;32m";
constexpr const char *kColorBrightRed = "\033[1;31m";
constexpr const char *kColorBrightGreen = "\033[1;32m";
constexpr const char *kColorBrightYellow = "\033[1;33m";
@@ -215,6 +216,7 @@ void LogOutput::write(const LogMessage &msg)
const char *categoryColor = color_ ? kColorBrightWhite : "";
const char *fileColor = color_ ? kColorBrightBlue : "";
+ const char *prefixColor = color_ ? kColorGreen : "";
const char *resetColor = color_ ? kColorReset : "";
const char *severityColor = "";
LogSeverity severity = msg.severity();
@@ -230,8 +232,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:
@@ -240,8 +244,10 @@ void LogOutput::write(const LogMessage &msg)
+ std::to_string(Thread::currentId()) + "] "
+ severityColor + log_severity_name(severity) + " "
+ categoryColor + msg.category().name() + " "
- + fileColor + msg.fileInfo() + " "
- + resetColor + msg.msg();
+ + fileColor + msg.fileInfo() + " ";
+ if (!msg.prefix().empty())
+ str += prefixColor + msg.prefix() + ": ";
+ str += resetColor + msg.msg();
writeStream(str);
break;
default:
@@ -819,14 +825,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);
}
@@ -916,6 +925,12 @@ LogMessage::~LogMessage()
*/
/**
+ * \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
* \return The message text of the message, as a string
@@ -962,12 +977,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());
}
/**