event_tracer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #pragma once
6 
7 #include <iostream>
8 #include <string>
9 #include <list>
11 #include <uavcan/time.hpp>
12 #include "../../clock.hpp"
13 
14 
16 {
18  {
19  const uavcan::dynamic_node_id_server::TraceCode code;
21 
22  EventLogEntry(uavcan::dynamic_node_id_server::TraceCode arg_code, uavcan::int64_t arg_argument)
23  : code(arg_code)
24  , argument(arg_argument)
25  { }
26  };
27 
28  const std::string id_;
30  std::list<EventLogEntry> event_log_;
31 
32 public:
34  startup_ts_(SystemClockDriver().getMonotonic())
35  { }
36 
37  EventTracer(const std::string& id) :
38  id_(id),
39  startup_ts_(SystemClockDriver().getMonotonic())
40  { }
41 
42  virtual void onEvent(uavcan::dynamic_node_id_server::TraceCode code, uavcan::int64_t argument)
43  {
45  std::cout << "EVENT [" << id_ << "]\t" << ts.toString() << "\t"
46  << int(code) << "\t" << getEventName(code) << "\t" << argument << std::endl;
47  event_log_.push_back(EventLogEntry(code, argument));
48  }
49 
50  unsigned countEvents(const uavcan::dynamic_node_id_server::TraceCode code) const
51  {
52  unsigned count = 0;
53  for (std::list<EventLogEntry>::const_iterator it = event_log_.begin(); it != event_log_.end(); ++it)
54  {
55  count += (it->code == code) ? 1U : 0U;
56  }
57  return count;
58  }
59 
60  uavcan::int64_t getLastEventArgumentOrFail(const uavcan::dynamic_node_id_server::TraceCode code) const
61  {
62  for (std::list<EventLogEntry>::const_reverse_iterator it = event_log_.rbegin(); it != event_log_.rend(); ++it)
63  {
64  if (it->code == code)
65  {
66  return it->argument;
67  }
68  }
69 
70  std::cout << "No such event in the event log, code " << int(code) << ", log length " << event_log_.size()
71  << std::endl;
72 
73  throw std::runtime_error("EventTracer::getLastEventArgumentOrFail()");
74  }
75 
76  unsigned getNumEvents() const { return static_cast<unsigned>(event_log_.size()); }
77 };
virtual void onEvent(uavcan::dynamic_node_id_server::TraceCode code, uavcan::int64_t argument)
virtual uavcan::MonotonicTime getMonotonic() const
unsigned countEvents(const uavcan::dynamic_node_id_server::TraceCode code) const
const uavcan::dynamic_node_id_server::TraceCode code
const std::string id_
uavcan::MonotonicTime getMonotonic()
Definition: clock.cpp:83
void toString(char buf[StringBufSize]) const
Prints time in seconds with microsecond resolution.
Definition: time.hpp:224
EventLogEntry(uavcan::dynamic_node_id_server::TraceCode arg_code, uavcan::int64_t arg_argument)
uavcan::int64_t getLastEventArgumentOrFail(const uavcan::dynamic_node_id_server::TraceCode code) const
const uavcan::MonotonicTime startup_ts_
unsigned getNumEvents() const
const uavcan::int64_t argument
std::int64_t int64_t
Definition: std.hpp:32
std::list< EventLogEntry > event_log_
EventTracer(const std::string &id)
int
Definition: libstubs.cpp:120


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39