From 57de49f7583b8174c7f3d8e21956d4eaac64ac28 Mon Sep 17 00:00:00 2001 From: Vladimir Lavor Date: Tue, 6 Jul 2021 14:17:36 +0200 Subject: feat: api-trace Signed-off-by: Vladimir Lavor Change-Id: I7de363dfb3930db13a30e97f154c57d75c07f01c --- core/connection.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'core/connection.go') diff --git a/core/connection.go b/core/connection.go index f3ff964..ee5a06b 100644 --- a/core/connection.go +++ b/core/connection.go @@ -123,6 +123,8 @@ type Connection struct { msgControlPing api.Message msgControlPingReply api.Message + + apiTrace *trace // API tracer (disabled by default) } func newConnection(binapi adapter.VppAPI, attempts int, interval time.Duration) *Connection { @@ -145,6 +147,10 @@ func newConnection(binapi adapter.VppAPI, attempts int, interval time.Duration) subscriptions: make(map[uint16][]*subscriptionCtx), msgControlPing: msgControlPing, msgControlPingReply: msgControlPingReply, + apiTrace: &trace{ + list: make([]*api.Record, 0), + mux: &sync.Mutex{}, + }, } binapi.SetMsgCallback(c.msgCallback) return c @@ -480,3 +486,24 @@ func (c *Connection) sendConnEvent(event ConnectionEvent) { log.Warn("Connection state channel is full, discarding value.") } } + +// Trace gives access to the API trace interface +func (c *Connection) Trace() api.Trace { + return c.apiTrace +} + +// trace records api message +func (c *Connection) trace(msg api.Message, chId uint16, t time.Time, isReceived bool) { + if atomic.LoadInt32(&c.apiTrace.isEnabled) == 0 { + return + } + entry := &api.Record{ + Message: msg, + Timestamp: t, + IsReceived: isReceived, + ChannelID: chId, + } + c.apiTrace.mux.Lock() + c.apiTrace.list = append(c.apiTrace.list, entry) + c.apiTrace.mux.Unlock() +} -- cgit 1.2.3-korg