{"id":22608,"url":"https://patchwork.libcamera.org/api/patches/22608/?format=json","web_url":"https://patchwork.libcamera.org/patch/22608/","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":"<20250121185554.301901-5-pobrn@protonmail.com>","date":"2025-01-21T18:56:16","name":"[RFC,v1,7/7] libcamera: base: log: Protect log categories with lock","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"b3add3286440099a891a09d82e158b254e9643e2","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/22608/mbox/","series":[{"id":4960,"url":"https://patchwork.libcamera.org/api/series/4960/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4960","date":"2025-01-21T18:50:47","name":"libcamera: base: log: Misc. changes","version":1,"mbox":"https://patchwork.libcamera.org/series/4960/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22608/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22608/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 6008CBD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 21 Jan 2025 18:56:22 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 15A296855F;\n\tTue, 21 Jan 2025 19:56:22 +0100 (CET)","from mail-10631.protonmail.ch (mail-10631.protonmail.ch\n\t[79.135.106.31])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id DD38E68503\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 21 Jan 2025 19:56:19 +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=\"OSDu8I1X\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1737485778; x=1737744978;\n\tbh=tRnrGPXV3ZBCOP1M93HbbxpJB+M8In0TCeEHyNnuqxY=;\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=OSDu8I1XvLIeLBX62JZqEqk1JbNgTW571uKp/tHYyNVNDdmuq03+RBR+8RdovdJPt\n\thfx4Is7+ov8kFnHIPFQYtOUVSIW4smEDll5hNhQo12w+paimK7aH9xOpY40ZuRRJuw\n\tgQUzOtZvFHIvnEcRnAnRhG8jPrsvOX/5OJEMTNbO5/BAcMRJ8nwD+QIL9PNENHjp5m\n\tM9BIY3Usme9Qvnr8tX/4Z/FFJERw1uKs1/9Z9O0AcKgXv7x116W4aVhtB0oUcuAGXc\n\tDfIMB+WIIxOclEmNe7tJbhU7Ib636/8jOgKFGlAjDPvyY35FLTDtwsN6tH+o9AbL1N\n\tSSEF65XvrYguQ==","Date":"Tue, 21 Jan 2025 18:56:16 +0000","To":"libcamera-devel@lists.libcamera.org","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Subject":"[RFC PATCH v1 7/7] libcamera: base: log: Protect log categories with\n\tlock","Message-ID":"<20250121185554.301901-5-pobrn@protonmail.com>","In-Reply-To":"<20250121185554.301901-1-pobrn@protonmail.com>","References":"<20250121185554.301901-1-pobrn@protonmail.com>","Feedback-ID":"20568564:user:proton","X-Pm-Message-ID":"d984294653f07e4422d50e9cbdd1b3e756226cbc","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   | 235 ++++++++++++++++-------------------\n 2 files changed, 110 insertions(+), 127 deletions(-)","diff":"diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h\nindex ef161bece..58e873fa9 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 6430650ec..d2de5a80e 100644\n--- a/src/libcamera/base/log.cpp\n+++ b/src/libcamera/base/log.cpp\n@@ -66,7 +66,9 @@\n \n namespace libcamera {\n \n-static int log_severity_to_syslog(LogSeverity severity)\n+namespace {\n+\n+int log_severity_to_syslog(LogSeverity severity)\n {\n \tswitch (severity) {\n \tcase LogDebug:\n@@ -84,7 +86,7 @@ static int log_severity_to_syslog(LogSeverity severity)\n \t}\n }\n \n-static const char *log_severity_name(LogSeverity severity)\n+const char *log_severity_name(LogSeverity severity)\n {\n \tstatic const char *const names[] = {\n \t\t\"DEBUG\",\n@@ -100,6 +102,92 @@ static const char *log_severity_name(LogSeverity severity)\n \t\treturn \"UNKWN\";\n }\n \n+/**\n+ * \\brief Parse a log level string into a LogSeverity\n+ * \\param[in] level The log level string\n+ *\n+ * Log levels can be specified as an integer value in the range from LogDebug to\n+ * LogFatal, or as a string corresponding to the severity name in uppercase. Any\n+ * other value is invalid.\n+ *\n+ * \\return The log severity, or LogInvalid if the string is invalid\n+ */\n+LogSeverity parseLogLevel(std::string_view level)\n+{\n+\tstatic const char *const names[] = {\n+\t\t\"DEBUG\",\n+\t\t\"INFO\",\n+\t\t\"WARN\",\n+\t\t\"ERROR\",\n+\t\t\"FATAL\",\n+\t};\n+\n+\tunsigned int severity;\n+\n+\tif (std::isdigit(level[0])) {\n+\t\tauto [end, ec] = std::from_chars(level.data(), level.data() + level.size(), severity, 10);\n+\t\tif (ec != std::errc() || *end != '\\0' || severity > LogFatal)\n+\t\t\tseverity = LogInvalid;\n+\t} else {\n+\t\tseverity = LogInvalid;\n+\t\tfor (unsigned int i = 0; i < std::size(names); ++i) {\n+\t\t\tif (names[i] == level) {\n+\t\t\t\tseverity = i;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\treturn static_cast<LogSeverity>(severity);\n+}\n+\n+/**\n+ * \\brief Parse the log levels from the environment\n+ *\n+ * The log levels are stored in the LIBCAMERA_LOG_LEVELS environment variable\n+ * as a list of \"category:level\" pairs, separated by commas (','). Parse the\n+ * variable and store the levels to configure all log categories.\n+ */\n+void parseLogLevels(const char *line, std::list<std::pair<std::string, LogSeverity>> &levels)\n+{\n+\tfor (const char *pair = line; *line != '\\0'; pair = line) {\n+\t\tconst char *comma = strchrnul(line, ',');\n+\t\tsize_t len = comma - pair;\n+\n+\t\t/* Skip over the comma. */\n+\t\tline = *comma == ',' ? comma + 1 : comma;\n+\n+\t\t/* Skip to the next pair if the pair is empty. */\n+\t\tif (!len)\n+\t\t\tcontinue;\n+\n+\t\tstd::string_view category;\n+\t\tstd::string_view level;\n+\n+\t\tconst char *colon = static_cast<const char *>(memchr(pair, ':', len));\n+\t\tif (!colon) {\n+\t\t\t/* 'x' is a shortcut for '*:x'. */\n+\t\t\tcategory = \"*\";\n+\t\t\tlevel = std::string_view(pair, len);\n+\t\t} else {\n+\t\t\tcategory = std::string_view(pair, colon - pair);\n+\t\t\tlevel = std::string_view(colon + 1, comma - colon - 1);\n+\t\t}\n+\n+\t\t/* Both the category and the level must be specified. */\n+\t\tif (category.empty() || level.empty())\n+\t\t\tcontinue;\n+\n+\t\tLogSeverity severity = parseLogLevel(level);\n+\t\tif (severity == LogInvalid)\n+\t\t\tcontinue;\n+\n+\t\tlevels.emplace_back(category, severity);\n+\t}\n+}\n+\n+} /* namespace */\n+\n /**\n  * \\brief Log output\n  *\n@@ -313,15 +401,13 @@ private:\n \tLogger();\n \n \tvoid parseLogFile();\n-\tvoid parseLogLevels();\n-\tstatic LogSeverity parseLogLevel(std::string_view level);\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@@ -572,6 +658,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@@ -593,7 +681,9 @@ Logger::Logger()\n \tlogSetStream(&std::cerr, color);\n \n \tparseLogFile();\n-\tparseLogLevels();\n+\n+\tif (const char *debug = utils::secure_getenv(\"LIBCAMERA_LOG_LEVELS\"))\n+\t\tparseLogLevels(debug, levels_);\n }\n \n /**\n@@ -620,105 +710,21 @@ void Logger::parseLogFile()\n }\n \n /**\n- * \\brief Parse the log levels from the environment\n- *\n- * The log levels are stored in the LIBCAMERA_LOG_LEVELS environment variable\n- * as a list of \"category:level\" pairs, separated by commas (','). Parse the\n- * variable and store the levels to configure all log categories.\n- */\n-void Logger::parseLogLevels()\n-{\n-\tconst char *debug = utils::secure_getenv(\"LIBCAMERA_LOG_LEVELS\");\n-\tif (!debug)\n-\t\treturn;\n-\n-\tfor (const char *pair = debug; *debug != '\\0'; pair = debug) {\n-\t\tconst char *comma = strchrnul(debug, ',');\n-\t\tsize_t len = comma - pair;\n-\n-\t\t/* Skip over the comma. */\n-\t\tdebug = *comma == ',' ? comma + 1 : comma;\n-\n-\t\t/* Skip to the next pair if the pair is empty. */\n-\t\tif (!len)\n-\t\t\tcontinue;\n-\n-\t\tstd::string_view category;\n-\t\tstd::string_view level;\n-\n-\t\tconst char *colon = static_cast<const char *>(memchr(pair, ':', len));\n-\t\tif (!colon) {\n-\t\t\t/* 'x' is a shortcut for '*:x'. */\n-\t\t\tcategory = \"*\";\n-\t\t\tlevel = std::string_view(pair, len);\n-\t\t} else {\n-\t\t\tcategory = std::string_view(pair, colon - pair);\n-\t\t\tlevel = std::string_view(colon + 1, comma - colon - 1);\n-\t\t}\n-\n-\t\t/* Both the category and the level must be specified. */\n-\t\tif (category.empty() || level.empty())\n-\t\t\tcontinue;\n-\n-\t\tLogSeverity severity = parseLogLevel(level);\n-\t\tif (severity == LogInvalid)\n-\t\t\tcontinue;\n-\n-\t\tlevels_.emplace_back(category, severity);\n-\t}\n-}\n-\n-/**\n- * \\brief Parse a log level string into a LogSeverity\n- * \\param[in] level The log level string\n- *\n- * Log levels can be specified as an integer value in the range from LogDebug to\n- * LogFatal, or as a string corresponding to the severity name in uppercase. Any\n- * other value is invalid.\n- *\n- * \\return The log severity, or LogInvalid if the string is invalid\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-LogSeverity Logger::parseLogLevel(std::string_view level)\n+LogCategory *Logger::findOrCreateCategory(std::string_view name)\n {\n-\tstatic const char *const names[] = {\n-\t\t\"DEBUG\",\n-\t\t\"INFO\",\n-\t\t\"WARN\",\n-\t\t\"ERROR\",\n-\t\t\"FATAL\",\n-\t};\n-\n-\tunsigned int severity;\n+\tMutexLocker locker(mutex_);\n \n-\tif (std::isdigit(level[0])) {\n-\t\tauto [end, ec] = std::from_chars(level.data(), level.data() + level.size(), severity, 10);\n-\t\tif (ec != std::errc() || *end != '\\0' || severity > LogFatal)\n-\t\t\tseverity = LogInvalid;\n-\t} else {\n-\t\tseverity = LogInvalid;\n-\t\tfor (unsigned int i = 0; i < std::size(names); ++i) {\n-\t\t\tif (names[i] == level) {\n-\t\t\t\tseverity = i;\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t}\n+\tfor (LogCategory *c : categories_) {\n+\t\tif (c->name() == name)\n+\t\t\treturn c;\n \t}\n \n-\treturn static_cast<LogSeverity>(severity);\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- */\n-void Logger::registerCategory(LogCategory *category)\n-{\n-\tcategories_.push_back(category);\n+\tLogCategory *c = 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@@ -734,26 +740,12 @@ void Logger::registerCategory(LogCategory *category)\n \t\t}\n \n \t\tif (match) {\n-\t\t\tcategory->setSeverity(level.second);\n+\t\t\tc->setSeverity(level.second);\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 c;\n }\n \n /**\n@@ -791,16 +783,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","v1","7/7"]}