Skip to content

Commit 6fdac77

Browse files
committed
Replace direct print to stdout/stderr with pipes
This allows buffering warnings/errors messages from gcc
1 parent 89e7213 commit 6fdac77

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

utils/utils.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package utils
3131

3232
import (
33+
"bufio"
3334
"bytes"
3435
"crypto/md5"
3536
"encoding/hex"
@@ -291,33 +292,62 @@ func ExecCommand(ctx *types.Context, command *exec.Cmd, stdout int, stderr int)
291292
ctx.GetLogger().UnformattedFprintln(os.Stdout, PrintableCommand(command.Args))
292293
}
293294

295+
var stdoutCopy io.ReadCloser
296+
var stderrCopy io.ReadCloser
297+
294298
if stdout == Capture {
295299
buffer := &bytes.Buffer{}
296300
command.Stdout = buffer
297301
} else if stdout == Show || stdout == ShowIfVerbose && ctx.Verbose {
298-
command.Stdout = os.Stdout
302+
stdoutCopy, _ = command.StdoutPipe()
299303
}
300304

301305
if stderr == Capture {
302306
buffer := &bytes.Buffer{}
303307
command.Stderr = buffer
304308
} else if stderr == Show || stderr == ShowIfVerbose && ctx.Verbose {
305-
command.Stderr = os.Stderr
309+
stderrCopy, _ = command.StderrPipe()
306310
}
307311

308312
err := command.Start()
309313
if err != nil {
310314
return nil, nil, i18n.WrapError(err)
311315
}
312316

313-
err = command.Wait()
314-
315317
var outbytes, errbytes []byte
316-
if buf, ok := command.Stdout.(*bytes.Buffer); ok {
317-
outbytes = buf.Bytes()
318+
319+
if stdoutCopy != nil && stderrCopy != nil {
320+
stdOut := bufio.NewScanner(stdoutCopy)
321+
stdErr := bufio.NewScanner(stderrCopy)
322+
stdOut.Split(bufio.ScanLines)
323+
stdErr.Split(bufio.ScanLines)
324+
325+
go func() {
326+
for stdOut.Scan() {
327+
txt := stdOut.Text() + "\n"
328+
fmt.Fprint(os.Stderr, txt)
329+
outbytes = append(outbytes, txt...)
330+
}
331+
}()
332+
333+
go func() {
334+
for stdErr.Scan() {
335+
txt := stdErr.Text() + "\n"
336+
fmt.Fprint(os.Stderr, txt)
337+
errbytes = append(errbytes, txt...)
338+
}
339+
}()
318340
}
319-
if buf, ok := command.Stderr.(*bytes.Buffer); ok {
320-
errbytes = buf.Bytes()
341+
342+
err = command.Wait()
343+
344+
if stdoutCopy == nil || stderrCopy == nil {
345+
if buf, ok := command.Stdout.(*bytes.Buffer); ok {
346+
outbytes = buf.Bytes()
347+
}
348+
if buf, ok := command.Stderr.(*bytes.Buffer); ok {
349+
errbytes = buf.Bytes()
350+
}
321351
}
322352

323353
return outbytes, errbytes, i18n.WrapError(err)

0 commit comments

Comments
 (0)