From 01d75ca9502c1b7a66eec5e08cffabd063cc80fe Mon Sep 17 00:00:00 2001 From: Derek Su Date: Tue, 20 Feb 2024 00:14:14 +0000 Subject: [PATCH] log: add client and server functions Longhorn 7939 Signed-off-by: Derek Su --- pkg/client/client.go | 46 ++++++++++++++++++ pkg/spdk/log.go | 112 +++++++++++++++++++++++++++++++++++++++++++ pkg/spdk/server.go | 54 +++++++++++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 pkg/spdk/log.go diff --git a/pkg/client/client.go b/pkg/client/client.go index 9005e1fa..214a20f3 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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 +} diff --git a/pkg/spdk/log.go b/pkg/spdk/log.go new file mode 100644 index 00000000..620c24ad --- /dev/null +++ b/pkg/spdk/log.go @@ -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] +} diff --git a/pkg/spdk/server.go b/pkg/spdk/server.go index a459fbff..d7f09805 100644 --- a/pkg/spdk/server.go +++ b/pkg/spdk/server.go @@ -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{