From ab2a10f00757a3defbf027591a696d06a45c835c Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Mon, 29 Aug 2022 11:44:45 +0300 Subject: libcamera: base: log: Fix LogCategory creation issues Each declaration of a LogCategory will create a new LogCategory, and will be stored in an unordered_set Logger::categories_. This means that when a plugin .so is unloaded and loaded, as happens when destructing and creating a CamereManager, we'll get duplicate categories. The Logger::registerCategory docs say "Log categories must have unique names. If a category with the same name already exists this function performs no operation.". The code does not comply with this. We solve the issue with two changes: Change the unordered_set to a vector for simplicity, as there's no need for an unordered_set. Instead of using the LogCategory constructor to create new categories in _LOG_CATEGORY() macro, use a factory method. The factory method will return either an existing LogCategory if one exists with the given name, or a newly created one. Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham Signed-off-by: Laurent Pinchart --- include/libcamera/base/log.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 8b462767..dcaacbe0 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -29,7 +29,7 @@ enum LogSeverity { class LogCategory { public: - explicit LogCategory(const char *name); + static LogCategory *create(const char *name); const std::string &name() const { return name_; } LogSeverity severity() const { return severity_; } @@ -38,6 +38,8 @@ public: static const LogCategory &defaultCategory(); private: + explicit LogCategory(const char *name); + const std::string name_; LogSeverity severity_; }; @@ -49,7 +51,7 @@ extern const LogCategory &_LOG_CATEGORY(name)(); const LogCategory &_LOG_CATEGORY(name)() \ { \ /* The instance will be deleted by the Logger destructor. */ \ - static LogCategory *category = new LogCategory(#name); \ + static LogCategory *category = LogCategory::create(#name); \ return *category; \ } -- cgit v1.2.1