/* * Copyright (C) 2015 Pavel Kirienko */ #pragma once #include #include #include #include class EventTracer : public uavcan::dynamic_node_id_server::IEventTracer { struct EventLogEntry { const uavcan::dynamic_node_id_server::TraceCode code; const uavcan::int64_t argument; EventLogEntry(uavcan::dynamic_node_id_server::TraceCode arg_code, uavcan::int64_t arg_argument) : code(arg_code) , argument(arg_argument) { } }; const std::string id_; std::list event_log_; public: EventTracer() { } EventTracer(const std::string& id) : id_(id) { } virtual void onEvent(uavcan::dynamic_node_id_server::TraceCode code, uavcan::int64_t argument) { std::cout << "EVENT [" << id_ << "]\t" << code << "\t" << getEventName(code) << "\t" << argument << std::endl; event_log_.push_back(EventLogEntry(code, argument)); } unsigned countEvents(const uavcan::dynamic_node_id_server::TraceCode code) const { unsigned count = 0; for (std::list::const_iterator it = event_log_.begin(); it != event_log_.end(); ++it) { count += (it->code == code) ? 1U : 0U; } return count; } uavcan::int64_t getLastEventArgumentOrFail(const uavcan::dynamic_node_id_server::TraceCode code) const { for (std::list::const_reverse_iterator it = event_log_.rbegin(); it != event_log_.rend(); ++it) { if (it->code == code) { return it->argument; } } std::cout << "No such event in the event log, code " << code << ", log length " << event_log_.size() << std::endl; throw std::runtime_error("EventTracer::getLastEventArgumentOrFail()"); } unsigned getNumEvents() const { return static_cast(event_log_.size()); } };