From patchwork Fri Jul 12 20:16: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: 1676 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1D9F861607 for ; Fri, 12 Jul 2019 22:16:32 +0200 (CEST) Received: from neptunite.amanokami.net (softbank126209254147.bbtec.net [126.209.254.147]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 6396D2B2; Fri, 12 Jul 2019 22:16:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1562962591; bh=rvkyK7edjzN2JWOT54Mw0k+Pn94QaMBEyyw+XbTYZlc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hZXxtFUzPF3Mu2mplqub5xQGBpnbO3QTDugbMxSA4a0TjH3SxFCBn9EhGP57Gru+F LnXCGj/scBTusws6Ldl07Sy0Yp7pfJhTQ7thV2qeqPW0ENhFmDOh6hO75yoaPeB6vS uW3JQgRC2LaWMr6D3tiK7b7hKifiCy87FM2iFrDs= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Sat, 13 Jul 2019 05:16:18 +0900 Message-Id: <20190712201620.30457-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190712201620.30457-1-paul.elder@ideasonboard.com> References: <20190712201620.30457-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 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: Fri, 12 Jul 2019 20:16:32 -0000 Test the new logSetStream loggin API call. Reorganize the logging API tests at the same time. logSetTarget is not tested since the nowhere and syslog logging destinations do not really need to be tested. Signed-off-by: Paul Elder --- test/log.cpp | 82 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/test/log.cpp b/test/log.cpp index 89fb5ca..c1446bd 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,19 +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; + int verifyOutput(string &str) + { + list goodList = { 1, 3, 5 }; + basic_istringstream iss(str); + string line; while (getline(iss, line)) { if (goodList.empty()) { cout << "Too many log lines" << endl; @@ -90,6 +73,57 @@ 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); + + logSetFile(path); + + 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); + + string str(buf); + return verifyOutput(str); + } + + int testStream() + { + ostringstream log; + logSetStream(log); + + doLogging(); + + string str(log.str()); + return verifyOutput(str); + } + + int run() override + { + int ret1 = testFile(); + + int ret2 = testStream(); + + if (ret1 == TestPass && ret2 == TestPass) + return TestPass; + + return TestFail; + } }; TEST_REGISTER(LogAPITest)