{"id":285,"url":"https://patchwork.libcamera.org/api/1.1/patches/285/?format=json","web_url":"https://patchwork.libcamera.org/patch/285/","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":"<20190121005930.10112-6-laurent.pinchart@ideasonboard.com>","date":"2019-01-21T00:59:29","name":"[libcamera-devel,5/6] libcamera: log: Get log output file from the environment","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"a36d1576ff0d826c91d56b464d564f726246b1bd","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/285/mbox/","series":[{"id":96,"url":"https://patchwork.libcamera.org/api/1.1/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/285/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/285/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 4DF8260C82\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 21 Jan 2019 01:59:36 +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 E40BAD80\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=1548032376;\n\tbh=vsAxlJR9u+NKGSOLJzHa2NWn1kqMCcBH9/tflkbD8zE=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=JosVeN9UQ2tvI/iQCj55jlIpXDDa2S+UMh+mm23XYFT4bkJONTGeLXBghsfNoggCP\n\th9QUgJ+hm11pRTojq9SBu9wXGaGjR6z6k01X0ear6Snx0kD48ByIl2QH0fUBJ3TsCj\n\tJ1JrqpUNrE2D7c3lmM5u4bjo1Q+sszFMpkzIlVeM=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 21 Jan 2019 02:59:29 +0200","Message-Id":"<20190121005930.10112-6-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 5/6] libcamera: log: Get log output file\n\tfrom the environment","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:37 -0000"},"content":"If the LIBCAMERA_LOG_FILE environment variable is set, open the file it\npoints to and redirect the logger output to it.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/log.cpp | 47 +++++++++++++++++++++++++++++++++++++++++--\n 1 file changed, 45 insertions(+), 2 deletions(-)","diff":"diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp\nindex 143029f19826..805612d3e17e 100644\n--- a/src/libcamera/log.cpp\n+++ b/src/libcamera/log.cpp\n@@ -8,7 +8,9 @@\n #include <cstdio>\n #include <cstdlib>\n #include <ctime>\n+#include <fstream>\n #include <iomanip>\n+#include <iostream>\n #include <list>\n #include <string.h>\n #include <unordered_set>\n@@ -37,6 +39,12 @@\n  * message with a level higher than or equal to the configured log level for\n  * their category are output to the log, while other messages are silently\n  * discarded.\n+ *\n+ * By default log messages are output to stderr. They can be redirected to a log\n+ * file by setting the LIBCAMERA_LOG_FILE environment variable to the name of\n+ * the file. The file must be writable and is truncated if it exists. If any\n+ * error occurs when opening the file, the file is ignored and the log is output\n+ * to stderr.\n  */\n \n namespace libcamera {\n@@ -51,9 +59,12 @@ class Logger\n public:\n \tstatic Logger *instance();\n \n+\tvoid write(const std::string &msg);\n+\n private:\n \tLogger();\n \n+\tvoid parseLogFile();\n \tvoid parseLogLevels();\n \tstatic LogSeverity parseLogLevel(const std::string &level);\n \n@@ -63,6 +74,9 @@ private:\n \n \tstd::unordered_set<LogCategory *> categories_;\n \tstd::list<std::pair<std::string, LogSeverity>> levels_;\n+\n+\tstd::ofstream file_;\n+\tstd::ostream *output_;\n };\n \n /**\n@@ -79,14 +93,44 @@ Logger *Logger::instance()\n \treturn &instance;\n }\n \n+/**\n+ * \\brief Write a message to the configured logger output\n+ * \\param[in] msg The message string\n+ */\n+void Logger::write(const std::string &msg)\n+{\n+\toutput_->write(msg.c_str(), msg.size());\n+\toutput_->flush();\n+}\n+\n /**\n  * \\brief Construct a logger\n  */\n Logger::Logger()\n+\t: output_(&std::cerr)\n {\n+\tparseLogFile();\n \tparseLogLevels();\n }\n \n+/**\n+ * \\brief Parse the log output file from the environment\n+ *\n+ * If the LIBCAMERA_LOG_FILE environment variable is set, open the file it\n+ * points to and redirect the logger output to it. Errors are silently ignored\n+ * and don't affect the logger output (set to stderr).\n+ */\n+void Logger::parseLogFile()\n+{\n+\tconst char *file = secure_getenv(\"LIBCAMERA_LOG_FILE\");\n+\tif (!file)\n+\t\treturn;\n+\n+\tfile_.open(file);\n+\tif (file_.good())\n+\t\toutput_ = &file_;\n+}\n+\n /**\n  * \\brief Parse the log levels from the environment\n  *\n@@ -384,8 +428,7 @@ LogMessage::~LogMessage()\n \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\tLogger::instance()->write(msg);\n \t}\n \n \tif (severity_ == LogSeverity::LogFatal)\n","prefixes":["libcamera-devel","5/6"]}