RTXI  3.0.0
The Real-Time eXperiment Interface Reference Manual
logger.cpp
Go to the documentation of this file.
1 
2 #include <chrono>
3 #include <iostream>
4 
5 #include "logger.hpp"
6 
7 #include "event.hpp"
8 #include "rt.hpp"
9 #include "widgets.hpp"
10 
11 // TODO: change localtime to be thread-safe
13 {
14  try {
15  const std::unique_lock<std::mutex> lk(this->log_mutex);
16  const auto time_point = std::chrono::system_clock::now();
17  const std::time_t now = std::chrono::system_clock::to_time_t(time_point);
18  this->ss << "[ ";
19  this->ss << std::put_time(std::localtime(&now), "%F %T"); // NOLINT
20  this->ss << " ] (EVENT FIRED)\t";
21  this->ss << " TYPE -- " << event->getName();
22  switch (event->getType()) {
24  this->ss << "\t VALUE -- ";
25  this->ss << std::any_cast<int64_t>(event->getParam("period"));
26  break;
29  break;
34  this->ss << "\t SOURCE -- ";
35  this->ss
36  << std::any_cast<RT::Thread*>(event->getParam("thread"))->getName();
37  break;
42  this->ss << "\t SOURCE -- ";
43  this->ss
44  << std::any_cast<RT::Device*>(event->getParam("device"))->getName();
45  break;
48  auto connection = std::any_cast<RT::block_connection_t>(
49  event->getParam("connection"));
50  this->ss << "\t CONNECTION -- {";
51  this->ss << "source: " << connection.src->getName();
52  this->ss << " type: "
53  << (connection.src_port_type == IO::OUTPUT ? "Output"
54  : "Input");
55  this->ss << " port: " << connection.src_port;
56  this->ss << "} <==> {";
57  this->ss << "destination: " << connection.dest->getName();
58  this->ss << " port: " << connection.dest_port << "}";
59  break;
60  }
62  this->ss << "\t SOURCE -- ";
63  this->ss << std::any_cast<std::string>(event->getParam("pluginName"));
64  break;
66  this->ss << "\t SOURCE -- ";
67  this->ss << std::any_cast<Widgets::Plugin*>(
68  event->getParam("pluginPointer"))
69  ->getName();
70  break;
72  this->ss << "\t SOURCE -- ";
73  this->ss << std::any_cast<Widgets::Component*>(
74  event->getParam("paramWidget"))
75  ->getName();
76  this->ss << " TYPE -- ";
78  std::any_cast<Widgets::Variable::variable_t>(
79  event->getParam("paramType")));
80  break;
82  this->ss << "\t SOURCE -- ";
83  this->ss << std::any_cast<Widgets::Component*>(
84  event->getParam("component"))
85  ->getName();
86  this->ss << " TYPE -- ";
88  std::any_cast<RT::State::state_t>(event->getParam("state")));
89  break;
90  default:
91  break;
92  }
93  this->ss << "\n";
94  std::cout << this->ss.str();
95  this->ss.str("");
96  } catch (std::bad_any_cast&) {
97  ERROR_MSG("Error while parsing event of type: {}", event->getName());
98  ERROR_MSG("Flushing event stream");
99  ERROR_MSG("{}", this->ss.str());
100  this->ss.str("");
101  }
102 }
103 
105 {
106  try {
107  const std::unique_lock<std::mutex> lk(this->log_mutex);
108  const auto time_point = std::chrono::system_clock::now();
109  const std::time_t now = std::chrono::system_clock::to_time_t(time_point);
110  this->ss << "[ ";
111  this->ss << std::put_time(std::localtime(&now), "%F %T"); // NOLINT
112  this->ss << " ] (TELEMITRY)\t";
113  this->ss << " TYPE -- ";
114  switch (response.type) {
116  this->ss << "Period Updated";
117  break;
119  this->ss << "System Threadlist Updated";
120  break;
122  this->ss << " System Devicelist Updated";
123  break;
125  this->ss << "NO-OP Acknowledged";
126  break;
128  this->ss << "Real-Time System Shutdown";
129  break;
131  this->ss << "Widget Parameter Updated";
132  break;
134  this->ss << "IO Link Updated";
135  break;
137  this->ss << "Widget State Updated";
138  break;
140  this->ss << "Real-Time System Error";
141  break;
143  this->ss << "NO TELEMITRY";
144  break;
145  default:
146  break;
147  }
148  this->ss << "\n";
149  std::cout << this->ss.str();
150  this->ss.str("");
151  } catch (...) {
152  ERROR_MSG("Something went wrong while logging telemitry from RT system");
153  }
154 }
std::string getName()
Definition: event.cpp:165
Event::Type getType() const
Definition: event.cpp:228
std::any getParam(const std::string &param_name) const
Definition: event.cpp:170
void log(Event::Object *event)
Definition: logger.cpp:12
void ERROR_MSG(const std::string &errmsg, Args... args)
Definition: debug.hpp:36
@ RT_WIDGET_PARAMETER_CHANGE_EVENT
Definition: event.hpp:68
@ RT_THREAD_PAUSE_EVENT
Definition: event.hpp:62
@ PLUGIN_INSERT_EVENT
Definition: event.hpp:77
@ RT_PERIOD_EVENT
Definition: event.hpp:56
@ RT_DEVICE_UNPAUSE_EVENT
Definition: event.hpp:67
@ RT_PREPERIOD_EVENT
Definition: event.hpp:57
@ RT_THREAD_UNPAUSE_EVENT
Definition: event.hpp:63
@ RT_WIDGET_STATE_CHANGE_EVENT
Definition: event.hpp:69
@ IO_LINK_INSERT_EVENT
Definition: event.hpp:71
@ PLUGIN_REMOVE_EVENT
Definition: event.hpp:78
@ IO_LINK_REMOVE_EVENT
Definition: event.hpp:72
@ RT_THREAD_INSERT_EVENT
Definition: event.hpp:60
@ RT_DEVICE_PAUSE_EVENT
Definition: event.hpp:66
@ RT_DEVICE_INSERT_EVENT
Definition: event.hpp:64
@ RT_THREAD_REMOVE_EVENT
Definition: event.hpp:61
@ RT_POSTPERIOD_EVENT
Definition: event.hpp:58
@ RT_DEVICE_REMOVE_EVENT
Definition: event.hpp:65
@ OUTPUT
Definition: io.hpp:53
constexpr response_t RT_WIDGET_PARAM_UPDATE
Definition: rt.hpp:76
constexpr response_t RT_DEVICE_LIST_UPDATE
Definition: rt.hpp:72
constexpr response_t RT_NOOP
Definition: rt.hpp:73
constexpr response_t NO_TELEMITRY
Definition: rt.hpp:84
constexpr response_t IO_LINK_UPDATED
Definition: rt.hpp:78
constexpr response_t RT_PERIOD_UPDATE
Definition: rt.hpp:70
constexpr response_t RT_ERROR
Definition: rt.hpp:82
constexpr response_t RT_THREAD_LIST_UPDATE
Definition: rt.hpp:71
constexpr response_t RT_WIDGET_STATE_UPDATE
Definition: rt.hpp:80
constexpr response_t RT_SHUTDOWN
Definition: rt.hpp:74
std::string vartype2string(variable_t type)
Definition: widgets.cpp:51
std::string state2string(RT::State::state_t state)
Definition: widgets.cpp:22
response_t type
Definition: rt.hpp:98