{"id":283,"url":"https://patchwork.libcamera.org/api/patches/283/?format=json","web_url":"https://patchwork.libcamera.org/patch/283/","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":"<20190121005930.10112-4-laurent.pinchart@ideasonboard.com>","date":"2019-01-21T00:59:27","name":"[libcamera-devel,3/6] libcamera: log: Add log categories","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"21dbeb32433f24e00c8cc7f84e6f881519beddbe","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/283/mbox/","series":[{"id":96,"url":"https://patchwork.libcamera.org/api/series/96/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=96","date":"2019-01-21T00:59:24","name":"Extend the logger with categories and configuration","version":1,"mbox":"https://patchwork.libcamera.org/series/96/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/283/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/283/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AFA3F60C82\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 21 Jan 2019 01:59:35 +0100 (CET)","from pendragon.bb.dnainternet.fi\n\t(dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi\n\t[IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4E2B8D80\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 21 Jan 2019 01:59:35 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1548032375;\n\tbh=mEuK0o21xBH6Uqfcuz+k921mCyRQmJY5dm7UkPI1dl8=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=kDtY3MpO/0Euag3EzHI+eHspAxDXsLBzySJRWho9izCWzv8yZ4+HJl9IkImmz+ZFm\n\tJTkOXzzyB/exgjJ1oJ85M8qhWYjoDwbf+SDI3l2b+nVqyp1YHXq+k2fNFqwpezQafj\n\tiCQB4TNkpEPysd6d+3Bx+OMNyghp9n+BD3teZyGQ=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 21 Jan 2019 02:59:27 +0200","Message-Id":"<20190121005930.10112-4-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.19.2","In-Reply-To":"<20190121005930.10112-1-laurent.pinchart@ideasonboard.com>","References":"<20190121005930.10112-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 3/6] libcamera: log: Add log categories","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Mon, 21 Jan 2019 00:59:36 -0000"},"content":"Log categories are used to group log messages by topic. Introduce\nsupport for categories with a new LOGC() macro. Support for configuring\nlog level per category will be introduced in a subsequent commit.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/include/log.h |  45 +++++++-\n src/libcamera/log.cpp       | 217 ++++++++++++++++++++++++++++++------\n 2 files changed, 222 insertions(+), 40 deletions(-)","diff":"diff --git a/src/libcamera/include/log.h b/src/libcamera/include/log.h\nindex cc3f9404a3d4..c7644291724d 100644\n--- a/src/libcamera/include/log.h\n+++ b/src/libcamera/include/log.h\n@@ -19,26 +19,63 @@ enum LogSeverity {\n \tLogFatal,\n };\n \n+class LogCategory\n+{\n+public:\n+\texplicit LogCategory(const char *name);\n+\t~LogCategory();\n+\n+\tconst char *name() const { return name_; }\n+\tLogSeverity severity() const { return severity_; }\n+\tvoid setSeverity(LogSeverity severity);\n+\n+\tstatic const LogCategory &defaultCategory();\n+\n+private:\n+\tconst char *name_;\n+\tLogSeverity severity_;\n+};\n+\n+#define LOG_CATEGORY(name) logCategory##name\n+\n+#define LOG_DECLARE_CATEGORY(name)\t\t\t\t\t\\\n+extern const LogCategory &LOG_CATEGORY(name)();\n+\n+#define LOG_DEFINE_CATEGORY(name)\t\t\t\t\t\\\n+const LogCategory &LOG_CATEGORY(name)()\t\t\t\t\t\\\n+{\t\t\t\t\t\t\t\t\t\\\n+\tstatic LogCategory category(#name);\t\t\t\t\\\n+\treturn category;\t\t\t\t\t\t\\\n+}\n+\n class LogMessage\n {\n public:\n \tLogMessage(const char *fileName, unsigned int line,\n \t\t   LogSeverity severity);\n+\tLogMessage(const char *fileName, unsigned int line,\n+\t\t   const LogCategory &category, LogSeverity severity);\n \tLogMessage(const LogMessage &) = delete;\n \t~LogMessage();\n \n-\tstd::ostream &stream() { return msgStream; }\n+\tstd::ostream &stream() { return msgStream_; }\n \n private:\n-\tstd::ostringstream msgStream;\n+\tvoid init(const char *fileName, unsigned int line);\n+\n+\tstd::ostringstream msgStream_;\n+\tconst LogCategory &category_;\n \tLogSeverity severity_;\n };\n \n-#define LOG(severity) LogMessage(__FILE__, __LINE__, Log##severity).stream()\n+#define LOG(severity) \\\n+\tLogMessage(__FILE__, __LINE__, Log##severity).stream()\n+#define LOGC(category, severity) \\\n+\tLogMessage(__FILE__, __LINE__, LOG_CATEGORY(category)(), Log##severity).stream()\n \n #ifndef NDEBUG\n #define ASSERT(condition) static_cast<void>(({\t\t\t\t\\\n-\tif (!(condition))\t\t\t\t\t\t\t\\\n+\tif (!(condition))\t\t\t\t\t\t\\\n \t\tLOG(Fatal) << \"assertion \\\"\" #condition \"\\\" failed\";\t\\\n }))\n #else\ndiff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp\nindex 74cba383363d..8f3d1c181245 100644\n--- a/src/libcamera/log.cpp\n+++ b/src/libcamera/log.cpp\n@@ -37,32 +37,64 @@ namespace libcamera {\n  */\n \n /**\n- * \\def LOG(severity)\n- * \\brief Log a message\n+ * \\class LogCategory\n+ * \\brief A category of log message\n  *\n- * Return an std::ostream reference to which a message can be logged using the\n- * iostream API. The \\a severity controls whether the message is printed or\n- * dropped, depending on the global log level.\n- *\n- * If the severity is set to Fatal, execution is aborted and the program\n- * terminates immediately after printing the message.\n+ * The LogCategory class represents a category of log messages, related to an\n+ * area of the library. It groups all messages belonging to the same category,\n+ * and is used to control the log level per group.\n  */\n \n /**\n- * \\def ASSERT(condition)\n- * \\brief Abort program execution if assertion fails\n+ * \\brief Construct a log category\n+ * \\param[in] name The category name\n+ */\n+LogCategory::LogCategory(const char *name)\n+\t: name_(name), severity_(LogSeverity::LogInfo)\n+{\n+}\n+\n+LogCategory::~LogCategory()\n+{\n+}\n+\n+/**\n+ * \\fn LogCategory::name()\n+ * \\brief Retrieve the log category name\n+ * \\return The log category name\n+ */\n+\n+/**\n+ * \\fn LogCategory::severity()\n+ * \\brief Retrieve the severity of the log category\n+ * \\sa setSeverity()\n+ * \\return Return the severity of the log category\n+ */\n+\n+/**\n+ * \\brief Set the severity of the log category\n  *\n- * If \\a condition is false, ASSERT() logs an error message with the Fatal log\n- * level and aborts program execution.\n+ * Messages of severity higher than or equal to the severity of the log category\n+ * are printed, other messages are discarded.\n+ */\n+void LogCategory::setSeverity(LogSeverity severity)\n+{\n+\tseverity_ = severity;\n+}\n+\n+/**\n+ * \\brief Retrieve the default log category\n  *\n- * If the macro NDEBUG is defined before including log.h, ASSERT() generates no\n- * code.\n+ * The default log category is named \"default\" and is used by the LOG() macro\n+ * when no log category is specified.\n  *\n- * Using conditions that have side effects with ASSERT() is not recommended, as\n- * these effects would depend on whether NDEBUG is defined or not. Similarly,\n- * ASSERT() should not be used to check for errors that can occur under normal\n- * conditions as those checks would then be removed when compiling with NDEBUG.\n+ * \\return A pointer to the default log category\n  */\n+const LogCategory &LogCategory::defaultCategory()\n+{\n+\tstatic LogCategory category(\"default\");\n+\treturn category;\n+}\n \n static const char *log_severity_name(LogSeverity severity)\n {\n@@ -90,10 +122,11 @@ static const char *log_severity_name(LogSeverity severity)\n  */\n \n /**\n- * \\param fileName The file name where the message is logged from\n- * \\param line The line number where the message is logged from\n- * \\param severity The log message severity, controlling how the message will be\n- * displayed\n+ * \\brief Construct a log message for the default category\n+ * \\param[in] fileName The file name where the message is logged from\n+ * \\param[in] line The line number where the message is logged from\n+ * \\param[in] severity The log message severity, controlling how the message\n+ * will be displayed\n  *\n  * Create a log message pertaining to line \\a line of file \\a fileName. The\n  * \\a severity argument sets the message severity to control whether it will be\n@@ -101,29 +134,57 @@ static const char *log_severity_name(LogSeverity severity)\n  */\n LogMessage::LogMessage(const char *fileName, unsigned int line,\n \t\t       LogSeverity severity)\n-\t: severity_(severity)\n+\t: category_(LogCategory::defaultCategory()), severity_(severity)\n+{\n+\tinit(fileName, line);\n+}\n+\n+/**\n+ * \\brief Construct a log message for a given category\n+ * \\param[in] fileName The file name where the message is logged from\n+ * \\param[in] line The line number where the message is logged from\n+ * \\param[in] category The log message category, controlling how the message\n+ * will be displayed\n+ * \\param[in] severity The log message severity, controlling how the message\n+ * will be displayed\n+ *\n+ * Create a log message pertaining to line \\a line of file \\a fileName. The\n+ * \\a severity argument sets the message severity to control whether it will be\n+ * output or dropped.\n+ */\n+LogMessage::LogMessage(const char *fileName, unsigned int line,\n+\t\t       const LogCategory &category, LogSeverity severity)\n+\t: category_(category), severity_(severity)\n+{\n+\tinit(fileName, line);\n+}\n+\n+void LogMessage::init(const char *fileName, unsigned int line)\n {\n \t/* Log the timestamp, severity and file information. */\n \tstruct timespec timestamp;\n \tclock_gettime(CLOCK_MONOTONIC, &timestamp);\n-\tmsgStream.fill('0');\n-\tmsgStream << \"[\" << timestamp.tv_sec / (60 * 60) << \":\"\n-\t\t  << std::setw(2) << (timestamp.tv_sec / 60) % 60 << \":\"\n-\t\t  << std::setw(2) << timestamp.tv_sec % 60 << \".\"\n-\t\t  << std::setw(9) << timestamp.tv_nsec << \"]\";\n-\tmsgStream.fill(' ');\n-\n-\tmsgStream << \" \" << log_severity_name(severity);\n-\tmsgStream << \" \" << basename(fileName) << \":\" << line << \" \";\n+\tmsgStream_.fill('0');\n+\tmsgStream_ << \"[\" << timestamp.tv_sec / (60 * 60) << \":\"\n+\t\t   << std::setw(2) << (timestamp.tv_sec / 60) % 60 << \":\"\n+\t\t   << std::setw(2) << timestamp.tv_sec % 60 << \".\"\n+\t\t   << std::setw(9) << timestamp.tv_nsec << \"]\";\n+\tmsgStream_.fill(' ');\n+\n+\tmsgStream_ << \" \" << log_severity_name(severity_);\n+\tmsgStream_ << \" \" << category_.name();\n+\tmsgStream_ << \" \" << basename(fileName) << \":\" << line << \" \";\n }\n \n LogMessage::~LogMessage()\n {\n-\tmsgStream << std::endl;\n+\tmsgStream_ << std::endl;\n \n-\tstd::string msg(msgStream.str());\n-\tfwrite(msg.data(), msg.size(), 1, stderr);\n-\tfflush(stderr);\n+\tif (severity_ >= category_.severity()) {\n+\t\tstd::string msg(msgStream_.str());\n+\t\tfwrite(msg.data(), msg.size(), 1, stderr);\n+\t\tfflush(stderr);\n+\t}\n \n \tif (severity_ == LogSeverity::LogFatal)\n \t\tstd::abort();\n@@ -139,4 +200,88 @@ LogMessage::~LogMessage()\n  * \\return A reference to the log message stream\n  */\n \n+/**\n+ * \\def LOG_CATEGORY(name)\n+ * \\brief Name of the log category symbol for a category name\n+ */\n+\n+/**\n+ * \\def LOG_DECLARE_CATEGORY(name)\n+ * \\brief Declare a category of log messages\n+ *\n+ * This macro is used to declare a log category defined in another compilation\n+ * unit by the LOG_DEFINE_CATEGORY() macro.\n+ *\n+ * The LOG_DECLARE_CATEGORY() macro must be used in the libcamera namespace.\n+ *\n+ * \\sa LogCategory\n+ */\n+\n+/**\n+ * \\def LOG_DECLARE_CATEGORY(name)\n+ * \\brief Declare a category of log messages\n+ *\n+ * This macro is used to declare a log category defined in another compilation\n+ * unit by the LOG_DEFINE_CATEGORY() macro.\n+ *\n+ * The LOG_DECLARE_CATEGORY() macro must be used in the libcamera namespace.\n+ *\n+ * \\sa LogCategory\n+ */\n+\n+/**\n+ * \\def LOG_DEFINE_CATEGORY(name)\n+ * \\brief Define a category of log messages\n+ *\n+ * This macro is used to define a log category that can then be used with the\n+ * LOGC() macro. Category names shall be unique, if a category is shared between\n+ * compilation units, it shall be defined in one compilation unit only and\n+ * declared with LOG_DECLARE_CATEGORY() in the other compilation units.\n+ *\n+ * The LOG_DEFINE_CATEGORY() macro must be used in the libcamera namespace.\n+ *\n+ * \\sa LogCategory\n+ */\n+\n+/**\n+ * \\def LOG(severity)\n+ * \\brief Log a message\n+ *\n+ * Return an std::ostream reference to which a message can be logged using the\n+ * iostream API. The \\a severity controls whether the message is printed or\n+ * discarded, depending on the global log level.\n+ *\n+ * If the severity is set to Fatal, execution is aborted and the program\n+ * terminates immediately after printing the message.\n+ */\n+\n+/**\n+ * \\def LOGC(category, severity)\n+ * \\brief Log a message with a category\n+ *\n+ * Return an std::ostream reference to which a message can be logged using the\n+ * iostream API. The \\a category and \\a severity controls whether the message is\n+ * printed or discarded, depending on if the \\a category is enabled and on the\n+ * log level for the \\a category.\n+ *\n+ * If the severity is set to Fatal, execution is aborted and the program\n+ * terminates immediately after printing the message.\n+ */\n+\n+/**\n+ * \\def ASSERT(condition)\n+ * \\brief Abort program execution if assertion fails\n+ *\n+ * If \\a condition is false, ASSERT() logs an error message with the Fatal log\n+ * level and aborts program execution.\n+ *\n+ * If the macro NDEBUG is defined before including log.h, ASSERT() generates no\n+ * code.\n+ *\n+ * Using conditions that have side effects with ASSERT() is not recommended, as\n+ * these effects would depend on whether NDEBUG is defined or not. Similarly,\n+ * ASSERT() should not be used to check for errors that can occur under normal\n+ * conditions as those checks would then be removed when compiling with NDEBUG.\n+ */\n+\n } /* namespace libcamera */\n","prefixes":["libcamera-devel","3/6"]}