summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/build.py
diff options
context:
space:
mode:
authorMark Asselstine <mark.asselstine@windriver.com>2023-03-14 15:20:27 -0400
committerRichard Purdie <richard.purdie@linuxfoundation.org>2023-03-21 22:44:00 +0000
commit312db493eead16c9ce0a069c0ae5b7496b87b149 (patch)
treea8e30fc72464f8c7af7afd61e8752aca249a3692 /bitbake/lib/bb/build.py
parentf475ef04ca169944155cc06c5e294b0da22e32f8 (diff)
downloadpoky-312db493eead16c9ce0a069c0ae5b7496b87b149.tar.gz
bitbake: build: Make python output print to stdout when running with -v (verbose)
When tasks are run with -v (verbose) on the bitbake commandline, shell tasks print their stdout, python tasks do not. This change redirects the python task's print output to an in memory buffer. After the task is executed the output is printed to stdout via the logger. This makes the python task behavior match the shell task behavior when running with -v. The contents of the task's log files remain unchanged after this change. This approach should keep the correct order in most cases, however, if the python task accesses the logger directly, that content will appear before other output. On the other hand, this change should negate the need for python tasks to access the logger directly. Special care is taken to save/restore the existing stdout and stderr and preventing sending output directly to the logger when there are "recursive" calls, for instance when a python function calls a shell function, avoiding printing things potentially out of order and/or multiple times. The logging-test.bb in meta-selftest can be used to review this change. This has been tested with the full bblogging oeqa tests. [Yocto #14544] (Bitbake rev: 81a58647b2f4fc0a2589b2978fc9d81b2bfe6aec) Signed-off-by: Mark Asselstine <mark.asselstine@windriver.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/build.py')
-rw-r--r--bitbake/lib/bb/build.py23
1 files changed, 21 insertions, 2 deletions
diff --git a/bitbake/lib/bb/build.py b/bitbake/lib/bb/build.py
index 5a1727116a..44d08f5c55 100644
--- a/bitbake/lib/bb/build.py
+++ b/bitbake/lib/bb/build.py
@@ -25,6 +25,7 @@ import bb
25import bb.msg 25import bb.msg
26import bb.process 26import bb.process
27import bb.progress 27import bb.progress
28from io import StringIO
28from bb import data, event, utils 29from bb import data, event, utils
29 30
30bblogger = logging.getLogger('BitBake') 31bblogger = logging.getLogger('BitBake')
@@ -177,7 +178,9 @@ class StdoutNoopContextManager:
177 178
178 @property 179 @property
179 def name(self): 180 def name(self):
180 return sys.stdout.name 181 if "name" in dir(sys.stdout):
182 return sys.stdout.name
183 return "<mem>"
181 184
182 185
183def exec_func(func, d, dirs = None): 186def exec_func(func, d, dirs = None):
@@ -296,9 +299,21 @@ def exec_func_python(func, d, runfile, cwd=None):
296 lineno = int(d.getVarFlag(func, "lineno", False)) 299 lineno = int(d.getVarFlag(func, "lineno", False))
297 bb.methodpool.insert_method(func, text, fn, lineno - 1) 300 bb.methodpool.insert_method(func, text, fn, lineno - 1)
298 301
302 if verboseStdoutLogging:
303 sys.stdout.flush()
304 sys.stderr.flush()
305 currout = sys.stdout
306 currerr = sys.stderr
307 sys.stderr = sys.stdout = execio = StringIO()
299 comp = utils.better_compile(code, func, "exec_func_python() autogenerated") 308 comp = utils.better_compile(code, func, "exec_func_python() autogenerated")
300 utils.better_exec(comp, {"d": d}, code, "exec_func_python() autogenerated") 309 utils.better_exec(comp, {"d": d}, code, "exec_func_python() autogenerated")
301 finally: 310 finally:
311 if verboseStdoutLogging:
312 execio.flush()
313 logger.plain("%s" % execio.getvalue())
314 sys.stdout = currout
315 sys.stderr = currerr
316 execio.close()
302 # We want any stdout/stderr to be printed before any other log messages to make debugging 317 # We want any stdout/stderr to be printed before any other log messages to make debugging
303 # more accurate. In some cases we seem to lose stdout/stderr entirely in logging tests without this. 318 # more accurate. In some cases we seem to lose stdout/stderr entirely in logging tests without this.
304 sys.stdout.flush() 319 sys.stdout.flush()
@@ -441,7 +456,11 @@ exit $ret
441 if fakerootcmd: 456 if fakerootcmd:
442 cmd = [fakerootcmd, runfile] 457 cmd = [fakerootcmd, runfile]
443 458
444 if verboseStdoutLogging: 459 # We only want to output to logger via LogTee if stdout is sys.__stdout__ (which will either
460 # be real stdout or subprocess PIPE or similar). In other cases we are being run "recursively",
461 # ie. inside another function, in which case stdout is already being captured so we don't
462 # want to Tee here as output would be printed twice, and out of order.
463 if verboseStdoutLogging and sys.stdout == sys.__stdout__:
445 logfile = LogTee(logger, StdoutNoopContextManager()) 464 logfile = LogTee(logger, StdoutNoopContextManager())
446 else: 465 else:
447 logfile = StdoutNoopContextManager() 466 logfile = StdoutNoopContextManager()