[{"id":2227,"web_url":"https://patchwork.libcamera.org/comment/2227/","msgid":"<20190711182414.GQ5247@pendragon.ideasonboard.com>","date":"2019-07-11T18:24:14","subject":"Re: [libcamera-devel] [PATCH v3 2/2] test: add logging API test","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the patch.\n\nOn Fri, Jul 12, 2019 at 03:05:25AM +0900, Paul Elder wrote:\n> Test that setting the log file and log levels works from an application\n> point of view. The test uses the internal logging mechanism as well,\n> just to write to the log file.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> ---\n> Changes in v3:\n> - better testing (actually check logging output)\n> - move and rename test file into main testing directory\n> \n> Changes in v2:\n> - added more test cases to catch if log level changes fail\n> \n>  test/log.cpp     | 84 ++++++++++++++++++++++++++++++++++++++++++++++++\n>  test/meson.build |  1 +\n>  2 files changed, 85 insertions(+)\n>  create mode 100644 test/log.cpp\n> \n> diff --git a/test/log.cpp b/test/log.cpp\n> new file mode 100644\n> index 0000000..ffe066f\n> --- /dev/null\n> +++ b/test/log.cpp\n> @@ -0,0 +1,84 @@\n> +/* SPDX-License-Identifier: GPL-2.0-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * log_api.cpp - log API test\n\nlog.cpp\n\n> + */\n> +\n> +#include <algorithm>\n> +#include <fcntl.h>\n> +#include <iostream>\n> +#include <list>\n> +#include <sstream>\n> +#include <stdio.h>\n> +#include <string.h>\n> +#include <sys/stat.h>\n> +#include <sys/types.h>\n> +#include <unistd.h>\n> +\n> +#include <libcamera/logging.h>\n> +\n> +#include \"log.h\"\n> +#include \"test.h\"\n> +\n> +using namespace std;\n> +using namespace libcamera;\n> +\n> +LOG_DEFINE_CATEGORY(LogAPITest)\n> +\n> +class LogAPITest : public Test\n> +{\n> +protected:\n> +\tint run() override\n> +\t{\n> +\t\tint fd = open(\"/tmp\", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);\n> +\t\tif (fd < 0) {\n> +\t\t\tcerr << \"Failed to open tmp log file\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tchar path[32];\n> +\t\tsnprintf(path, sizeof(path), \"/proc/self/fd/%u\", fd);\n> +\n> +\t\tlogSetFile(path);\n> +\n> +\t\tlogSetLevel(\"LogAPITest\", \"DEBUG\");\n> +\t\tLOG(LogAPITest, Info) << \"good 1\";\n> +\n> +\t\tlogSetLevel(\"LogAPITest\", \"WARN\");\n> +\t\tLOG(LogAPITest, Info) << \"bad\";\n> +\n> +\t\tlogSetLevel(\"LogAPITest\", \"ERROR\");\n> +\t\tLOG(LogAPITest, Error) << \"good 3\";\n> +\t\tLOG(LogAPITest, Info) << \"bad\";\n> +\n> +\t\tlogSetLevel(\"LogAPITest\", \"WARN\");\n> +\t\tLOG(LogAPITest, Warning) << \"good 5\";\n> +\t\tLOG(LogAPITest, Info) << \"bad\";\n> +\n> +\t\tchar buf[1000];\n> +\t\tmemset(buf, 0, sizeof(buf));\n> +\t\tlseek(fd, 0, SEEK_SET);\n> +\t\tread(fd, buf, sizeof(buf));\n> +\t\tclose(fd);\n> +\n> +\t\tstd::list<int> goodList = { 1, 3, 5 };\n> +\t\tstd::basic_istringstream<char> iss((std::string(buf)));\n> +\t\tchar str[100];\n> +\t\twhile (iss.getline(str, sizeof(str))) {\n> +\t\t\tchar tmp = std::string(str).back();\n> +\t\t\tint i = tmp - '0';\n> +\t\t\tint j = goodList.front();\n\n\t\tstd::string line;\n\t\twhile (iss.getline(line)) {\n\t\t\tif (goodList.empty()) {\n\t\t\t\tstd::cout << \"Too many log lines\" << std:endl;\n\t\t\t\treturn TestFail;\n\t\t\t}\n\n\t\t\tunsigned int digit = line.back() - '0';\n\t\t\tunsigned int expect = goodList.front();\n\n> +\t\t\tgoodList.pop_front();\n> +\t\t\tif (i != j)\n\n\t\t\t\tstd::cout << \"Incorrect log line\" << std:endl;\n\n> +\t\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tif (!goodList.empty())\n\n\t\t\tstd::cout << \"Too few log lines\" << std:endl;\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\t\t\treturn TestFail;\n> +\n> +\t\treturn TestPass;\n> +\t}\n> +};\n> +\n> +TEST_REGISTER(LogAPITest)\n> diff --git a/test/meson.build b/test/meson.build\n> index 60ce960..d308ac9 100644\n> --- a/test/meson.build\n> +++ b/test/meson.build\n> @@ -21,6 +21,7 @@ public_tests = [\n>  \n>  internal_tests = [\n>      ['camera-sensor',                   'camera-sensor.cpp'],\n> +    ['log',                             'log.cpp'],\n>      ['message',                         'message.cpp'],\n>      ['signal-threads',                  'signal-threads.cpp'],\n>      ['threads',                         'threads.cpp'],","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 576F460BC8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 11 Jul 2019 20:24:43 +0200 (CEST)","from pendragon.ideasonboard.com (softbank126163157105.bbtec.net\n\t[126.163.157.105])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id DAB5031C;\n\tThu, 11 Jul 2019 20:24:41 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1562869482;\n\tbh=WIGnLPfv+1MXHFTlfApM87ZlI0dYhb1r+NWPyKQH4yo=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=rtTZUPoPK8xh0+Zj7tzW1/Qc3ZxdZMWXh24sNOU5dIO2T+MTd6HuLdagYP76Dd8ND\n\tVj8A6wxE0oflg38w4dVIsxzyXVfVfkghhcRcQCiPfXoO0xK3jZfOT5dB9inTR2i6Go\n\tPsl9VsGjCcIK0GQ5aOerPJf/0w9syj84ktvPNLyo=","Date":"Thu, 11 Jul 2019 21:24:14 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190711182414.GQ5247@pendragon.ideasonboard.com>","References":"<20190711180525.31519-1-paul.elder@ideasonboard.com>\n\t<20190711180525.31519-2-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190711180525.31519-2-paul.elder@ideasonboard.com>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v3 2/2] test: add logging API test","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":"Thu, 11 Jul 2019 18:24:43 -0000"}}]