diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-01-20 15:01:49 +0200 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-01-21 21:47:17 +0200 |
commit | 63675b6dcfc18d156ab293e2bf1d924d25b14ba3 (patch) | |
tree | 5909ab6e05c89e383d564a09f7780b05ac33045d /src/libcamera/include/log.h | |
parent | 5c85ef58833ee9866a3280d0dac6b285bc9ab6ad (diff) |
libcamera: log: Add log categories
Log categories are used to group log messages by topic. Introduce
support for categories by making the LOG() macro variadic. Support for
configuring log level per category will be introduced in a subsequent
commit.
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'src/libcamera/include/log.h')
-rw-r--r-- | src/libcamera/include/log.h | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/src/libcamera/include/log.h b/src/libcamera/include/log.h index cc3f9404..ad8f8452 100644 --- a/src/libcamera/include/log.h +++ b/src/libcamera/include/log.h @@ -19,26 +19,74 @@ enum LogSeverity { LogFatal, }; +class LogCategory +{ +public: + explicit LogCategory(const char *name); + ~LogCategory(); + + const char *name() const { return name_; } + LogSeverity severity() const { return severity_; } + void setSeverity(LogSeverity severity); + + static const LogCategory &defaultCategory(); + +private: + const char *name_; + LogSeverity severity_; +}; + +#define LOG_DECLARE_CATEGORY(name) \ +extern const LogCategory &_LOG_CATEGORY(name)(); + +#define LOG_DEFINE_CATEGORY(name) \ +const LogCategory &_LOG_CATEGORY(name)() \ +{ \ + static LogCategory category(#name); \ + return category; \ +} + class LogMessage { public: LogMessage(const char *fileName, unsigned int line, LogSeverity severity); + LogMessage(const char *fileName, unsigned int line, + const LogCategory &category, LogSeverity severity); LogMessage(const LogMessage &) = delete; ~LogMessage(); - std::ostream &stream() { return msgStream; } + std::ostream &stream() { return msgStream_; } private: - std::ostringstream msgStream; + void init(const char *fileName, unsigned int line); + + std::ostringstream msgStream_; + const LogCategory &category_; LogSeverity severity_; }; -#define LOG(severity) LogMessage(__FILE__, __LINE__, Log##severity).stream() +#ifndef __DOXYGEN__ +#define _LOG_CATEGORY(name) logCategory##name + +#define _LOG1(severity) \ + LogMessage(__FILE__, __LINE__, Log##severity).stream() +#define _LOG2(category, severity) \ + LogMessage(__FILE__, __LINE__, _LOG_CATEGORY(category)(), Log##severity).stream() + +/* + * Expand the LOG() macro to _LOG1() or _LOG2() based on the number of + * arguments. + */ +#define _LOG_MACRO(_1, _2, NAME, ...) NAME +#define LOG(...) _LOG_MACRO(__VA_ARGS__, _LOG2, _LOG1)(__VA_ARGS__) +#else /* __DOXYGEN___ */ +#define LOG(category, severity) +#endif /* __DOXYGEN__ */ #ifndef NDEBUG #define ASSERT(condition) static_cast<void>(({ \ - if (!(condition)) \ + if (!(condition)) \ LOG(Fatal) << "assertion \"" #condition "\" failed"; \ })) #else |