{"id":17219,"url":"https://patchwork.libcamera.org/api/patches/17219/?format=json","web_url":"https://patchwork.libcamera.org/patch/17219/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20220829084445.61585-3-tomi.valkeinen@ideasonboard.com>","date":"2022-08-29T08:44:45","name":"[libcamera-devel,v2,2/2] libcamera: base: log: Fix LogCategory creation issues","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"c0a5fca8e10dd5f1e0f42fc0d11e4f3486fe3ab9","submitter":{"id":109,"url":"https://patchwork.libcamera.org/api/people/109/?format=json","name":"Tomi Valkeinen","email":"tomi.valkeinen@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17219/mbox/","series":[{"id":3442,"url":"https://patchwork.libcamera.org/api/series/3442/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3442","date":"2022-08-29T08:44:43","name":"LogCategory fixing","version":2,"mbox":"https://patchwork.libcamera.org/series/3442/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17219/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17219/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 79555C327D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 29 Aug 2022 08:45:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 07E2E61FC7;\n\tMon, 29 Aug 2022 10:45:02 +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 D044F61F9F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Aug 2022 10:44:57 +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 5A53B481;\n\tMon, 29 Aug 2022 10:44:57 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1661762702;\n\tbh=I057OMLs4150pBj6/72SFrnIhQGH3+aIBAlQ9u4drzU=;\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=vL/kHv7CUgLpW+S62uH6IyDq/AMIYcPCKCyuDGpygj7I057p/R6VWAr4XQS1USg2h\n\tNSCi9eLbz5eIcTioLnIwRNLBr1HH1my+OePkbhTuSU2S9Q23W5SRPdKrgl/OBpA5QY\n\tZT/bd55wKesps2mtMdEzP2jlt3en9nY+xc3QxGUE9nMSZMHRFjzPsDxE7AoYYQW23b\n\t4CAKK7hZsM9bXSG2kQPd6aUIoonVtoVHL5vahr/7ZkNWPKQFpGYfQHOdOV1ZqGQYcb\n\twUwQ5FDtCY3b1XqjCzQDa8rnE3n5/Dgg8FZccOWjSpn0SAze48PUZ8xPw9PaVDPDZ9\n\tZ4lVAPhQhOWUg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1661762697;\n\tbh=I057OMLs4150pBj6/72SFrnIhQGH3+aIBAlQ9u4drzU=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=Hz6VAkYBxiDPTlnkhaq/iySoSsUHSMV2CxegqcYXdnhFqEEWHY3xJpLbCmL7FRrIo\n\tCfEKygvQickLBho7V9fjvZ1XEdtkzM1X44pA4tglktkrBjAy9uxEragpqlcZih4U0R\n\tlkWRdWpQ27eCshuyDgyXKwNB+LQDGwGJSxaEna0I="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Hz6VAkYB\"; dkim-atps=neutral","To":"libcamera-devel <libcamera-devel@lists.libcamera.org>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>","Date":"Mon, 29 Aug 2022 11:44:45 +0300","Message-Id":"<20220829084445.61585-3-tomi.valkeinen@ideasonboard.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20220829084445.61585-1-tomi.valkeinen@ideasonboard.com>","References":"<20220829084445.61585-1-tomi.valkeinen@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 2/2] libcamera: base: log: Fix\n\tLogCategory creation 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   | 49 +++++++++++++++++++++++++++++++-----\n 2 files changed, 47 insertions(+), 8 deletions(-)","diff":"diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h\nindex 8b462767..dcaacbe0 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 char *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 char *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..8bf1beb9 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 char *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,12 @@ 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 invalid 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+\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 +737,22 @@ 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 Name of the log category\n+ * \\return The pointer to the found log category or nullptr if not found.\n+ */\n+LogCategory *Logger::findCategory(const char *name) const\n+{\n+\tif (auto it = std::find_if(categories_.begin(), categories_.end(),\n+\t\t\t\t   [name](auto c) { return c->name() == 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,6 +777,27 @@ 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+ * \\param[in] name Name of the log category\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+ * \\return The pointer to the LogCategory\n+ */\n+LogCategory *LogCategory::create(const char *name)\n+{\n+\tLogCategory *category = Logger::instance()->findCategory(name);\n+\n+\tif (!category) {\n+\t\tcategory = new LogCategory(name);\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@@ -767,7 +805,6 @@ void Logger::registerCategory(LogCategory *category)\n LogCategory::LogCategory(const char *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","v2","2/2"]}