Skip to content

Commit

Permalink
exec: include stream name and WARN: in outputstream log messages
Browse files Browse the repository at this point in the history
If an error happens when streaming the output, always include the stream name
and WARN in the related log message.

Some internal variable names are also shortened.
  • Loading branch information
fho committed Jan 11, 2024
1 parent 5465066 commit bb4ef18
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions internal/exec/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ func newMultiWriter(w ...io.Writer) io.Writer {
}

func (c *Cmd) startOutputStreamLogging(name string, useColorStderrColorfn bool) (io.Writer, func() error) {
logReader, logWriter := io.Pipe()
scannerTerminated := make(chan struct{})
r, w := io.Pipe()
done := make(chan struct{})

go func() {
sc := bufio.NewScanner(logReader)
sc := bufio.NewScanner(r)
// use a bigger buf to make it more unlikely that it will fail because of very long lines
sc.Buffer([]byte{}, outputStreamLineReaderBufSiz)

Expand All @@ -142,32 +142,32 @@ func (c *Cmd) startOutputStreamLogging(name string, useColorStderrColorfn bool)

if err := sc.Err(); err != nil {
if errors.Is(err, bufio.ErrTooLong) {
c.logf("streaming output failed, shown output might be complete, lines are too long, requiring newlines after latest %dBytes in output\n",
outputStreamLineReaderBufSiz)
c.logf("WARN: streaming %s output failed, shown output might be complete, lines are too long, requiring newlines after latest %dBytes in output\n",
name, outputStreamLineReaderBufSiz)
} else {
c.logf("streaming output failed, shown output might be complete: %s\n", err)
c.logf("WARN: streaming %s output failed, shown output might be complete: %s\n", name, err)
}
// We do not Close the logReader with an error because
// it would cause the MultiWriter to fail on all subsequent Write() operations for all streams,
// the command could fail because it could not write to
// stderr/sdout anymore.

// drain the logReader until the end, to prevent blocks of the MultiWriter
_, cpErr := io.Copy(io.Discard, logReader)
_, cpErr := io.Copy(io.Discard, r)
if cpErr != nil {
logReader.CloseWithError(fmt.Errorf("draining stream reader after line scanning failed, also failed: %w", errors.Join(err, cpErr)))
r.CloseWithError(fmt.Errorf("draining %s stream reader after line scanning failed, also failed: %w", name, errors.Join(err, cpErr)))
}

}
close(scannerTerminated)
close(done)
}()

return logWriter, func() error {
err := logWriter.Close()
<-scannerTerminated
return w, func() error {
err := w.Close()
<-done

if err != nil {
return fmt.Errorf("%s: closing output stream failed: %w", name, err)
return fmt.Errorf("closing %s output stream failed: %w", name, err)
}

return nil
Expand Down

0 comments on commit bb4ef18

Please sign in to comment.