{"id":22717,"url":"https://patchwork.libcamera.org/api/patches/22717/?format=json","web_url":"https://patchwork.libcamera.org/patch/22717/","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":"<20250130195811.1230581-9-pobrn@protonmail.com>","date":"2025-01-30T19:58:57","name":"[RFC,v2,8/9] libcamera: base: log: Protect log categories with lock","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"5cfc99e904645a8c483c513311da48a4c7ae3aae","submitter":{"id":133,"url":"https://patchwork.libcamera.org/api/people/133/?format=json","name":"Pőcze Barnabás","email":"pobrn@protonmail.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22717/mbox/","series":[{"id":4982,"url":"https://patchwork.libcamera.org/api/series/4982/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4982","date":"2025-01-30T19:58:14","name":"libcamera: base: log: Misc. changes","version":2,"mbox":"https://patchwork.libcamera.org/series/4982/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22717/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22717/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 A2C95BD808\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 30 Jan 2025 19:59:01 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 563F168574;\n\tThu, 30 Jan 2025 20:59:01 +0100 (CET)","from mail-40133.protonmail.ch (mail-40133.protonmail.ch\n\t[185.70.40.133])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4F5DD68564\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 30 Jan 2025 20:58:59 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=protonmail.com header.i=@protonmail.com\n\theader.b=\"enXbmsrZ\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1738267138; x=1738526338;\n\tbh=sZE2KOGbSBN3Vmzmtw3niI/Hu81qYNu7iaBrkn0Uux0=;\n\th=Date:To:From:Subject:Message-ID:In-Reply-To:References:\n\tFeedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID:\n\tMessage-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post;\n\tb=enXbmsrZ2KngK7mSvkIpe3p/Rq91zRhvQVlKaKz8GfkIaP5eNsJMUvoHXsZGQrt08\n\tLNg5ZV4BbU8BxARkU/PPuLiNFo9pfUmq7lU35j/JG1ldKKxCGXghCOhw1qQt/9TNj/\n\tTpPJfYrgbutfUVyiVvawTMSJGF/HC5rxJ5CdWgTsCcOuxS7Cvue+rLLR7He2oQ+12A\n\tepyWJYq44atQ7kHpzTvDCG5CmY2Nlk1wSTbBMb3ClJ4TmrbVNxLkFoi4yfSTibQPH3\n\tUvJm5jTcO+cCCuZeOdTafD0jCy7P1YjY5RMSeQqwXCbZcQnKnY5c6BieSSKLm33WDz\n\tBy/V66BnX+VAg==","Date":"Thu, 30 Jan 2025 19:58:57 +0000","To":"libcamera-devel@lists.libcamera.org","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Subject":"[RFC PATCH v2 8/9] libcamera: base: log: Protect log categories with\n\tlock","Message-ID":"<20250130195811.1230581-9-pobrn@protonmail.com>","In-Reply-To":"<20250130195811.1230581-1-pobrn@protonmail.com>","References":"<20250130195811.1230581-1-pobrn@protonmail.com>","Feedback-ID":"20568564:user:proton","X-Pm-Message-ID":"2e2a475dd5ae2b3250d6eb6c09094d6ee26e9c28","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Log categories may be added from any thread, so it is important\nto synchronize access to the `Logger::categories_` list between\nits two users: `Logger::findOrCreateCategory()` and `Logger::logSetLevel()`.\n\nTo achieve that, `Logger::{find,register}Category()` are merged into\na single function: `Logger::findOrCreateCategory()`; and the mutex\nfrom `LogCategory::create()` is moved into the `Logger` class.\n\nFurthermore, appropriate `MutexLocker`s are placed in\n`Logger::findOrCreateCategory()` and `Logger::logSetLevel()`.\n\nSigned-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n---\n include/libcamera/base/log.h |  2 +-\n src/libcamera/base/log.cpp   | 53 ++++++++++++------------------------\n 2 files changed, 19 insertions(+), 36 deletions(-)","diff":"diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h\nindex 1fb92603f..b32ec4516 100644\n--- a/include/libcamera/base/log.h\n+++ b/include/libcamera/base/log.h\n@@ -30,6 +30,7 @@ enum LogSeverity {\n class LogCategory\n {\n public:\n+\texplicit LogCategory(std::string_view name);\n \tstatic LogCategory *create(std::string_view name);\n \n \tconst std::string &name() const { return name_; }\n@@ -39,7 +40,6 @@ public:\n \tstatic const LogCategory &defaultCategory();\n \n private:\n-\texplicit LogCategory(std::string_view name);\n \n \tconst std::string name_;\n \ndiff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\nindex 19fc5cc67..69aa16c4c 100644\n--- a/src/libcamera/base/log.cpp\n+++ b/src/libcamera/base/log.cpp\n@@ -402,11 +402,11 @@ private:\n \tvoid parseLogFile();\n \n \tfriend LogCategory;\n-\tvoid registerCategory(LogCategory *category);\n-\tLogCategory *findCategory(std::string_view name) const;\n+\tLogCategory *findOrCreateCategory(std::string_view name);\n \n \tstatic bool destroyed_;\n \n+\tMutex mutex_;\n \tstd::vector<LogCategory *> categories_;\n \tstd::list<std::pair<std::string, LogSeverity>> levels_;\n \n@@ -657,6 +657,8 @@ void Logger::logSetLevel(const char *category, const char *level)\n \tif (severity == LogInvalid)\n \t\treturn;\n \n+\tMutexLocker locker(mutex_);\n+\n \tfor (LogCategory *c : categories_) {\n \t\tif (c->name() == category) {\n \t\t\tc->setSeverity(severity);\n@@ -707,17 +709,21 @@ void Logger::parseLogFile()\n }\n \n /**\n- * \\brief Register a log category with the logger\n- * \\param[in] category The log category\n- *\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+ * \\brief Find an existing log category with the given name or create one\n+ * \\param[in] name Name of the log category\n+ * \\return The pointer to the log category found or created\n  */\n-void Logger::registerCategory(LogCategory *category)\n+LogCategory *Logger::findOrCreateCategory(std::string_view name)\n {\n-\tcategories_.push_back(category);\n+\tMutexLocker locker(mutex_);\n+\n+\tfor (LogCategory *category : categories_) {\n+\t\tif (category->name() == name)\n+\t\t\treturn category;\n+\t}\n+\n+\tLogCategory *category = categories_.emplace_back(new LogCategory(name));\n \n-\tconst std::string &name = category->name();\n \tfor (const std::pair<std::string, LogSeverity> &level : levels_) {\n \t\tbool match = true;\n \n@@ -737,22 +743,8 @@ void Logger::registerCategory(LogCategory *category)\n \t\t\tbreak;\n \t\t}\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(std::string_view 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+\treturn category;\n }\n \n /**\n@@ -790,16 +782,7 @@ LogCategory *Logger::findCategory(std::string_view name) const\n  */\n LogCategory *LogCategory::create(std::string_view name)\n {\n-\tstatic Mutex mutex_;\n-\tMutexLocker locker(mutex_);\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+\treturn Logger::instance()->findOrCreateCategory(name);\n }\n \n /**\n","prefixes":["RFC","v2","8/9"]}