From patchwork Mon Jan 21 19:56:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 314 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 96D8C60C80 for ; Mon, 21 Jan 2019 20:56:12 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 363B0598 for ; Mon, 21 Jan 2019 20:56:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1548100572; bh=ZBIQlpmFV6blC8Lxb42ENmxmm8zQx0Kl4Dg2Hl+zhrU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=dLLVtAD+5LjPFZYBI9uogPHuP9a1UDHKNx5nLQxfWiQFHXPw8yGrHZdDR2Dp9vHoe ck93rW61kcPORYW8If5TEk+BxKUmr85xwgJ4J1FS01fep9x5/uEud+XuIYp4sBBBMM al3juM64ZYX5QIrcc3zhFy2rQ3rZFnFoC7g/3vaw= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 21 Jan 2019 21:56:05 +0200 Message-Id: <20190121195606.8526-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190121195606.8526-1-laurent.pinchart@ideasonboard.com> References: <20190121195606.8526-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/4] libcamera: log: Get log output file from the environment X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Jan 2019 19:56:12 -0000 If the LIBCAMERA_LOG_FILE environment variable is set, open the file it points to and redirect the logger output to it. Signed-off-by: Laurent Pinchart --- src/libcamera/log.cpp | 47 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp index 23bb9edb9d14..241dc5395976 100644 --- a/src/libcamera/log.cpp +++ b/src/libcamera/log.cpp @@ -8,7 +8,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -37,6 +39,12 @@ * message with a level higher than or equal to the configured log level for * their category are output to the log, while other messages are silently * discarded. + * + * By default log messages are output to stderr. They can be redirected to a log + * file by setting the LIBCAMERA_LOG_FILE environment variable to the name of + * the file. The file must be writable and is truncated if it exists. If any + * error occurs when opening the file, the file is ignored and the log is output + * to stderr. */ namespace libcamera { @@ -51,9 +59,12 @@ class Logger public: static Logger *instance(); + void write(const std::string &msg); + private: Logger(); + void parseLogFile(); void parseLogLevels(); static LogSeverity parseLogLevel(const std::string &level); @@ -63,6 +74,9 @@ private: std::unordered_set categories_; std::list> levels_; + + std::ofstream file_; + std::ostream *output_; }; /** @@ -79,14 +93,44 @@ Logger *Logger::instance() return &instance; } +/** + * \brief Write a message to the configured logger output + * \param[in] msg The message string + */ +void Logger::write(const std::string &msg) +{ + output_->write(msg.c_str(), msg.size()); + output_->flush(); +} + /** * \brief Construct a logger */ Logger::Logger() + : output_(&std::cerr) { + parseLogFile(); parseLogLevels(); } +/** + * \brief Parse the log output file from the environment + * + * If the LIBCAMERA_LOG_FILE environment variable is set, open the file it + * points to and redirect the logger output to it. Errors are silently ignored + * and don't affect the logger output (set to stderr). + */ +void Logger::parseLogFile() +{ + const char *file = secure_getenv("LIBCAMERA_LOG_FILE"); + if (!file) + return; + + file_.open(file); + if (file_.good()) + output_ = &file_; +} + /** * \brief Parse the log levels from the environment * @@ -384,8 +428,7 @@ LogMessage::~LogMessage() if (severity_ >= category_.severity()) { std::string msg(msgStream_.str()); - fwrite(msg.data(), msg.size(), 1, stderr); - fflush(stderr); + Logger::instance()->write(msg); } if (severity_ == LogSeverity::LogFatal)