From d4b0dedc2a63b5531fb40a8502fe27864dd2c4f5 Mon Sep 17 00:00:00 2001
From: Sergei Trofimov <sergei.trofimov@arm.com>
Date: Wed, 13 Jun 2018 13:47:41 +0100
Subject: [PATCH] utils/misc: add combined output option to check_output

Add an option to combine stderr and stdout into a single stream.
---
 devlib/utils/misc.py | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/devlib/utils/misc.py b/devlib/utils/misc.py
index 34d9367..dde6755 100644
--- a/devlib/utils/misc.py
+++ b/devlib/utils/misc.py
@@ -143,7 +143,8 @@ check_output_logger = logging.getLogger('check_output')
 check_output_lock = threading.Lock()
 
 
-def check_output(command, timeout=None, ignore=None, inputtext=None, **kwargs):
+def check_output(command, timeout=None, ignore=None, inputtext=None,
+                 combined_output=False, **kwargs):
     """This is a version of subprocess.check_output that adds a timeout parameter to kill
     the subprocess if it does not return within the specified time."""
     # pylint: disable=too-many-branches
@@ -165,9 +166,10 @@ def check_output(command, timeout=None, ignore=None, inputtext=None, **kwargs):
             pass  # process may have already terminated.
 
     with check_output_lock:
+        stderr = subprocess.STDOUT if combined_output else subprocess.PIPE
         process = subprocess.Popen(command,
                                    stdout=subprocess.PIPE,
-                                   stderr=subprocess.PIPE,
+                                   stderr=stderr,
                                    stdin=subprocess.PIPE,
                                    preexec_fn=preexec_function,
                                    **kwargs)
@@ -181,7 +183,8 @@ def check_output(command, timeout=None, ignore=None, inputtext=None, **kwargs):
         if sys.version_info[0] == 3:
             # Currently errors=replace is needed as 0x8c throws an error
             output = output.decode(sys.stdout.encoding, "replace")
-            error =  error.decode(sys.stderr.encoding, "replace")
+            if error:
+                error =  error.decode(sys.stderr.encoding, "replace")
     finally:
         if timeout:
             timer.cancel()