summaryrefslogtreecommitdiff
path: root/test/log.cpp
diff options
context:
space:
mode:
authorPaul Elder <paul.elder@ideasonboard.com>2019-07-11 18:29:59 +0900
committerPaul Elder <paul.elder@ideasonboard.com>2019-07-12 13:46:48 +0900
commita25c937f8afee766d3001c488a19acc9a8aac2dd (patch)
tree84ef17317e96fe518b4afdf74ff1e262a3d92443 /test/log.cpp
parent60f5d472d96c9c1ffcb1887bd760bcee3c09ec2a (diff)
test: add logging API test
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@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'test/log.cpp')
-rw-r--r--test/log.cpp92
1 files changed, 92 insertions, 0 deletions
diff --git a/test/log.cpp b/test/log.cpp
new file mode 100644
index 00000000..bc64325c
--- /dev/null
+++ b/test/log.cpp
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * log.cpp - log API test
+ */
+
+#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)));
+ std::string line;
+ while (getline(iss, line)) {
+ if (goodList.empty()) {
+ cout << "Too many log lines" << endl;
+ return TestFail;
+ }
+
+ unsigned int digit = line.back() - '0';
+ unsigned int expect = goodList.front();
+ goodList.pop_front();
+ if (digit != expect) {
+ cout << "Incorrect log line" << endl;
+ return TestFail;
+ }
+ }
+
+ if (!goodList.empty()) {
+ cout << "Too few log lines" << endl;
+ return TestFail;
+ }
+
+ return TestPass;
+ }
+};
+
+TEST_REGISTER(LogAPITest)