Skip to content

Commit

Permalink
log: add client and server functions
Browse files Browse the repository at this point in the history
Longhorn 7939

Signed-off-by: Derek Su <[email protected]>
  • Loading branch information
derekbit authored and innobead committed Feb 20, 2024
1 parent a24b20f commit 01d75ca
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,49 @@ func (c *SPDKClient) DiskDelete(diskName, diskUUID string) error {
})
return err
}

func (c *SPDKClient) LogSetLevel(level string) error {
client := c.getSPDKServiceClient()
ctx, cancel := context.WithTimeout(context.Background(), GRPCServiceTimeout)
defer cancel()

_, err := client.LogSetLevel(ctx, &spdkrpc.LogSetLevelRequest{
Level: level,
})
return err
}

func (c *SPDKClient) LogSetFlags(flags string) error {
client := c.getSPDKServiceClient()
ctx, cancel := context.WithTimeout(context.Background(), GRPCServiceTimeout)
defer cancel()

_, err := client.LogSetFlags(ctx, &spdkrpc.LogSetFlagsRequest{
Flags: flags,
})
return err
}

func (c *SPDKClient) LogGetLevel() (string, error) {
client := c.getSPDKServiceClient()
ctx, cancel := context.WithTimeout(context.Background(), GRPCServiceTimeout)
defer cancel()

resp, err := client.LogGetLevel(ctx, &emptypb.Empty{})
if err != nil {
return "", err
}
return resp.Level, nil
}

func (c *SPDKClient) LogGetFlags() (string, error) {
client := c.getSPDKServiceClient()
ctx, cancel := context.WithTimeout(context.Background(), GRPCServiceTimeout)
defer cancel()

resp, err := client.LogGetFlags(ctx, &emptypb.Empty{})
if err != nil {
return "", err
}
return resp.Flags, nil
}
112 changes: 112 additions & 0 deletions pkg/spdk/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package spdk

import (
"strings"

"github.com/sirupsen/logrus"

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

func svcLogSetLevel(spdkClient *spdkclient.Client, level string) error {
log := logrus.WithFields(logrus.Fields{
"level": level,
})

log.Trace("Setting log level")

if _, err := spdkClient.LogSetLevel(level); err != nil {
return err
}
if _, err := spdkClient.LogSetPrintLevel(level); err != nil {
return err
}
return nil

}

func svcLogSetFlags(spdkClient *spdkclient.Client, flags string) (err error) {
log := logrus.WithFields(logrus.Fields{
"flags": flags,
})

log.Trace("Setting log flags")

if flags == "" {
_, err = spdkClient.LogClearFlag("all")
return err
}

flagMap := commaSeparatedStringToMap(flags)
if _, ok := flagMap["all"]; ok {
_, err = spdkClient.LogSetFlag(flags)
return err
}

currentFlagMap, err := spdkClient.LogGetFlags()
if err != nil {
return err
}

for flag, enabled := range currentFlagMap {
targetFlagEnabled := flagMap[flag]
if enabled != targetFlagEnabled {
if targetFlagEnabled {
_, err = spdkClient.LogSetFlag(flag)
} else {
_, err = spdkClient.LogClearFlag(flag)
}
if err != nil {
return err
}
}
}

return nil
}

func commaSeparatedStringToMap(flags string) map[string]bool {
flagMap := make(map[string]bool)
if flags == "" {
return flagMap
}

flagList := strings.Split(flags, ",")
for _, flag := range flagList {
flagMap[flag] = true
}

return flagMap
}

func svcLogGetLevel(spdkClient *spdkclient.Client) (level string, err error) {
logrus.Trace("Getting log level")

return spdkClient.LogGetPrintLevel()
}

func svcLogGetFlags(spdkClient *spdkclient.Client) (flags string, err error) {
logrus.Trace("Getting log flags")

var flagsMap map[string]bool

flagsMap, err = spdkClient.LogGetFlags()
if err != nil {
return "", err
}
return mapToCommaSeparatedString(flagsMap), nil
}

func mapToCommaSeparatedString(m map[string]bool) string {
if len(m) == 0 {
return ""
}

var s string
for k, v := range m {
if v {
s += k + ","
}
}
return s[:len(s)-1]
}
54 changes: 54 additions & 0 deletions pkg/spdk/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,60 @@ func (s *Server) DiskGet(ctx context.Context, req *spdkrpc.DiskGetRequest) (ret
return svcDiskGet(spdkClient, req.DiskName)
}

func (s *Server) LogSetLevel(ctx context.Context, req *spdkrpc.LogSetLevelRequest) (ret *emptypb.Empty, err error) {
s.RLock()
spdkClient := s.spdkClient
s.RUnlock()

err = svcLogSetLevel(spdkClient, req.Level)
if err != nil {
return nil, err
}
return &emptypb.Empty{}, nil
}

func (s *Server) LogSetFlags(ctx context.Context, req *spdkrpc.LogSetFlagsRequest) (ret *emptypb.Empty, err error) {
s.RLock()
spdkClient := s.spdkClient
s.RUnlock()

err = svcLogSetFlags(spdkClient, req.Flags)
if err != nil {
return nil, err
}
return &emptypb.Empty{}, nil
}

func (s *Server) LogGetLevel(ctx context.Context, req *emptypb.Empty) (ret *spdkrpc.LogGetLevelResponse, err error) {
s.RLock()
spdkClient := s.spdkClient
s.RUnlock()

level, err := svcLogGetLevel(spdkClient)
if err != nil {
return nil, err
}

return &spdkrpc.LogGetLevelResponse{
Level: level,
}, nil
}

func (s *Server) LogGetFlags(ctx context.Context, req *emptypb.Empty) (ret *spdkrpc.LogGetFlagsResponse, err error) {
s.RLock()
spdkClient := s.spdkClient
s.RUnlock()

flags, err := svcLogGetFlags(spdkClient)
if err != nil {
return nil, err
}

return &spdkrpc.LogGetFlagsResponse{
Flags: flags,
}, nil
}

func (s *Server) VersionDetailGet(context.Context, *emptypb.Empty) (*spdkrpc.VersionDetailGetReply, error) {
// TODO: Implement this
return &spdkrpc.VersionDetailGetReply{
Expand Down

0 comments on commit 01d75ca

Please sign in to comment.