Skip to content

Commit

Permalink
Add log level and flag operations (#76)
Browse files Browse the repository at this point in the history
* Add log level and flag operations

Longhorn 7939

Signed-off-by: Derek Su <[email protected]>

---------

Signed-off-by: Derek Su <[email protected]>
Signed-off-by: David Ko <[email protected]>
Co-authored-by: David Ko <[email protected]>
Co-authored-by: David Ko <[email protected]>
  • Loading branch information
3 people authored Feb 19, 2024
1 parent b64c3bc commit a6d23f9
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/cmd/basic/bdev_nvme.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ func bdevNvmeSetOptions(c *cli.Context) error {
return err
}

bdevNameList, err := spdkCli.BdevNvmeSetOptions(int32(c.Int("ctrlr-loss-timeout-sec")),
result, err := spdkCli.BdevNvmeSetOptions(int32(c.Int("ctrlr-loss-timeout-sec")),
int32(c.Int("reconnect-delay-sec")), int32(c.Int("fast-io-fail-timeout-sec")),
int32(c.Int("transport-ack-timeout")), int32(c.Int("keep_alive_timeout_ms")))
if err != nil {
return err
}

return util.PrintObject(bdevNameList)
return util.PrintObject(result)
}
208 changes: 208 additions & 0 deletions app/cmd/basic/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package basic

import (
"context"

"github.com/sirupsen/logrus"
"github.com/urfave/cli"

"github.com/longhorn/go-spdk-helper/pkg/spdk/client"
"github.com/longhorn/go-spdk-helper/pkg/util"
)

func LogCmd() cli.Command {
return cli.Command{
Name: "log",
Subcommands: []cli.Command{
LogSetFlagCmd(),
LogClearFlagCmd(),
LogGetFlagsCmd(),
LogSetLevelCmd(),
LogGetLevelCmd(),
LogSetPrintLevelCmd(),
LogGetPrintLevelCmd(),
},
}
}

func LogSetFlagCmd() cli.Command {
return cli.Command{
Name: "set-flag",
Usage: "set log flag: set-flag <FLAG>",
Action: func(c *cli.Context) {
if err := logSetFlag(c); err != nil {
logrus.WithError(err).Fatalf("Failed to run set log flag command")
}
},
}
}

func logSetFlag(c *cli.Context) error {
spdkCli, err := client.NewClient(context.Background())
if err != nil {
return err
}

result, err := spdkCli.LogSetFlag(c.Args().First())
if err != nil {
return err
}

return util.PrintObject(result)
}

func LogClearFlagCmd() cli.Command {
return cli.Command{
Name: "clear-flag",
Usage: "clear log flag: clear-flag <FLAG>",
Action: func(c *cli.Context) {
if err := logClearFlag(c); err != nil {
logrus.WithError(err).Fatalf("Failed to run clear log flag command")
}
},
}
}

func logClearFlag(c *cli.Context) error {
spdkCli, err := client.NewClient(context.Background())
if err != nil {
return err
}

result, err := spdkCli.LogClearFlag(c.Args().First())
if err != nil {
return err
}

return util.PrintObject(result)
}

func LogGetFlagsCmd() cli.Command {
return cli.Command{
Name: "get-flags",
Usage: "get log flags",
Action: func(c *cli.Context) {
if err := logGetFlags(c); err != nil {
logrus.WithError(err).Fatalf("Failed to run get log flags command")
}
},
}
}

func logGetFlags(c *cli.Context) error {
spdkCli, err := client.NewClient(context.Background())
if err != nil {
return err
}

logFlags, err := spdkCli.LogGetFlags()
if err != nil {
return err
}

return util.PrintObject(logFlags)
}

func LogSetLevelCmd() cli.Command {
return cli.Command{
Name: "set-level",
Usage: "set log level: set-level <LEVEL>",
Action: func(c *cli.Context) {
if err := logSetLevel(c); err != nil {
logrus.WithError(err).Fatalf("Failed to run set log level command")
}
},
}
}

func logSetLevel(c *cli.Context) error {
spdkCli, err := client.NewClient(context.Background())
if err != nil {
return err
}

result, err := spdkCli.LogSetLevel(c.Args().First())
if err != nil {
return err
}

return util.PrintObject(result)
}

func LogGetLevelCmd() cli.Command {
return cli.Command{
Name: "get-level",
Usage: "get log level",
Action: func(c *cli.Context) {
if err := logGetLevel(c); err != nil {
logrus.WithError(err).Fatalf("Failed to run get log level command")
}
},
}
}

func logGetLevel(c *cli.Context) error {
spdkCli, err := client.NewClient(context.Background())
if err != nil {
return err
}

logLevel, err := spdkCli.LogGetLevel()
if err != nil {
return err
}

return util.PrintObject(logLevel)
}

func LogSetPrintLevelCmd() cli.Command {
return cli.Command{
Name: "set-print-level",
Usage: "set log print level: set-print-level <LEVEL>",
Action: func(c *cli.Context) {
if err := logSetPrintLevel(c); err != nil {
logrus.WithError(err).Fatalf("Failed to run set log print level command")
}
},
}
}

func logSetPrintLevel(c *cli.Context) error {
spdkCli, err := client.NewClient(context.Background())
if err != nil {
return err
}

result, err := spdkCli.LogSetPrintLevel(c.Args().First())
if err != nil {
return err
}

return util.PrintObject(result)
}

func LogGetPrintLevelCmd() cli.Command {
return cli.Command{
Name: "get-print-level",
Usage: "get log print level",
Action: func(c *cli.Context) {
if err := logGetPrintLevel(c); err != nil {
logrus.WithError(err).Fatalf("Failed to run get log print level command")
}
},
}
}

func logGetPrintLevel(c *cli.Context) error {
spdkCli, err := client.NewClient(context.Background())
if err != nil {
return err
}

logPrintLevel, err := spdkCli.LogGetPrintLevel()
if err != nil {
return err
}

return util.PrintObject(logPrintLevel)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func main() {
basic.BdevNvmeCmd(),
basic.BdevRaidCmd(),
basic.NvmfCmd(),
basic.LogCmd(),

advanced.DeviceCmd(),
advanced.ExposeCmd(),
Expand Down
101 changes: 101 additions & 0 deletions pkg/spdk/client/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"encoding/json"
"strings"

spdktypes "github.com/longhorn/go-spdk-helper/pkg/spdk/types"
)
Expand Down Expand Up @@ -944,3 +945,103 @@ func (c *Client) NvmfSubsystemGetListeners(nqn, tgtName string) (listenerList []

return listenerList, json.Unmarshal(cmdOutput, &listenerList)
}

// LogSetFlag sets the log flag.
//
// "flag": Required. Log flag to set.
func (c *Client) LogSetFlag(flag string) (result bool, err error) {
req := spdktypes.LogSetFlagRequest{
Flag: flag,
}

cmdOutput, err := c.jsonCli.SendCommand("log_set_flag", req)
if err != nil {
return false, err
}

return result, json.Unmarshal(cmdOutput, &result)
}

// LogClearFlag clears the log flag.
//
// "flag": Required. Log flag to clear.
func (c *Client) LogClearFlag(flag string) (result bool, err error) {
req := spdktypes.LogClearFlagRequest{
Flag: flag,
}

cmdOutput, err := c.jsonCli.SendCommand("log_clear_flag", req)
if err != nil {
return false, err
}

return result, json.Unmarshal(cmdOutput, &result)
}

// LogGetFlags gets the log flags.
func (c *Client) LogGetFlags() (flags map[string]bool, err error) {
req := spdktypes.LogGetFlagsRequest{}

cmdOutput, err := c.jsonCli.SendCommand("log_get_flags", req)
if err != nil {
return nil, err
}

return flags, json.Unmarshal(cmdOutput, &flags)
}

// LogSetLevel sets the log level.
//
// "level": Required. Supported values are "disabled", "error", "warn", "notice", "info", "debug". Default is "notice".
func (c *Client) LogSetLevel(level string) (result bool, err error) {
req := spdktypes.LogSetLevelRequest{
Level: level,
}

cmdOutput, err := c.jsonCli.SendCommand("log_set_level", req)
if err != nil {
return false, err
}

return result, json.Unmarshal(cmdOutput, &result)
}

// LogGetLevel gets the log level.
func (c *Client) LogGetLevel() (string, error) {
req := spdktypes.LogGetLevelRequest{}

level, err := c.jsonCli.SendCommand("log_get_level", req)
if err != nil {
return "", err
}

return strings.Trim(string(level), "\"\n"), nil
}

// LogSetPrintLevel sets the log print level. The log print level is the level at which log messages are printed to the console.
//
// "level": Required. Supported values are "disabled", "error", "warn", "notice", "info", "debug". Default is "notice".
func (c *Client) LogSetPrintLevel(level string) (result bool, err error) {
req := spdktypes.LogSetPrintLevelRequest{
Level: level,
}

cmdOutput, err := c.jsonCli.SendCommand("log_set_print_level", req)
if err != nil {
return false, err
}

return result, json.Unmarshal(cmdOutput, &result)
}

// LogGetPrintLevel gets the log print level. The log print level is the level at which log messages are printed to the console.
func (c *Client) LogGetPrintLevel() (string, error) {
req := spdktypes.LogGetPrintLevelRequest{}

level, err := c.jsonCli.SendCommand("log_get_print_level", req)
if err != nil {
return "", err
}

return strings.Trim(string(level), "\"\n"), nil
}
26 changes: 26 additions & 0 deletions pkg/spdk/types/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package types

type LogSetFlagRequest struct {
Flag string `json:"flag"`
}

type LogClearFlagRequest struct {
Flag string `json:"flag"`
}

type LogGetFlagsRequest struct {
}

type LogSetLevelRequest struct {
Level string `json:"level"`
}

type LogGetLevelRequest struct {
}

type LogSetPrintLevelRequest struct {
Level string `json:"level"`
}

type LogGetPrintLevelRequest struct {
}

0 comments on commit a6d23f9

Please sign in to comment.