Skip to content

Commit

Permalink
Fix #190 - Add subcommand output to logs (#417)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #417

The feature for writing output to a log file was incomplete in the initial stub implementation D44521651 and subsequent updates (#10) because it did not configure subcommands to write their output to the specified logFile

This has now been corrected - all output of the TTP, including subcommand stdout/stderr, should now write to the specified logfile

Reviewed By: nicolagiacchetta

Differential Revision: D51306975

fbshipit-source-id: a810eee5fcb19672788ef4f76bb7f395f6d022bf
  • Loading branch information
d3sch41n authored and facebook-github-bot committed Nov 15, 2023
1 parent 02c554d commit 89d4330
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions pkg/blocks/iocapture.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,45 @@ package blocks
import (
"bytes"
"io"
"os"
"os/exec"

"github.com/facebookincubator/ttpforge/pkg/logging"
)

type zapWriter struct {
prefix string
}

func (z *zapWriter) Write(b []byte) (int, error) {
n := len(b)
// extra-defensive programming :P
if n <= 0 {
return 0, nil
}

// strip trailing newline
if b[n-1] == '\n' {
b = b[:n-1]
}

// split lines
lines := bytes.Split(b, []byte{'\n'})
for _, line := range lines {
logging.L().Info(z.prefix, string(line))
}
return n, nil
}

func streamAndCapture(cmd exec.Cmd, stdout, stderr io.Writer) (*ActResult, error) {
if stdout == nil {
stdout = os.Stdout
stdout = &zapWriter{
prefix: "[STDOUT] ",
}
}
if stderr == nil {
stderr = os.Stderr
stderr = &zapWriter{
prefix: "[STDERR] ",
}
}

var stdoutBuf, stderrBuf bytes.Buffer
Expand Down

0 comments on commit 89d4330

Please sign in to comment.