logging_test.cpp
Go to the documentation of this file.
1 #include <spdlog/spdlog.h>
2 
3 #include <catch2/catch_all.hpp>
4 #include <chrono>
5 #include <thread>
6 
7 #include "depthai/depthai.hpp"
8 
9 int test(dai::LogLevel logLevel) {
10  // Create pipeline
11  dai::Pipeline pipeline;
12 
13  auto xIn = pipeline.create<dai::node::XLinkIn>();
14  auto script = pipeline.create<dai::node::Script>();
15  auto xOut = pipeline.create<dai::node::XLinkOut>();
16 
17  xIn->setStreamName("input");
18  xOut->setStreamName("output");
19 
20  // Link xin node to script node
21  xIn->out.link(script->inputs["log"]);
22  script->outputs["out"].link(xOut->input);
23 
24  script->setScript(R"(
25  while True:
26  _ = node.io["log"].get()
27  node.trace("TRACE")
28  node.debug("DEBUG")
29  node.info("INFO")
30  node.warn("WARN")
31  node.error("ERROR")
32  node.critical("CRITICAL")
33 
34  message = Buffer(10)
35  node.io["out"].send(message)
36  )");
37 
38  dai::Device device(pipeline);
39 
40  auto in = device.getInputQueue("input");
41  auto out = device.getOutputQueue("output");
42  dai::Buffer message; // Arbitrary message, used only to control flow
43 
44  device.setLogLevel(logLevel);
45  device.setLogOutputLevel(logLevel);
46 
47  // -1 below is for no_error, which cannot arrive
48  std::array<bool, spdlog::level::n_levels - 1> arrivedLogs;
49  for(auto& level : arrivedLogs) {
50  level = false;
51  }
52  bool testPassed = true;
53  auto logLevelConverted = static_cast<typename std::underlying_type<dai::LogLevel>::type>(logLevel);
54  auto callbackSink = [&testPassed, &arrivedLogs, logLevelConverted](dai::LogMessage message) {
55  // Convert message to spd for easier comparison
56  auto messageLevelConverted = static_cast<typename std::underlying_type<dai::LogLevel>::type>(message.level);
57  REQUIRE(messageLevelConverted >= logLevelConverted);
58  if(messageLevelConverted < arrivedLogs.size()) {
59  arrivedLogs[messageLevelConverted] = true;
60  } else {
61  FAIL();
62  }
63  };
64 
65  device.addLogCallback(callbackSink);
66  in->send(message);
67  out->get(); // Wait for the device to send the log(s)
68  using namespace std::chrono;
69  std::this_thread::sleep_for(milliseconds(200)); // Wait for the logs to arrive
70 
71  for(int i = 0; i < arrivedLogs.size(); i++) {
72  if(i < logLevelConverted) {
73  REQUIRE(!arrivedLogs[i]);
74  } else {
75  REQUIRE(arrivedLogs[i]);
76  }
77  }
80  // Exit with success error code
81  return 0;
82 }
83 
84 TEST_CASE("TRACE") {
86 }
87 
88 TEST_CASE("DEBUG") {
90 }
91 
92 TEST_CASE("INFO") {
94 }
95 
96 TEST_CASE("WARN") {
98 }
99 
100 TEST_CASE("ERROR") {
102 }
103 
104 TEST_CASE("CRITICAL") {
106 }
107 
108 TEST_CASE("OFF") {
110 }
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
TEST_CASE
TEST_CASE("TRACE")
Definition: logging_test.cpp:84
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
dai::LogLevel::TRACE
@ TRACE
dai::DeviceBase::setLogLevel
void setLogLevel(LogLevel level)
Definition: DeviceBase.cpp:1196
dai::LogLevel::INFO
@ INFO
dai::DeviceBase::setLogOutputLevel
void setLogOutputLevel(LogLevel level)
Definition: DeviceBase.cpp:1232
dai::LogLevel::CRITICAL
@ CRITICAL
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::node::Script
Definition: Script.hpp:15
dai::LogMessage
Definition: LogMessage.hpp:13
test
int test(dai::LogLevel logLevel)
Definition: logging_test.cpp:9
dai::LogLevel
LogLevel
Definition: LogLevel.hpp:12
nanorpc::core::detail::pack::meta::type
type
Definition: pack_meta.h:26
dai::LogLevel::OFF
@ OFF
dai::LogLevel::WARN
@ WARN
dai::Buffer
Base message - buffer of binary data.
Definition: Buffer.hpp:13
dai::LogLevel::ERR
@ ERR
dai::Device
Definition: Device.hpp:21
dai::node::XLinkIn
XLinkIn node. Receives messages over XLink.
Definition: XLinkIn.hpp:14
dai::Device::getInputQueue
std::shared_ptr< DataInputQueue > getInputQueue(const std::string &name)
Definition: Device.cpp:120
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
dai::DeviceBase::addLogCallback
int addLogCallback(std::function< void(LogMessage)> callback)
Definition: DeviceBase.cpp:1272
dai::LogLevel::DEBUG
@ DEBUG


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19