[Concept,1/9] u_boot_pylib: Add stderr parameter to tprint()

Message ID 20251124134932.1991031-2-sjg@u-boot.org
State New
Headers
Series codman: Add a new source-code analysis tool |

Commit Message

Simon Glass Nov. 24, 2025, 1:49 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add optional stderr parameter to tprint() to allow printing to stderr
instead of stdout.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 tools/u_boot_pylib/terminal.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
  

Patch

diff --git a/tools/u_boot_pylib/terminal.py b/tools/u_boot_pylib/terminal.py
index 69c183e85e5..e62fa166dca 100644
--- a/tools/u_boot_pylib/terminal.py
+++ b/tools/u_boot_pylib/terminal.py
@@ -141,7 +141,7 @@  def trim_ascii_len(text, size):
 
 
 def tprint(text='', newline=True, colour=None, limit_to_line=False,
-           bright=True, back=None, col=None):
+           bright=True, back=None, col=None, stderr=False):
     """Handle a line of output to the terminal.
 
     In test mode this is recorded in a list. Otherwise it is output to the
@@ -151,6 +151,7 @@  def tprint(text='', newline=True, colour=None, limit_to_line=False,
         text: Text to print
         newline: True to add a new line at the end of the text
         colour: Colour to use for the text
+        stderr: True to print to stderr instead of stdout
     """
     global last_print_len
 
@@ -161,14 +162,17 @@  def tprint(text='', newline=True, colour=None, limit_to_line=False,
             if not col:
                 col = Color()
             text = col.build(colour, text, bright=bright, back=back)
+
+        file = sys.stderr if stderr else sys.stdout
+
         if newline:
-            print(text)
+            print(text, file=file)
             last_print_len = None
         else:
             if limit_to_line:
                 cols = shutil.get_terminal_size().columns
                 text = trim_ascii_len(text, cols)
-            print(text, end='', flush=True)
+            print(text, end='', flush=True, file=file)
             last_print_len = calc_ascii_len(text)
 
 def print_clear():