From f8d0a6c4abb29ba41b61849f17c5b83f9a98921f Mon Sep 17 00:00:00 2001 From: matoval Date: Fri, 20 Sep 2024 18:30:01 -0700 Subject: [PATCH] Updated log var and implemented log levels --- pkg/controlsvc/controlsvc.go | 27 ++++++++++++++++----------- pkg/workceptor/command.go | 15 ++++++++++----- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/pkg/controlsvc/controlsvc.go b/pkg/controlsvc/controlsvc.go index 77e511c5e..67205835e 100644 --- a/pkg/controlsvc/controlsvc.go +++ b/pkg/controlsvc/controlsvc.go @@ -15,6 +15,7 @@ import ( "os" "reflect" "runtime" + "strconv" "strings" "sync" "time" @@ -123,33 +124,37 @@ func (s *SockControl) ReadFromConn(message string, out io.Writer, io Copier) err if err := s.WriteMessage(message); err != nil { return err } - isPayloadDebug := os.Getenv("RECEPTOR_PAYLOAD_DEBUG") - if isPayloadDebug != "" { + payloadDebug, _ := strconv.Atoi(os.Getenv("RECEPTOR_PAYLOAD_TRACE_LEVEL")) + switch { + case payloadDebug > 2: var data string reader := bufio.NewReader(s.conn) for { - var connectType string - if s.conn.LocalAddr().Network() == "unix" { - connectType = "unix socket" - } else { - connectType = "network connection" - } response, err := reader.ReadString('\n') if err != nil { if err.Error() != "EOF" { - MainInstance.nc.GetLogger().Error("Error reading from %v: %v \n", connectType, err) + MainInstance.nc.GetLogger().Error("Error reading from conn: %v \n", err) } break } data += response - MainInstance.nc.GetLogger().Debug("Response from %v: %v", connectType, response) + MainInstance.nc.GetLogger().Debug("Response reading from conn: %v", response) } if _, err := out.Write([]byte(data)); err != nil { return err } - } else { + fallthrough + case payloadDebug > 0: + var connectType string + if s.conn.LocalAddr().Network() == "unix" { + connectType = "unix socket" + } else { + connectType = "network connection" + } + MainInstance.nc.GetLogger().Debug("Reading from %v", connectType) + default: if _, err := io.Copy(out, s.conn); err != nil { return err } diff --git a/pkg/workceptor/command.go b/pkg/workceptor/command.go index c0f425cd8..971e74078 100644 --- a/pkg/workceptor/command.go +++ b/pkg/workceptor/command.go @@ -13,6 +13,7 @@ import ( "os/exec" "os/signal" "path" + "strconv" "strings" "sync" "syscall" @@ -114,11 +115,12 @@ func commandRunner(command string, params string, unitdir string) error { if err != nil { return err } - isPayloadDebug := os.Getenv("RECEPTOR_PAYLOAD_DEBUG") - if isPayloadDebug != "" { + payloadDebug, _ := strconv.Atoi(os.Getenv("RECEPTOR_PAYLOAD_TRACE_LEVEL")) + splitUnitDir := strings.Split(unitdir, "/") + workUnitID := splitUnitDir[len(splitUnitDir)-1] + switch payloadDebug { + case 3: var data string - splitUnitDir := strings.Split(unitdir, "/") - workUnitID := splitUnitDir[len(splitUnitDir)-1] reader := bufio.NewReader(stdin) stdinStream, err := cmd.StdinPipe() if err != nil { @@ -137,7 +139,10 @@ func commandRunner(command string, params string, unitdir string) error { MainInstance.nc.GetLogger().Debug("Work unit %v stdin: %v", workUnitID, response) } io.WriteString(stdinStream, data) - } else { + case 2: + MainInstance.nc.GetLogger().Debug("Work unit %v received command\n", workUnitID) + cmd.Stdin = stdin + default: cmd.Stdin = stdin } stdout, err := os.OpenFile(path.Join(unitdir, "stdout"), os.O_CREATE+os.O_WRONLY+os.O_SYNC, 0o600)