[{"id":2225,"web_url":"https://patchwork.libcamera.org/comment/2225/","msgid":"<20190711161107.GN5247@pendragon.ideasonboard.com>","date":"2019-07-11T16:11:07","subject":"Re: [libcamera-devel] [PATCH v2 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 Thu, Jul 11, 2019 at 07:24:18PM +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 v2:\n> - added more test cases to catch if log level changes fail\n> \n>  test/log/log_api.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++\n>  test/log/meson.build | 12 +++++++\n>  test/meson.build     |  1 +\n\nThere's no need to create a directory for a single test, you can move it\nto test/, and I would even call it log.cpp for now.\n\n>  3 files changed, 88 insertions(+)\n>  create mode 100644 test/log/log_api.cpp\n>  create mode 100644 test/log/meson.build\n> \n> diff --git a/test/log/log_api.cpp b/test/log/log_api.cpp\n> new file mode 100644\n> index 0000000..395b857\n> --- /dev/null\n> +++ b/test/log/log_api.cpp\n> @@ -0,0 +1,75 @@\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> + */\n> +\n> +#include <algorithm>\n> +#include <fcntl.h>\n> +#include <iostream>\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[PATH_MAX];\n\nThat's a bit long, 32 will largely suffice.\n\n> +\t\tsnprintf(path, PATH_MAX, \"/proc/self/fd/%d\", fd);\n\ns/PATH_MAX/sizeof(path)/\ns/%d/%u/ as we know the number is positive\n\n> +\n> +\t\tlogSetFile(path);\n> +\n> +\t\tlogSetLevel(\"LogAPITest\", \"DEBUG\");\n> +\t\tLOG(LogAPITest, Info) << \"asdf\";\n> +\n> +\t\tlogSetLevel(\"LogAPITest\", \"WARN\");\n> +\t\tLOG(LogAPITest, Info) << \"asdf\";\n> +\n> +\t\tlogSetLevel(\"LogAPITest\", \"WARN\");\n> +\t\tLOG(LogAPITest, Info) << \"asdf\";\n\nThose two lines don't appear to do anything more than the two previous\nones.\n\n> +\t\tLOG(LogAPITest, Debug) << \"asdf\";\n> +\n> +\t\tlogSetLevel(\"LogAPITest\", \"ERROR\");\n> +\t\tLOG(LogAPITest, Error) << \"asdf\";\n> +\t\tLOG(LogAPITest, Info) << \"asdf\";\n> +\n> +\t\tlogSetLevel(\"LogAPITest\", \"WARN\");\n> +\t\tLOG(LogAPITest, Warning) << \"asdf\";\n> +\t\tLOG(LogAPITest, Info) << \"asdf\";\n\nI would print a sequence of numbers instead of \"asdf\", and verify that\nthe expected numbers are printed. This will be easier if you read the\nwhole file (in a bigger array than 200 bytes) and wrap the buffer in an\nistringstream\n(https://en.cppreference.com/w/cpp/io/basic_istringstream). You can then\ncall getline() in a loop and get the last character of each line.\n\n> +\n> +\t\tchar buf[200];\n> +\t\tlseek(fd, 0, SEEK_SET);\n> +\t\tread(fd, buf, 1000);\n> +\t\tclose(fd);\n> +\n> +\t\tstd::string s(buf);\n> +\t\tint n = count(s.begin(), s.end(), '\\n');\n> +\t\tif (n == 3)\n> +\t\t\treturn TestPass;\n> +\n> +\t\treturn TestFail;\n> +\t}\n> +};\n> +\n> +TEST_REGISTER(LogAPITest)\n> diff --git a/test/log/meson.build b/test/log/meson.build\n> new file mode 100644\n> index 0000000..35ea553\n> --- /dev/null\n> +++ b/test/log/meson.build\n> @@ -0,0 +1,12 @@\n> +log_api_test = [\n> +    ['log_api', 'log_api.cpp'],\n> +]\n> +\n> +foreach t : log_api_test\n> +    exe = executable(t[0], t[1],\n> +                     dependencies : libcamera_dep,\n> +                     link_with : test_libraries,\n> +                     include_directories : test_includes_internal)\n> +\n> +    test(t[0], exe, suite : 'log')\n> +endforeach\n> diff --git a/test/meson.build b/test/meson.build\n> index 60ce960..44d8495 100644\n> --- a/test/meson.build\n> +++ b/test/meson.build\n> @@ -4,6 +4,7 @@ subdir('camera')\n>  subdir('controls')\n>  subdir('ipa')\n>  subdir('ipc')\n> +subdir('log')\n>  subdir('media_device')\n>  subdir('pipeline')\n>  subdir('stream')","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1FF5A60C3A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 11 Jul 2019 18:11:36 +0200 (CEST)","from pendragon.ideasonboard.com (softbank126163157105.bbtec.net\n\t[126.163.157.105])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A5B7F31C;\n\tThu, 11 Jul 2019 18:11:34 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1562861495;\n\tbh=0pfVkT/8k+FU3TN2EQhcpadaMDevmayWteVnr359Dms=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=nvmLc7pPfRm+ghkvnkf9Iq19P9cw8B80uHGSgbxVGHVJ7flhStcKDdu//UgUYb8C/\n\taUOuFTt5cTUTY46YgCNoFE3J7s/Wb244lXIdI5vf5yt6ObSIhh2HjdJ9QlHe0fK742\n\toXV61lNxbAU+7AFJo7iX24zjKH9WMHWyXd3tGJOU=","Date":"Thu, 11 Jul 2019 19:11:07 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190711161107.GN5247@pendragon.ideasonboard.com>","References":"<20190711102418.29661-1-paul.elder@ideasonboard.com>\n\t<20190711102418.29661-2-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190711102418.29661-2-paul.elder@ideasonboard.com>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [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":"<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 16:11:36 -0000"}}]