|
| 1 | +#define CATCH_CONFIG_MAIN |
| 2 | +#include <spdlog/spdlog.h> |
| 3 | + |
| 4 | +#include <catch2/catch.hpp> |
| 5 | +#include <chrono> |
| 6 | +#include <thread> |
| 7 | + |
| 8 | +#include "depthai/depthai.hpp" |
| 9 | + |
| 10 | +int test(dai::LogLevel logLevel) { |
| 11 | + // Create pipeline |
| 12 | + dai::Pipeline pipeline; |
| 13 | + |
| 14 | + auto xIn = pipeline.create<dai::node::XLinkIn>(); |
| 15 | + auto script = pipeline.create<dai::node::Script>(); |
| 16 | + auto xOut = pipeline.create<dai::node::XLinkOut>(); |
| 17 | + |
| 18 | + xIn->setStreamName("input"); |
| 19 | + xOut->setStreamName("output"); |
| 20 | + |
| 21 | + // Link xin node to script node |
| 22 | + xIn->out.link(script->inputs["log"]); |
| 23 | + script->outputs["out"].link(xOut->input); |
| 24 | + |
| 25 | + script->setScript(R"( |
| 26 | + while True: |
| 27 | + _ = node.io["log"].get() |
| 28 | + node.trace("TRACE") |
| 29 | + node.debug("DEBUG") |
| 30 | + node.info("INFO") |
| 31 | + node.warn("WARN") |
| 32 | + node.error("ERROR") |
| 33 | + node.critical("CRITICAL") |
| 34 | +
|
| 35 | + message = Buffer(10) |
| 36 | + node.io["out"].send(message) |
| 37 | + )"); |
| 38 | + |
| 39 | + dai::Device device(pipeline); |
| 40 | + |
| 41 | + auto in = device.getInputQueue("input"); |
| 42 | + auto out = device.getOutputQueue("output"); |
| 43 | + dai::Buffer message; // Arbitrary message, used only to control flow |
| 44 | + |
| 45 | + device.setLogLevel(logLevel); |
| 46 | + device.setLogOutputLevel(logLevel); |
| 47 | + |
| 48 | + // -1 below is for no_error, which cannot arrive |
| 49 | + std::array<bool, spdlog::level::n_levels - 1> arrivedLogs; |
| 50 | + for(auto& level : arrivedLogs) { |
| 51 | + level = false; |
| 52 | + } |
| 53 | + bool testPassed = true; |
| 54 | + auto logLevelConverted = static_cast<typename std::underlying_type<dai::LogLevel>::type>(logLevel); |
| 55 | + auto callbackSink = [&testPassed, &arrivedLogs, logLevelConverted](dai::LogMessage message) { |
| 56 | + // Convert message to spd for easier comparison |
| 57 | + auto messageLevelConverted = static_cast<typename std::underlying_type<dai::LogLevel>::type>(message.level); |
| 58 | + REQUIRE(messageLevelConverted >= logLevelConverted); |
| 59 | + if(messageLevelConverted < arrivedLogs.size()) { |
| 60 | + arrivedLogs[messageLevelConverted] = true; |
| 61 | + } else { |
| 62 | + FAIL(); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + device.addLogCallback(callbackSink); |
| 67 | + in->send(message); |
| 68 | + out->get(); // Wait for the device to send the log(s) |
| 69 | + using namespace std::chrono; |
| 70 | + std::this_thread::sleep_for(milliseconds(200)); // Wait for the logs to arrive |
| 71 | + |
| 72 | + for(int i = 0; i < arrivedLogs.size(); i++) { |
| 73 | + if(i < logLevelConverted) { |
| 74 | + REQUIRE(!arrivedLogs[i]); |
| 75 | + } else { |
| 76 | + REQUIRE(arrivedLogs[i]); |
| 77 | + } |
| 78 | + } |
| 79 | + device.setLogLevel(dai::LogLevel::WARN); |
| 80 | + device.setLogOutputLevel(dai::LogLevel::WARN); |
| 81 | + // Exit with success error code |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +TEST_CASE("TRACE") { |
| 86 | + test(dai::LogLevel::TRACE); |
| 87 | +} |
| 88 | + |
| 89 | +TEST_CASE("DEBUG") { |
| 90 | + test(dai::LogLevel::DEBUG); |
| 91 | +} |
| 92 | + |
| 93 | +TEST_CASE("INFO") { |
| 94 | + test(dai::LogLevel::INFO); |
| 95 | +} |
| 96 | + |
| 97 | +TEST_CASE("WARN") { |
| 98 | + test(dai::LogLevel::WARN); |
| 99 | +} |
| 100 | + |
| 101 | +TEST_CASE("ERROR") { |
| 102 | + test(dai::LogLevel::ERR); |
| 103 | +} |
| 104 | + |
| 105 | +TEST_CASE("CRITICAL") { |
| 106 | + test(dai::LogLevel::CRITICAL); |
| 107 | +} |
| 108 | + |
| 109 | +TEST_CASE("OFF") { |
| 110 | + test(dai::LogLevel::OFF); |
| 111 | +} |
0 commit comments