test_log_parser.cpp
Go to the documentation of this file.
1 // Test log parser
2 // Author: Max Schwarz <max.schwarz@ais.uni-bonn.de>
3 
4 #include <catch_ros/catch.hpp>
5 
6 #include "../../src/monitor/log_parser.h"
7 
8 using namespace rosmon::monitor;
9 
10 TEST_CASE("LogParser", "[log_parser]")
11 {
12  LogParser parser;
13 
14  int captures = 0;
15  LogParser::Event lastEvent;
16 
17  auto cb = [&](LogParser::Event&& event){
18  captures++;
19  lastEvent = std::move(event);
20  };
21  parser.setCallback(cb);
22 
23  SECTION("simple")
24  {
25  captures = 0;
26 
27  parser.processString("\e[0mThis is an info message\e[0m\n");
28  CHECK(captures == 1);
29  CHECK(lastEvent.severity == rosmon::LogEvent::Type::Info);
30  CHECK(lastEvent.message == "This is an info message");
31 
32  captures = 0;
33 
34  parser.processString("\e[31mThis is an error message\e[0m\n");
35  CHECK(captures == 1);
36  CHECK(lastEvent.severity == rosmon::LogEvent::Type::Error);
37  CHECK(lastEvent.message == "This is an error message");
38 
39  captures = 0;
40 
41  parser.processString("\e[32mThis is a debug message\e[0m\n");
42  CHECK(captures == 1);
43  CHECK(lastEvent.severity == rosmon::LogEvent::Type::Debug);
44  CHECK(lastEvent.message == "This is a debug message");
45 
46  captures = 0;
47 
48  parser.processString("\e[33mThis is a warning message\e[0m\n");
49  CHECK(captures == 1);
50  CHECK(lastEvent.severity == rosmon::LogEvent::Type::Warning);
51  CHECK(lastEvent.message == "This is a warning message");
52  }
53 
54  SECTION("raw")
55  {
56  captures = 0;
57 
58  parser.processString("This is a raw \e[31mred\e[0m message\n");
59  CHECK(captures == 1);
60  CHECK(lastEvent.severity == rosmon::LogEvent::Type::Raw);
61  CHECK(lastEvent.message == "This is a raw \e[31mred\e[0m message");
62  }
63 
64  SECTION("timeout")
65  {
66  captures = 0;
67 
68  auto t0 = std::chrono::steady_clock::time_point();
69  auto t1 = t0 + std::chrono::milliseconds(500);
70 
71  parser.processString("\e[33mThis is a warning message without its end!\n", t0);
72  CHECK(captures == 0);
73 
74  parser.checkPending(t0);
75  CHECK(captures == 0);
76 
77  parser.checkPending(t1);
78  CHECK(captures == 1);
79  CHECK(lastEvent.severity == rosmon::LogEvent::Type::Raw);
80  CHECK(lastEvent.message == "\e[33mThis is a warning message without its end!");
81  }
82 
83  SECTION("flush")
84  {
85  captures = 0;
86 
87  auto t0 = std::chrono::steady_clock::time_point();
88 
89  parser.processString("\e[33mThis is a warning message without its end!\n", t0);
90 
91  CHECK(captures == 0);
92 
93  parser.checkPending(t0);
94  CHECK(captures == 0);
95 
96  parser.flush();
97  CHECK(captures == 1);
98  CHECK(lastEvent.severity == rosmon::LogEvent::Type::Raw);
99  CHECK(lastEvent.message == "\e[33mThis is a warning message without its end!");
100  }
101 }
void setCallback(const std::function< void(Event &&)> &cb)
Definition: log_parser.cpp:211
void checkPending(const std::chrono::steady_clock::time_point &time=std::chrono::steady_clock::now())
Definition: log_parser.cpp:216
void processString(const std::string &str, const std::chrono::steady_clock::time_point &time=std::chrono::steady_clock::now())
Definition: log_parser.h:34
TEST_CASE("LogParser", "[log_parser]")


rosmon_core
Author(s): Max Schwarz
autogenerated on Fri Jun 16 2023 02:15:06