mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-23 08:07:35 +08:00
Improved Raft event tracer - added event code to string conversion
This commit is contained in:
@@ -76,6 +76,14 @@ public:
|
||||
class IDynamicNodeIDAllocationServerEventTracer
|
||||
{
|
||||
public:
|
||||
#if UAVCAN_TOSTRING
|
||||
/**
|
||||
* It is safe to call this function with any argument.
|
||||
* If the event code is out of range, an assertion failure will be triggered and an error text will be returned.
|
||||
*/
|
||||
static const char* getEventName(uint16_t code);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* The server invokes this method every time it believes that a noteworthy event has happened.
|
||||
* The table of event codes can be found in the server sources.
|
||||
@@ -658,7 +666,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace dynamic_node_id_impl
|
||||
} // namespace dynamic_node_id_server_impl
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
@@ -15,6 +15,51 @@
|
||||
|
||||
namespace uavcan
|
||||
{
|
||||
/*
|
||||
* IDynamicNodeIDAllocationServerEventTracer
|
||||
*/
|
||||
#if UAVCAN_TOSTRING
|
||||
const char* IDynamicNodeIDAllocationServerEventTracer::getEventName(uint16_t code)
|
||||
{
|
||||
using namespace dynamic_node_id_server_impl;
|
||||
|
||||
// import re
|
||||
// make_strings = lambda s: ',\n'.join('"%s"' % x for x in re.findall(r'\ \ \ \ Trace([A-Za-z0-9]+),', s))
|
||||
static const char* const Strings[NumTraceEventCodes] =
|
||||
{
|
||||
"Error",
|
||||
"LogLastIndexRestored",
|
||||
"LogAppend",
|
||||
"LogRemove",
|
||||
"CurrentTermRestored",
|
||||
"CurrentTermUpdate",
|
||||
"VotedForRestored",
|
||||
"VotedForUpdate",
|
||||
"DiscoveryBroadcast",
|
||||
"NewServerDiscovered",
|
||||
"DiscoveryReceived",
|
||||
"ClusterSizeInited",
|
||||
"InvalidClusterSizeReceived",
|
||||
"RaftCoreInited",
|
||||
"RaftStateSwitch",
|
||||
"RaftModeSwitch",
|
||||
"RaftNewLogEntry",
|
||||
"RaftRequestIgnored",
|
||||
"RaftVoteRequestReceived",
|
||||
"RaftVoteRequestSucceeded",
|
||||
"RaftVoteRequestInitiation",
|
||||
"RaftPersistStateUpdateError",
|
||||
"RaftCommitIndexUpdate",
|
||||
"RaftNewerTermInResponse",
|
||||
"RaftNewEntryCommitted",
|
||||
"RaftAppendEntriesCallFailure"
|
||||
};
|
||||
uavcan::StaticAssert<sizeof(Strings) / sizeof(Strings[0]) == NumTraceEventCodes>::check();
|
||||
UAVCAN_ASSERT(code < NumTraceEventCodes);
|
||||
return (code < NumTraceEventCodes) ? Strings[code] : "INVALID_EVENT_CODE";
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace dynamic_node_id_server_impl
|
||||
{
|
||||
/*
|
||||
|
||||
@@ -57,9 +57,9 @@ class EventTracer : public uavcan::IDynamicNodeIDAllocationServerEventTracer
|
||||
{
|
||||
const std::string id_;
|
||||
|
||||
virtual void onEvent(uavcan::uint16_t event_code, uavcan::int64_t event_argument)
|
||||
virtual void onEvent(uavcan::uint16_t code, uavcan::int64_t argument)
|
||||
{
|
||||
std::cout << "EVENT [" << id_ << "]\t" << event_code << "\t" << event_argument << std::endl;
|
||||
std::cout << "EVENT [" << id_ << "]\t" << code << "\t" << getEventName(code) << "\t" << argument << std::endl;
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -874,6 +874,23 @@ TEST(DynamicNodeIDAllocationServer, RaftCoreBasic)
|
||||
}
|
||||
|
||||
|
||||
TEST(DynamicNodeIDAllocationServer, EventCodeToString)
|
||||
{
|
||||
using uavcan::IDynamicNodeIDAllocationServerEventTracer;
|
||||
using namespace uavcan::dynamic_node_id_server_impl;
|
||||
|
||||
// Simply checking some error codes
|
||||
ASSERT_STREQ("Error",
|
||||
IDynamicNodeIDAllocationServerEventTracer::getEventName(TraceError));
|
||||
ASSERT_STREQ("RaftModeSwitch",
|
||||
IDynamicNodeIDAllocationServerEventTracer::getEventName(TraceRaftModeSwitch));
|
||||
ASSERT_STREQ("RaftAppendEntriesCallFailure",
|
||||
IDynamicNodeIDAllocationServerEventTracer::getEventName(TraceRaftAppendEntriesCallFailure));
|
||||
ASSERT_STREQ("DiscoveryReceived",
|
||||
IDynamicNodeIDAllocationServerEventTracer::getEventName(TraceDiscoveryReceived));
|
||||
}
|
||||
|
||||
|
||||
TEST(DynamicNodeIDAllocationServer, ObjectSizes)
|
||||
{
|
||||
std::cout << "Log: " << sizeof(uavcan::dynamic_node_id_server_impl::Log) << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user