Skip to content

Commit

Permalink
logs: Add support for reading number of entries since an index for a …
Browse files Browse the repository at this point in the history
…specific log
  • Loading branch information
vrahane committed Mar 21, 2024
1 parent 5c3d1e7 commit 7986b77
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 10 deletions.
85 changes: 82 additions & 3 deletions newtmgr/cli/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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("%10s %10s %22s | %16s %16s %6s %8s %s\n",
"[num_entries]", "[index]", "[timestamp]", "[module]", "[level]", "[type]",
"[img]", "[message]")
}

Expand Down Expand Up @@ -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("%10d %10d %20dus | %16s %16s %6s %8s %s\n",
entry.NumEntries,
entry.Index,
entry.Timestamp,
modText,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -407,5 +478,13 @@ func logCmd() *cobra.Command {

logCmd.AddCommand(ListCmd)

NumEntriesCmd := &cobra.Command{
Use: "num_entries [log-name [min-index]] -c <conn_profile>",
Short: "Show the number of entries from index for a particular log",
Run: logNumEntriesCmd,
}

logCmd.AddCommand(NumEntriesCmd)

return logCmd
}
2 changes: 2 additions & 0 deletions nmxact/nmp/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions nmxact/nmp/defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
46 changes: 39 additions & 7 deletions nmxact/nmp/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 //
//////////////////////////////////////////////////////////////////////////////
Expand Down
43 changes: 43 additions & 0 deletions nmxact/xact/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down

0 comments on commit 7986b77

Please sign in to comment.