[libcamera-devel] [PATCH v3 2/2] test: add logging API test

Laurent Pinchart laurent.pinchart at ideasonboard.com
Thu Jul 11 20:24:14 CEST 2019


Hi Paul,

Thank you for the patch.

On Fri, Jul 12, 2019 at 03:05:25AM +0900, Paul Elder wrote:
> 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 <paul.elder at ideasonboard.com>
> ---
> Changes in v3:
> - better testing (actually check logging output)
> - move and rename test file into main testing directory
> 
> Changes in v2:
> - added more test cases to catch if log level changes fail
> 
>  test/log.cpp     | 84 ++++++++++++++++++++++++++++++++++++++++++++++++
>  test/meson.build |  1 +
>  2 files changed, 85 insertions(+)
>  create mode 100644 test/log.cpp
> 
> diff --git a/test/log.cpp b/test/log.cpp
> new file mode 100644
> index 0000000..ffe066f
> --- /dev/null
> +++ b/test/log.cpp
> @@ -0,0 +1,84 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +/*
> + * Copyright (C) 2019, Google Inc.
> + *
> + * log_api.cpp - log API test

log.cpp

> + */
> +
> +#include <algorithm>
> +#include <fcntl.h>
> +#include <iostream>
> +#include <list>
> +#include <sstream>
> +#include <stdio.h>
> +#include <string.h>
> +#include <sys/stat.h>
> +#include <sys/types.h>
> +#include <unistd.h>
> +
> +#include <libcamera/logging.h>
> +
> +#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[32];
> +		snprintf(path, sizeof(path), "/proc/self/fd/%u", fd);
> +
> +		logSetFile(path);
> +
> +		logSetLevel("LogAPITest", "DEBUG");
> +		LOG(LogAPITest, Info) << "good 1";
> +
> +		logSetLevel("LogAPITest", "WARN");
> +		LOG(LogAPITest, Info) << "bad";
> +
> +		logSetLevel("LogAPITest", "ERROR");
> +		LOG(LogAPITest, Error) << "good 3";
> +		LOG(LogAPITest, Info) << "bad";
> +
> +		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);
> +		read(fd, buf, sizeof(buf));
> +		close(fd);
> +
> +		std::list<int> goodList = { 1, 3, 5 };
> +		std::basic_istringstream<char> iss((std::string(buf)));
> +		char str[100];
> +		while (iss.getline(str, sizeof(str))) {
> +			char tmp = std::string(str).back();
> +			int i = tmp - '0';
> +			int j = goodList.front();

		std::string line;
		while (iss.getline(line)) {
			if (goodList.empty()) {
				std::cout << "Too many log lines" << std:endl;
				return TestFail;
			}

			unsigned int digit = line.back() - '0';
			unsigned int expect = goodList.front();

> +			goodList.pop_front();
> +			if (i != j)

				std::cout << "Incorrect log line" << std:endl;

> +				return TestFail;
> +		}
> +
> +		if (!goodList.empty())

			std::cout << "Too few log lines" << std:endl;

Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>

> +			return TestFail;
> +
> +		return TestPass;
> +	}
> +};
> +
> +TEST_REGISTER(LogAPITest)
> diff --git a/test/meson.build b/test/meson.build
> index 60ce960..d308ac9 100644
> --- a/test/meson.build
> +++ b/test/meson.build
> @@ -21,6 +21,7 @@ public_tests = [
>  
>  internal_tests = [
>      ['camera-sensor',                   'camera-sensor.cpp'],
> +    ['log',                             'log.cpp'],
>      ['message',                         'message.cpp'],
>      ['signal-threads',                  'signal-threads.cpp'],
>      ['threads',                         'threads.cpp'],

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list