You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit eecb7f4
Browse filesBrowse the repository at this point in the historyBrowse files
Fix: make the Python logger a client of the host logger (#2001)
Python and C++ were two logging systems that agreed only on a threshold.
`Worker.init()` pushed the `simpler` logger's level into the native state, but a
Python record was then formatted by whatever handler sat on the root logger as
`[%(levelname)s] %(message)s` — no monotonic timestamp, no thread id. So a
Python record could not be ordered against a C++ one no matter where either was
written, and it went to the root logger's handler rather than to the host
logger's own output. The issue this belongs to is titled *one clock, one grammar,
one gate*; the Python side had the gate and neither of the others.
Seeding the native threshold now also installs a handler on the `simpler` logger
that forwards each record through `unified_log_*`, so every record in the process
shares one envelope, one clock and one destination.
- One new binding, `_emit_host_log(level, func, message)`, hands an
already-formatted message to `HostLogger::log`. The message is passed as a
`%s` argument, and a level outside the ladder is refused rather than cast.
- A record's level is rounded toward the milder name (`35` becomes `WARN`), the
opposite direction from `_normalize_threshold`: asking for a *threshold* of 35
must not silently admit warnings, but a record at 35 is a warning someone gave
a custom number.
- Forwarding is conditional on the host logger having a destination of its own.
It writes to stderr until a run gives it a directory, and the root logger's
handler already prints there, so forwarding unconditionally put one record on
one stream twice, in two envelopes, for every run that does not ask for
diagnostics. The check is per record because the directory arrives with a
run's config, after this handler is installed.
- `propagate` is deliberately untouched, so a record still reaches the root
logger as well. That keeps an interactive console readable and keeps
`caplog.at_level(..., logger="simpler")` working — five test files assert on
warnings that way. The host log is the complete copy; the console is a view of
it. Turning propagation off would make the file the only destination and blind
all of them.
- The handler is installed where the threshold is seeded, not at import.
`import simpler` has to keep working when `_task_interface` is missing or
stale, which the build-stamp guard makes routine on every source-tree move
until a rebuild, so putting the extension on the import path of the logging
surface would lose the logger exactly when it is needed to explain why.
Records logged before a worker is initialized stay on the root handler.
- The handler swallows its own failures through `handleError`: a logging call
must not become the reason a run fails.
- Installation takes a module-level lock. `Worker.init()` and `ChipWorker.init()`
hold per-instance locks, so nothing above serializes two instances
initializing from different threads, and an unguarded check-and-add would let
both add a handler and forward every record twice.
Tests cover the forwarded level/func/message, that nothing is forwarded while the
host log writes to stderr, that propagation and `caplog` survive, idempotent and
concurrent installation, the rounding direction, that a failing sink does not
raise into its caller, and that `import simpler` installs nothing. Their fixture
owns both the handler list and the logger level: this logger is process-global,
`configure_logging("null")` earlier in the same file leaves the level at `NUL`
without restoring it, and the suite's execution order is not the order the tests
are written in, so inheriting either one makes an assertion pass or fail by
position rather than by behaviour. One
of them exists specifically because the others do not need it: every other test
installs the handler itself, so without a test that `_initialize_host_log`
installs it, the production line doing so could be deleted with the suite
staying green. Verified by removing that line — exactly one test fails.
#1945 gave the host logger a configurable destination, so a run's Python records
now join its C++ records in `<output_prefix>/host.<pid>.log`. That PR's own
description of what stays on the console is corrected here in the same change:
it said routing Python's records through the host logger was a separate change,
which this is.
Copy file name to clipboardExpand all lines: docs/logging.md
+30-1Lines changed: 30 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -245,7 +245,7 @@ Onboard AICPU uses the CANN dlog format. Device TIMING uses CANN WARN and adds a
245
245
| Stage | Action | Source |
246
246
| ----- | ------ | ------ |
247
247
| Python import | Register `TIMING` / `NUL`; default the `simpler` logger to TIMING |`python/simpler/_log.py`|
248
-
|`Worker.init()`| Normalize the Python logger level and seed native state before the first fork |`python/simpler/worker.py`|
248
+
|`Worker.init()`| Normalize the Python logger level, seed native state before the first fork, and point the `simpler` logger at the host logger|`python/simpler/worker.py`|
249
249
|`ChipWorker.init()`| Re-seed inherited native state in a chip child, then enter C++ |`python/simpler/task_interface.py`|
250
250
|`_ChipWorker.init()`| Load sim context and host runtime, then bind each module's logger state |`src/common/worker/chip_worker.cpp`|
251
251
|`simpler_init`| Onboard maps the bound threshold to CANN; attach and take executor binaries |`src/common/platform/{onboard,sim}/host/c_api_shared.cpp`|
@@ -258,6 +258,35 @@ reinitialize the worker to apply a new Python configuration. Within a process,
258
258
all bound host modules observe a native state update immediately instead of
259
259
requiring threshold fan-out to every DSO.
260
260
261
+
### The Python logger is a client, not a second system
262
+
263
+
Seeding the native threshold also installs a handler on the `simpler` logger that
264
+
forwards each record through `unified_log_*`. Before that, Python and C++ agreed
265
+
only on a threshold: a Python record carried `[%(levelname)s] %(message)s` — no
266
+
timestamp, no thread id — so it could not be ordered against a C++ record no
267
+
matter where either was written, and it went to whatever handler happened to be
268
+
on the root logger rather than to the host logger's own output. Now one envelope,
269
+
one clock and one destination cover every record in the process.
270
+
271
+
Two properties this deliberately keeps:
272
+
273
+
-**Propagation is untouched**, so a record still reaches the root logger as
274
+
well. That is what keeps an interactive console readable and what keeps
275
+
`caplog.at_level(..., logger="simpler")` working in the tests that assert on
276
+
warnings. The host log is the complete copy; the console is a view of it.
277
+
-**The handler is installed where the threshold is seeded, not at import.**
278
+
`import simpler` must keep working when `_task_interface` is missing or stale —
279
+
the build-stamp guard in `simpler/task_interface.py` raises on every
280
+
source-tree move until a rebuild — so putting the extension on the import path
281
+
of the logging surface would lose the logger exactly when it is needed to say
282
+
why. Records logged before a worker is initialized stay on the root logger's
283
+
handler.
284
+
285
+
A record's level is rounded toward the milder name on the way through (`35`
286
+
becomes `WARN`), the opposite of how a *threshold* is normalized: asking for a
287
+
threshold of 35 must not silently admit warnings, but a record at 35 is a warning
288
+
someone gave a custom number.
289
+
261
290
### Forked chip subprocesses
262
291
263
292
The hierarchical parent seeds native state before `fork()` and passes the
0 commit comments