diff mbox series

[RFC,v2,2/3] build: Attach a log catcher to tasks and save their records

Message ID 20261115-mathieu-debug-replay-v4-v2-2-ad7d3c5cc213@bootlin.com
State New
Headers show
Series Preparatory work for sstate cache log replay | expand

Commit Message

Mathieu Dubois-Briand Nov. 15, 2026, 11:33 a.m. UTC
Every task gets a LogCatcher which records its warnings and errors
during execution. Right after the task body, and before the postfuncs,
the records are written to ${TMPDIR}/logrecords/[mc:<mc>/][<virtual
class>/]<recipe>/logrecords.<task>.json, so records from other recipes
can be bundled too. The sstate packaging postfunc reads the current
task's file to bundle the messages for replay when the task is later
restored from the sstate cache.

Writing the file before the postfuncs makes both the current task's own
record and the files of already finished dependency tasks readable from
the same place.

AI-Generated: Uses opencode (DeepSeek V4 Flash)
Signed-off-by: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
---
 lib/bb/build.py | 15 +++++++++++++++
 lib/bb/utils.py |  9 +++++++++
 2 files changed, 24 insertions(+)
diff mbox series

Patch

diff --git a/lib/bb/build.py b/lib/bb/build.py
index b38936fc360c..72f1c7bc72d2 100644
--- a/lib/bb/build.py
+++ b/lib/bb/build.py
@@ -21,6 +21,7 @@  import time
 import re
 import stat
 import datetime
+import json
 import bb
 import bb.msg
 import bb.process
@@ -668,6 +669,13 @@  def _exec_task(fn, task, d, quieterr):
         except OSError:
            pass
 
+    # Task log records, for sstate log replay. Written right after the task
+    # body, before the postfuncs, so the sstate packaging postfunc can read them.
+    # Stored under TMPDIR, keyed by multiconfig, virtual class and recipe, so
+    # any sstate task can reach the records of covered tasks from other recipes.
+    logrecords = bb.utils.logrecords_path(localdata, fn, task)
+    bb.utils.remove(logrecords)
+
     prefuncs = localdata.getVarFlag(task, 'prefuncs', expand=True)
     postfuncs = localdata.getVarFlag(task, 'postfuncs', expand=True)
 
@@ -710,6 +718,9 @@  def _exec_task(fn, task, d, quieterr):
     errchk = ErrorCheckHandler()
     bblogger.addHandler(errchk)
 
+    tasklogcatcher = bb.utils.LogCatcher()
+    bblogger.addHandler(tasklogcatcher)
+
     localdata.setVar('BB_LOGFILE', logfn)
     localdata.setVar('BB_RUNTASK', task)
     localdata.setVar('BB_TASK_LOGGER', bblogger)
@@ -723,6 +734,9 @@  def _exec_task(fn, task, d, quieterr):
             for func in (prefuncs or '').split():
                 exec_func(func, localdata)
             exec_func(task, localdata)
+            bb.utils.mkdirhier(os.path.dirname(logrecords))
+            with open(logrecords, 'w') as f:
+                json.dump(tasklogcatcher.records, f)
             for func in (postfuncs or '').split():
                 exec_func(func, localdata)
         finally:
@@ -732,6 +746,7 @@  def _exec_task(fn, task, d, quieterr):
             sys.stderr.flush()
 
             bblogger.removeHandler(handler)
+            bblogger.removeHandler(tasklogcatcher)
 
             # Restore the backup fds
             os.dup2(osi[0], osi[1])
diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index 4a4e911b0f8a..3728e8b6a02b 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -2150,6 +2150,15 @@  def load_plugins(logger, plugins, pluginpath):
                 plugins.append(plugin)
 
 
+def logrecords_path(d, fn, task):
+    from bb.cache import virtualfn2realfn
+
+    realfn, cls, mc = virtualfn2realfn(fn)
+    mc = "mc:" + mc if mc else ""
+    recipe = os.path.splitext(os.path.basename(realfn))[0]
+    return os.path.join(d.getVar("TMPDIR"), "logrecords", mc, cls, recipe,
+                        "logrecords.%s.json" % task)
+
 class LogCatcher(logging.Handler):
     """Logging handler for collecting logged messages so you can check them later"""
     def __init__(self):