From patchwork Tue Jul 16 07:05:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 1708 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 673EE61AA8 for ; Tue, 16 Jul 2019 09:05:21 +0200 (CEST) Received: from emerald.amanokami.net (unknown [IPv6:2a00:79e1:abc:3602:b57a:2dda:be67:ac6e]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1C6C6564; Tue, 16 Jul 2019 09:05:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1563260721; bh=vodG6dCzfOrde6CqMZrEEa+sgZMXVDKMDloBFibbjUg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hMvpUtnMa93apSTey7/s6wETbOUsGwldCbveabwl7UxxhaOd+W5aTyzNlMr9dlcrT UNQStuhkTbD4ZCso9Zim7pdIbU4PH41hEMc8tQV7HxIwugBDAHHtN7No5L/1FLXhtn mxu4IFfcX+Z1upEh3MVCkPBHDXxA2OU2cfP9Sg44= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Tue, 16 Jul 2019 16:05:06 +0900 Message-Id: <20190716070508.24589-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190716070508.24589-1-paul.elder@ideasonboard.com> References: <20190716070508.24589-1-paul.elder@ideasonboard.com> Subject: [libcamera-devel] [PATCH v2 2/4] test: logging: add logSetStream 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: Tue, 16 Jul 2019 07:05:21 -0000 Test the new logSetStream logging API call. Reorganize the logging API tests at the same time. logSetTarget for the syslog logging destination is not tested. Signed-off-by: Paul Elder Reviewed-by: Laurent Pinchart --- Changes in v2: - add testing for logSetTarget, to test that writing to none doesn't crash, and to test that setting to file or stream fail - make verifyOutput() take an istream (instead of a string) - make total test passage checking nicer test/log.cpp | 106 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 25 deletions(-) diff --git a/test/log.cpp b/test/log.cpp index 89fb5ca..33622f8 100644 --- a/test/log.cpp +++ b/test/log.cpp @@ -29,19 +29,8 @@ LOG_DEFINE_CATEGORY(LogAPITest) class LogAPITest : public Test { protected: - int run() override + void doLogging() { - 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[32]; - snprintf(path, sizeof(path), "/proc/self/fd/%u", fd); - - logSetFile(path); - logSetLevel("LogAPITest", "DEBUG"); LOG(LogAPITest, Info) << "good 1"; @@ -55,20 +44,13 @@ protected: logSetLevel("LogAPITest", "WARN"); LOG(LogAPITest, Warning) << "good 5"; LOG(LogAPITest, Info) << "bad"; + } - char buf[1000]; - memset(buf, 0, sizeof(buf)); - lseek(fd, 0, SEEK_SET); - if (read(fd, buf, sizeof(buf)) < 0) { - cerr << "Failed to read tmp log file" << endl; - return TestFail; - } - close(fd); - - std::list goodList = { 1, 3, 5 }; - std::basic_istringstream iss((std::string(buf))); - std::string line; - while (getline(iss, line)) { + int verifyOutput(istream &is) + { + list goodList = { 1, 3, 5 }; + string line; + while (getline(is, line)) { if (goodList.empty()) { cout << "Too many log lines" << endl; return TestFail; @@ -90,6 +72,80 @@ protected: return TestPass; } + + int testFile() + { + 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[32]; + snprintf(path, sizeof(path), "/proc/self/fd/%u", fd); + + if (logSetFile(path) < 0) { + cerr << "Failed to set log file" << endl; + return TestFail; + } + + doLogging(); + + char buf[1000]; + memset(buf, 0, sizeof(buf)); + lseek(fd, 0, SEEK_SET); + if (read(fd, buf, sizeof(buf)) < 0) { + cerr << "Failed to read tmp log file" << endl; + return TestFail; + } + close(fd); + + istringstream iss(buf); + return verifyOutput(iss); + } + + int testStream() + { + stringstream log; + /* Never fails, so no need to check return value */ + logSetStream(&log); + + doLogging(); + + return verifyOutput(log); + } + + int testTarget() + { + logSetTarget(LoggingTargetNone); + logSetLevel("LogAPITest", "DEBUG"); + LOG(LogAPITest, Info) << "don't crash please"; + + if (!logSetTarget(LoggingTargetFile)) + return TestFail; + + if (!logSetTarget(LoggingTargetStream)) + return TestFail; + + return TestPass; + } + + int run() override + { + int ret = testFile(); + if (ret != TestPass) + return TestFail; + + ret = testStream(); + if (ret != TestPass) + return TestFail; + + ret = testTarget(); + if (ret != TestPass) + return TestFail; + + return TestPass; + } }; TEST_REGISTER(LogAPITest)