From patchwork Thu Jul 11 10:24:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 1653 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 6AD4C60C3A for ; Thu, 11 Jul 2019 12:24:29 +0200 (CEST) Received: from neptunite.amanokami.net (softbank126163157105.bbtec.net [126.163.157.105]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B147CCC; Thu, 11 Jul 2019 12:24:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1562840669; bh=5ptHZ/YjI1SlSrKlhA2PE/2eLeSICGlVS2i4vl4mA+U=; h=From:To:Cc:Subject:Date:From; b=KbrQSvj3xhc+qIkoVslSkaiSYguDr2AIdG2DvDSS3D1O6nEM9XpmYzWsO1XZw7VUB 2Ck6eUKRP0c85/8syUpUAzxI6TqrvGxjoossus5RWvLse5UZUvc9YdYCerBY8ckY6z sMx7mXZaRATAGHb77MUGS1UdFkbAYwtAoLQwek98= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Thu, 11 Jul 2019 19:24:17 +0900 Message-Id: <20190711102418.29661-1-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/2] libcamera: logging: add logging API for applications 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: Thu, 11 Jul 2019 10:24:29 -0000 Currently the log file and the log level can only be set via environment variables, but applications may also want to set the log file and the log level at run time. Provide an API for this. Signed-off-by: Paul Elder --- Changes in v2: - add documentation - actually set the log level include/libcamera/logging.h | 17 +++++++++++ include/libcamera/meson.build | 1 + src/libcamera/log.cpp | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 include/libcamera/logging.h diff --git a/include/libcamera/logging.h b/include/libcamera/logging.h new file mode 100644 index 0000000..47c5e49 --- /dev/null +++ b/include/libcamera/logging.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * log.h - Logging infrastructure + */ +#ifndef __LIBCAMERA_LOGGING_H__ +#define __LIBCAMERA_LOGGING_H__ + +namespace libcamera { + +void logSetFile(const char *file); +void logSetLevel(const char *category, const char *level); + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_LOGGING_H__ */ diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 972513f..920eb5f 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -9,6 +9,7 @@ libcamera_api = files([ 'geometry.h', 'ipa/ipa_interface.h', 'ipa/ipa_module_info.h', + 'logging.h', 'object.h', 'request.h', 'signal.h', diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp index 0ba276e..dca4936 100644 --- a/src/libcamera/log.cpp +++ b/src/libcamera/log.cpp @@ -69,6 +69,9 @@ private: void parseLogLevels(); static LogSeverity parseLogLevel(const std::string &level); + friend void logSetFile(const char *file); + friend void logSetLevel(const char *category, const char *level); + friend LogCategory; void registerCategory(LogCategory *category); void unregisterCategory(LogCategory *category); @@ -80,6 +83,57 @@ private: std::ostream *output_; }; +/** + * \brief Set the log file + * \param[in] file + */ +void logSetFile(const char *file) +{ + if (std::string(file).empty()) + return; + + Logger *logger = Logger::instance(); + std::ofstream &file_ = logger->file_; + file_.close(); + file_.open(file); + if (file_.good()) + logger->output_ = &file_; +} + +/** + * \brief Set the log level + * \param[in] category Logging category + * \param[in] level Log level + * + * This function sets the log level of \a category to \a level. + * \a level should be one of the following strings: + * - "DEBUG" + * - "INFO" + * - "WARN" + * - "ERROR" + * - "FATAL" + * + * "*" is not a valid \a category for this function. + */ +void logSetLevel(const char *category, const char *level) +{ + Logger *logger = Logger::instance(); + std::string cat(category); + std::string lev(level); + + /* Both the category and the level must be specified. */ + if (cat.empty() || lev.empty()) + return; + + LogSeverity severity = Logger::parseLogLevel(lev); + if (severity == LogInvalid) + return; + + for (LogCategory *c : logger->categories_) + if (!strcmp(c->name(), category)) + c->setSeverity(severity); +} + /** * \brief Retrieve the logger instance * From patchwork Thu Jul 11 10:24:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 1654 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 00C6B61574 for ; Thu, 11 Jul 2019 12:24:31 +0200 (CEST) Received: from neptunite.amanokami.net (softbank126163157105.bbtec.net [126.163.157.105]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 98FF3CC; Thu, 11 Jul 2019 12:24:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1562840670; bh=WMuIkKCL1z6hOX3eK3QIAspUay7E/fz69zaUpbaBWp8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Gm28taQ5akzr6RGnnoFkiBt/rkLHMYBSnKt7oHjMD5UMV57z475HDcVNUEnSJXg7d aQ5KLmf8Y7g6VOJCVSwao38CbdN3yAhjk4FS+GBvfBqWUOgfyUKqGB5oZ8ujUSt+/R OkPz8gDoVXot4kMEgmCpmaCuOPwkm1q7fn+S/iaU= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Thu, 11 Jul 2019 19:24:18 +0900 Message-Id: <20190711102418.29661-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190711102418.29661-1-paul.elder@ideasonboard.com> References: <20190711102418.29661-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/2] test: add logging API test 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: Thu, 11 Jul 2019 10:24:31 -0000 Test that setting the log file and log levels works from an application point of view. The test uses the internal logging mechanism as well, just to write to the log file. Signed-off-by: Paul Elder --- Changes in v2: - added more test cases to catch if log level changes fail test/log/log_api.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++ test/log/meson.build | 12 +++++++ test/meson.build | 1 + 3 files changed, 88 insertions(+) create mode 100644 test/log/log_api.cpp create mode 100644 test/log/meson.build diff --git a/test/log/log_api.cpp b/test/log/log_api.cpp new file mode 100644 index 0000000..395b857 --- /dev/null +++ b/test/log/log_api.cpp @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * log_api.cpp - log API test + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "log.h" +#include "test.h" + +using namespace std; +using namespace libcamera; + +LOG_DEFINE_CATEGORY(LogAPITest) + +class LogAPITest : public Test +{ +protected: + int run() override + { + int fd = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR); + if (fd < 0) { + cerr << "Failed to open tmp log file" << endl; + return TestFail; + } + + char path[PATH_MAX]; + snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd); + + logSetFile(path); + + logSetLevel("LogAPITest", "DEBUG"); + LOG(LogAPITest, Info) << "asdf"; + + logSetLevel("LogAPITest", "WARN"); + LOG(LogAPITest, Info) << "asdf"; + + logSetLevel("LogAPITest", "WARN"); + LOG(LogAPITest, Info) << "asdf"; + LOG(LogAPITest, Debug) << "asdf"; + + logSetLevel("LogAPITest", "ERROR"); + LOG(LogAPITest, Error) << "asdf"; + LOG(LogAPITest, Info) << "asdf"; + + logSetLevel("LogAPITest", "WARN"); + LOG(LogAPITest, Warning) << "asdf"; + LOG(LogAPITest, Info) << "asdf"; + + char buf[200]; + lseek(fd, 0, SEEK_SET); + read(fd, buf, 1000); + close(fd); + + std::string s(buf); + int n = count(s.begin(), s.end(), '\n'); + if (n == 3) + return TestPass; + + return TestFail; + } +}; + +TEST_REGISTER(LogAPITest) diff --git a/test/log/meson.build b/test/log/meson.build new file mode 100644 index 0000000..35ea553 --- /dev/null +++ b/test/log/meson.build @@ -0,0 +1,12 @@ +log_api_test = [ + ['log_api', 'log_api.cpp'], +] + +foreach t : log_api_test + exe = executable(t[0], t[1], + dependencies : libcamera_dep, + link_with : test_libraries, + include_directories : test_includes_internal) + + test(t[0], exe, suite : 'log') +endforeach diff --git a/test/meson.build b/test/meson.build index 60ce960..44d8495 100644 --- a/test/meson.build +++ b/test/meson.build @@ -4,6 +4,7 @@ subdir('camera') subdir('controls') subdir('ipa') subdir('ipc') +subdir('log') subdir('media_device') subdir('pipeline') subdir('stream')