{"id":17205,"url":"https://patchwork.libcamera.org/api/1.1/patches/17205/?format=json","web_url":"https://patchwork.libcamera.org/patch/17205/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220826113931.59323-3-tomi.valkeinen@ideasonboard.com>","date":"2022-08-26T11:39:31","name":"[libcamera-devel,2/2] libcamera: base: log: Fix LogCategory creation issues","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"c474bd4ddcdbaf12a88e3d4dfe22c2a78163906a","submitter":{"id":109,"url":"https://patchwork.libcamera.org/api/1.1/people/109/?format=json","name":"Tomi Valkeinen","email":"tomi.valkeinen@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17205/mbox/","series":[{"id":3437,"url":"https://patchwork.libcamera.org/api/1.1/series/3437/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3437","date":"2022-08-26T11:39:29","name":"LogCategory fixing","version":1,"mbox":"https://patchwork.libcamera.org/series/3437/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17205/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17205/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 87F7AC0DA4\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 26 Aug 2022 11:39:47 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4293361FC6;\n\tFri, 26 Aug 2022 13:39:47 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8727561FA2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 26 Aug 2022 13:39:43 +0200 (CEST)","from deskari.lan (91-158-154-79.elisa-laajakaista.fi\n\t[91.158.154.79])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 1DB592B3;\n\tFri, 26 Aug 2022 13:39:43 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1661513987;\n\tbh=TO/Vx+RzC7G8vuR5uPw3FpEv0EXcr+Qx4fZmWen4Z0k=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=h6R8FdXR7Vi2AylcGABaS5f9OlYzKOVZFT+6mGAZ5rgpyCHPQ1HBmhvjrQYcG6A/x\n\tfL/GgIyZyZEp/r0dFl0+DcpZ+phIUsGfGNmEBTRVNNlCg4TqOZTNwKUgkh0y0X+0mo\n\tRgYvoK8RXx90ErDYJERTEUosEetvjk+zaMe4n+khtysKvcqaDPsSgdPv0L6BAHNfmg\n\tYZhbvyDjFz7WjNLkU2zMjowRCf0CDWoecpfol5CuTD+1/pDRw5iLkY2bXh/m6nl5VU\n\tlJ//UI+uFdxPVwog1QQXbaNuJHy8CbkD6XBWn8rTvF8r75D+x8UXffq099IkAn65l2\n\t+6OZG90LalMeg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1661513983;\n\tbh=TO/Vx+RzC7G8vuR5uPw3FpEv0EXcr+Qx4fZmWen4Z0k=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=Ig4f7onRZOvOvy+JgHVpU/b+mvfzgiJ/qfjDpTkHKt/K3qtNk23XukANyCR/msGdZ\n\tQQhoOG/EAuPRdAFC25eOg9tPd+CReFTGsXeN/CkyoG0buxphZK76eyAVzhngaXQ7Jz\n\tHTBXhTWkGQ864leVj7GKVWbtJCSfslBj2Nmvr8p4="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Ig4f7onR\"; dkim-atps=neutral","To":"libcamera-devel <libcamera-devel@lists.libcamera.org>","Date":"Fri, 26 Aug 2022 14:39:31 +0300","Message-Id":"<20220826113931.59323-3-tomi.valkeinen@ideasonboard.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20220826113931.59323-1-tomi.valkeinen@ideasonboard.com>","References":"<20220826113931.59323-1-tomi.valkeinen@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 2/2] libcamera: base: log: Fix LogCategory\n\tcreation issues","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Tomi Valkeinen via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Each declaration of a LogCategory will create a new LogCategory, and\nwill be stored in an unordered_set Logger::categories_. This means that\nwhen a plugin .so is unloaded and loaded, as happens when destructing\nand creating a CamereManager, we'll get duplicate categories.\n\nThe Logger::registerCategory docs say \"Log categories must have unique\nnames. If a category with the same name already exists this function\nperforms no operation.\". The code does not comply with this.\n\nWe solve the issue with two changes:\n\nChange the unordered_set to a vector for simplicity, as there's no need\nfor an unordered_set.\n\nInstead of using the LogCategory constructor to create new categories in\n_LOG_CATEGORY() macro, use a factory method. The factory method will\nreturn either an existing LogCategory if one exists with the given name,\nor a newly created one.\n\nSigned-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n---\n include/libcamera/base/log.h |  6 +++--\n src/libcamera/base/log.cpp   | 51 +++++++++++++++++++++++++++++++-----\n 2 files changed, 48 insertions(+), 9 deletions(-)","diff":"diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h\nindex 8b462767..76e01118 100644\n--- a/include/libcamera/base/log.h\n+++ b/include/libcamera/base/log.h\n@@ -29,7 +29,7 @@ enum LogSeverity {\n class LogCategory\n {\n public:\n-\texplicit LogCategory(const char *name);\n+\tstatic LogCategory *create(const std::string &name);\n \n \tconst std::string &name() const { return name_; }\n \tLogSeverity severity() const { return severity_; }\n@@ -38,6 +38,8 @@ public:\n \tstatic const LogCategory &defaultCategory();\n \n private:\n+\texplicit LogCategory(const std::string &name);\n+\n \tconst std::string name_;\n \tLogSeverity severity_;\n };\n@@ -49,7 +51,7 @@ extern const LogCategory &_LOG_CATEGORY(name)();\n const LogCategory &_LOG_CATEGORY(name)()\t\t\t\t\\\n {\t\t\t\t\t\t\t\t\t\\\n \t/* The instance will be deleted by the Logger destructor. */\t\\\n-\tstatic LogCategory *category = new LogCategory(#name);\t\t\\\n+\tstatic LogCategory *category = LogCategory::create(#name);\t\\\n \treturn *category;\t\t\t\t\t\t\\\n }\n \ndiff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\nindex a4a5b452..c6500e96 100644\n--- a/src/libcamera/base/log.cpp\n+++ b/src/libcamera/base/log.cpp\n@@ -314,10 +314,11 @@ private:\n \n \tfriend LogCategory;\n \tvoid registerCategory(LogCategory *category);\n+\tLogCategory *findCategory(const std::string &name) const;\n \n \tstatic bool destroyed_;\n \n-\tstd::unordered_set<LogCategory *> categories_;\n+\tstd::vector<LogCategory *> categories_;\n \tstd::list<std::pair<std::string, LogSeverity>> levels_;\n \n \tstd::shared_ptr<LogOutput> output_;\n@@ -707,12 +708,14 @@ LogSeverity Logger::parseLogLevel(const std::string &level)\n  * \\brief Register a log category with the logger\n  * \\param[in] category The log category\n  *\n- * Log categories must have unique names. If a category with the same name\n- * already exists this function performs no operation.\n+ * Log categories must have unique names. It is illegal to call this function\n+ * if a log category with the same name already exists.\n  */\n void Logger::registerCategory(LogCategory *category)\n {\n-\tcategories_.insert(category);\n+\tASSERT(!findCategory(category->name()));\n+\n+\tcategories_.push_back(category);\n \n \tconst std::string &name = category->name();\n \tfor (const std::pair<std::string, LogSeverity> &level : levels_) {\n@@ -736,6 +739,21 @@ void Logger::registerCategory(LogCategory *category)\n \t}\n }\n \n+/**\n+ * \\brief Find an existing log category with the given name\n+ * \\param[in] name The name of the log category\n+ */\n+LogCategory *Logger::findCategory(const std::string &name) const\n+{\n+\tif (auto it = std::find_if(categories_.begin(), categories_.end(),\n+\t\t\t\t   [&name](auto c) { return name == c->name(); });\n+\t    it != categories_.end()) {\n+\t\treturn *it;\n+\t}\n+\n+\treturn nullptr;\n+}\n+\n /**\n  * \\enum LogSeverity\n  * Log message severity\n@@ -760,14 +778,33 @@ void Logger::registerCategory(LogCategory *category)\n  * and is used to control the log level per group.\n  */\n \n+/**\n+ * \\brief Create a new LogCategory or return an existing one\n+ * \\return The pointer to the LogCategory\n+ *\n+ * Create and return a new LogCategory with the given name if such a category\n+ * does not yet exist, or return the existing one.\n+ */\n+LogCategory *LogCategory::create(const std::string &name)\n+{\n+\tLogCategory *category = Logger::instance()->findCategory(name);\n+\n+\tif (!category) {\n+\t\tcategory = new LogCategory(name);\n+\n+\t\tLogger::instance()->registerCategory(category);\n+\t}\n+\n+\treturn category;\n+}\n+\n /**\n  * \\brief Construct a log category\n  * \\param[in] name The category name\n  */\n-LogCategory::LogCategory(const char *name)\n+LogCategory::LogCategory(const std::string &name)\n \t: name_(name), severity_(LogSeverity::LogInfo)\n {\n-\tLogger::instance()->registerCategory(this);\n }\n \n /**\n@@ -804,7 +841,7 @@ void LogCategory::setSeverity(LogSeverity severity)\n  */\n const LogCategory &LogCategory::defaultCategory()\n {\n-\tstatic const LogCategory *category = new LogCategory(\"default\");\n+\tstatic const LogCategory *category = LogCategory::create(\"default\");\n \treturn *category;\n }\n \n","prefixes":["libcamera-devel","2/2"]}