From 88372c2c21b653131f993825484bf9a5ea509bdf Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Tue, 14 Jan 2020 15:41:38 +0100 Subject: top: use a separate pipe for the error stream Let's not mix apples and oranges and give stderr a dedicated pipe. This way, we don't return conmon log messages if run in debug mode. Signed-off-by: Valentin Rothberg --- pkg/adapter/containers.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'pkg') diff --git a/pkg/adapter/containers.go b/pkg/adapter/containers.go index df9f13944..a257ef4f4 100644 --- a/pkg/adapter/containers.go +++ b/pkg/adapter/containers.go @@ -951,17 +951,31 @@ func (r *LocalRuntime) execPS(c *libpod.Container, args []string) ([]string, err defer wPipe.Close() defer rPipe.Close() + rErrPipe, wErrPipe, err := os.Pipe() + if err != nil { + return nil, err + } + defer wErrPipe.Close() + defer rErrPipe.Close() + streams := new(libpod.AttachStreams) streams.OutputStream = wPipe - streams.ErrorStream = wPipe + streams.ErrorStream = wErrPipe streams.AttachOutput = true streams.AttachError = true - psOutput := []string{} + stdout := []string{} go func() { scanner := bufio.NewScanner(rPipe) for scanner.Scan() { - psOutput = append(psOutput, scanner.Text()) + stdout = append(stdout, scanner.Text()) + } + }() + stderr := []string{} + go func() { + scanner := bufio.NewScanner(rErrPipe) + for scanner.Scan() { + stderr = append(stderr, scanner.Text()) } }() @@ -970,10 +984,18 @@ func (r *LocalRuntime) execPS(c *libpod.Container, args []string) ([]string, err if err != nil { return nil, err } else if ec != 0 { - return nil, errors.Errorf("Runtime failed with exit status: %d and output: %s", ec, strings.Join(psOutput, " ")) + return nil, errors.Errorf("Runtime failed with exit status: %d and output: %s", ec, strings.Join(stderr, " ")) + } + + if logrus.GetLevel() >= logrus.DebugLevel { + // If we're running in debug mode or higher, we might want to have a + // look at stderr which includes debug logs from conmon. + for _, log := range stderr { + logrus.Debugf("%s", log) + } } - return psOutput, nil + return stdout, nil } // ExecContainer executes a command in the container -- cgit v1.2.3-54-g00ecf