From 1d1f72b8b691b2220ea1221cc3da1f9bf2347f66 Mon Sep 17 00:00:00 2001 From: Vipul Rahane Date: Wed, 20 Mar 2024 21:01:14 -0700 Subject: [PATCH] logs: Add support for reading number of entries since an index for a specific log --- newtmgr/cli/log.go | 85 ++++++++++++++++++++++++++++++++++++++++++-- nmxact/nmp/decode.go | 2 ++ nmxact/nmp/defs.go | 1 + nmxact/nmp/log.go | 46 ++++++++++++++++++++---- nmxact/xact/log.go | 43 ++++++++++++++++++++++ 5 files changed, 167 insertions(+), 10 deletions(-) diff --git a/newtmgr/cli/log.go b/newtmgr/cli/log.go index 0774103f..c2c3d69e 100644 --- a/newtmgr/cli/log.go +++ b/newtmgr/cli/log.go @@ -60,6 +60,35 @@ type logShowCfg struct { Timestamp int64 } +type logNumEntriesCfg struct { + Name string + Index uint32 +} + +func logNumEntriesParseArgs(args []string) (*logNumEntriesCfg, error) { + cfg := &logNumEntriesCfg{} + + if len(args) < 1 { + return cfg, nil + } + cfg.Name = args[0] + + if len(args) < 2 { + cfg.Index = 0 + } else { + u64, err := strconv.ParseUint(args[1], 0, 64) + if err != nil { + return nil, util.ChildNewtError(err) + } + if u64 > 0xffffffff { + return nil, util.NewNewtError("index out of range") + } + cfg.Index = uint32(u64) + } + + return cfg, nil +} + func logShowParseArgs(args []string) (*logShowCfg, error) { cfg := &logShowCfg{} @@ -110,8 +139,8 @@ func printLogShowRsp(rsp *nmp.LogShowRsp, printHdr bool) { fmt.Printf("Name: %s\n", log.Name) fmt.Printf("Type: %s\n", nmp.LogTypeToString(log.Type)) - fmt.Printf("%10s %22s | %16s %16s %6s %8s %s\n", - "[index]", "[timestamp]", "[module]", "[level]", "[type]", + fmt.Printf("%16s %10s %22s | %16s %16s %6s %8s %s\n", + "[num_entries]", "[index]", "[timestamp]", "[module]", "[level]", "[type]", "[img]", "[message]") } @@ -145,7 +174,8 @@ func printLogShowRsp(rsp *nmp.LogShowRsp, printHdr bool) { msgText = hex.EncodeToString(entry.Msg) } - fmt.Printf("%10d %20dus | %16s %16s %6s %8s %s\n", + fmt.Printf("%16d %10d %20dus | %16s %16s %6s %8s %s\n", + entry.NumEntries, entry.Index, entry.Timestamp, modText, @@ -207,6 +237,20 @@ func logShowPartialCmd(s sesn.Sesn, cfg *logShowCfg) error { return nil } +func logNumEntriesCmd(cmd *cobra.Command, args []string) { + cfg, err := logNumEntriesParseArgs(args) + if err != nil { + nmUsage(cmd, err) + } + + s, err := GetSesn() + if err != nil { + nmUsage(nil, err) + } + + logNumEntriesProcCmd(s, cfg) +} + func logShowCmd(cmd *cobra.Command, args []string) { cfg, err := logShowParseArgs(args) if err != nil { @@ -288,6 +332,33 @@ func logModuleListCmd(cmd *cobra.Command, args []string) { } } +func logNumEntriesProcCmd(s sesn.Sesn, cfg *logNumEntriesCfg) { + s, err := GetSesn() + if err != nil { + nmUsage(nil, err) + } + + c := xact.NewLogNumEntriesCmd() + c.SetTxOptions(nmutil.TxOptions()) + c.Name = cfg.Name + c.Index = cfg.Index + + res, err := c.Run(s) + if err != nil { + nmUsage(nil, util.ChildNewtError(err)) + } + + sres := res.(*xact.LogNumEntriesResult) + if sres.Rsp.Rc != 0 { + fmt.Printf("error: %d\n", sres.Rsp.Rc) + return; + } + + fmt.Printf("Number of entries: %d\n", sres.Rsp.NumEntries) + + return +} + func logLevelListCmd(cmd *cobra.Command, args []string) { s, err := GetSesn() if err != nil { @@ -407,5 +478,13 @@ func logCmd() *cobra.Command { logCmd.AddCommand(ListCmd) + NumEntriesCmd := &cobra.Command{ + Use: "num_entries [log-name [min-index]] -c ", + Short: "Show the number of entries from index for a particular log", + Run: logNumEntriesCmd, + } + + logCmd.AddCommand(NumEntriesCmd) + return logCmd } diff --git a/nmxact/nmp/decode.go b/nmxact/nmp/decode.go index aa94ba59..d728dee0 100644 --- a/nmxact/nmp/decode.go +++ b/nmxact/nmp/decode.go @@ -66,6 +66,7 @@ func logListRspCtor() NmpRsp { return NewLogListRsp() } func logModuleListRspCtor() NmpRsp { return NewLogModuleListRsp() } func logLevelListRspCtor() NmpRsp { return NewLogLevelListRsp() } func logClearRspCtor() NmpRsp { return NewLogClearRsp() } +func logNumEntriesRspCtor() NmpRsp { return NewLogNumEntriesRsp() } func crashRspCtor() NmpRsp { return NewCrashRsp() } func runTestRspCtor() NmpRsp { return NewRunTestRsp() } func runListRspCtor() NmpRsp { return NewRunListRsp() } @@ -96,6 +97,7 @@ var rspCtorMap = map[Ogi]rspCtor{ {op_rr, gr_log, NMP_ID_LOG_MODULE_LIST}: logModuleListRspCtor, {op_rr, gr_log, NMP_ID_LOG_LEVEL_LIST}: logLevelListRspCtor, {op_wr, gr_log, NMP_ID_LOG_CLEAR}: logClearRspCtor, + {op_rr, gr_log, NMP_ID_LOG_NUM_ENTRIES}: logNumEntriesRspCtor, {op_wr, gr_cra, NMP_ID_CRASH_TRIGGER}: crashRspCtor, {op_wr, gr_run, NMP_ID_RUN_TEST}: runTestRspCtor, {op_rr, gr_run, NMP_ID_RUN_LIST}: runListRspCtor, diff --git a/nmxact/nmp/defs.go b/nmxact/nmp/defs.go index 5ef2c298..15fe6158 100644 --- a/nmxact/nmp/defs.go +++ b/nmxact/nmp/defs.go @@ -90,6 +90,7 @@ const ( NMP_ID_LOG_MODULE_LIST = 3 NMP_ID_LOG_LEVEL_LIST = 4 NMP_ID_LOG_LIST = 5 + NMP_ID_LOG_NUM_ENTRIES = 6 ) // Crash group (5). diff --git a/nmxact/nmp/log.go b/nmxact/nmp/log.go index 4e34cc13..8990d6bd 100644 --- a/nmxact/nmp/log.go +++ b/nmxact/nmp/log.go @@ -130,13 +130,14 @@ type LogShowReq struct { } type LogEntry struct { - Index uint32 `codec:"index"` - Timestamp int64 `codec:"ts"` - Module uint8 `codec:"module"` - Level uint8 `codec:"level"` - Type LogEntryType `codec:"type"` - ImgHash []byte `codec:"imghash"` - Msg []byte `codec:"msg"` + NumEntries uint32 `codec:"num_entries"` + Index uint32 `codec:"index"` + Timestamp int64 `codec:"ts"` + Module uint8 `codec:"module"` + Level uint8 `codec:"level"` + Type LogEntryType `codec:"type"` + ImgHash []byte `codec:"imghash"` + Msg []byte `codec:"msg"` } type LogShowLog struct { @@ -166,6 +167,37 @@ func NewLogShowRsp() *LogShowRsp { func (r *LogShowRsp) Msg() *NmpMsg { return MsgFromReq(r) } +////////////////////////////////////////////////////////////////////////////// +// $NumEntries // +////////////////////////////////////////////////////////////////////////////// + +type LogNumEntriesReq struct { + NmpBase `codec:"-"` + Name string `codec:"log_name"` + Index uint32 `codec:"index"` +} + +type LogNumEntriesRsp struct { + NmpBase + Rc int `codec:"rc"` + NumEntries uint32 `codec:"num_entries"` +} + +func NewLogNumEntriesReq() *LogNumEntriesReq { + r := &LogNumEntriesReq{} + fillNmpReq(r, NMP_OP_READ, NMP_GROUP_LOG, NMP_ID_LOG_NUM_ENTRIES) + return r +} + +func (r *LogNumEntriesReq) Msg() *NmpMsg { return MsgFromReq(r) } + +func NewLogNumEntriesRsp() *LogNumEntriesRsp { + return &LogNumEntriesRsp{} +} + +func (r *LogNumEntriesRsp) Msg() *NmpMsg { return MsgFromReq(r) } + + ////////////////////////////////////////////////////////////////////////////// // $list // ////////////////////////////////////////////////////////////////////////////// diff --git a/nmxact/xact/log.go b/nmxact/xact/log.go index b99a4b62..50f453a4 100644 --- a/nmxact/xact/log.go +++ b/nmxact/xact/log.go @@ -233,6 +233,49 @@ func (c *LogModuleListCmd) Run(s sesn.Sesn) (Result, error) { res.Rsp = srsp return res, nil } +////////////////////////////////////////////////////////////////////////////// +// $Num Entries // +////////////////////////////////////////////////////////////////////////////// + +type LogNumEntriesCmd struct { + CmdBase + Name string + Index uint32 +} + +func NewLogNumEntriesCmd() *LogNumEntriesCmd { + return &LogNumEntriesCmd{ + CmdBase: NewCmdBase(), + } +} + +type LogNumEntriesResult struct { + Rsp *nmp.LogNumEntriesRsp +} + +func newLogNumEntriesResult() *LogNumEntriesResult { + return &LogNumEntriesResult{} +} + +func (r *LogNumEntriesResult) Status() int { + return r.Rsp.Rc +} + +func (c *LogNumEntriesCmd) Run(s sesn.Sesn) (Result, error) { + r := nmp.NewLogNumEntriesReq() + r.Name = c.Name + r.Index = c.Index + + rsp, err := txReq(s, r.Msg(), &c.CmdBase) + if err != nil { + return nil, err + } + srsp := rsp.(*nmp.LogNumEntriesRsp) + + res := newLogNumEntriesResult() + res.Rsp = srsp + return res, nil +} ////////////////////////////////////////////////////////////////////////////// // $level list //