libcamera: log: Allow extending log messages

Introduce a base Loggable class that can be inherited from by other
classes to support adding per-instance information to the log messages.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
This commit is contained in:
Laurent Pinchart
2019-02-08 16:40:57 +02:00
parent d8f2ed7d0d
commit 8a401ed411
2 changed files with 145 additions and 2 deletions
+21 -2
View File
@@ -54,6 +54,7 @@ public:
LogMessage(const char *fileName, unsigned int line,
const LogCategory &category, LogSeverity severity);
LogMessage(const LogMessage &) = delete;
LogMessage(LogMessage &&);
~LogMessage();
std::ostream &stream() { return msgStream_; }
@@ -66,13 +67,31 @@ private:
LogSeverity severity_;
};
class Loggable
{
public:
virtual ~Loggable();
protected:
virtual std::string logPrefix() const = 0;
LogMessage _log(const char *file, unsigned int line,
LogSeverity severity);
LogMessage _log(const char *file, unsigned int line,
const LogCategory &category, LogSeverity severity);
};
LogMessage _log(const char *file, unsigned int line, LogSeverity severity);
LogMessage _log(const char *file, unsigned int line,
const LogCategory &category, LogSeverity severity);
#ifndef __DOXYGEN__
#define _LOG_CATEGORY(name) logCategory##name
#define _LOG1(severity) \
LogMessage(__FILE__, __LINE__, Log##severity).stream()
_log(__FILE__, __LINE__, Log##severity).stream()
#define _LOG2(category, severity) \
LogMessage(__FILE__, __LINE__, _LOG_CATEGORY(category)(), Log##severity).stream()
_log(__FILE__, __LINE__, _LOG_CATEGORY(category)(), Log##severity).stream()
/*
* Expand the LOG() macro to _LOG1() or _LOG2() based on the number of