Commit 73a63a9
committed
Add leveled logging that costs nothing until it is switched on
Instrumenting a function with a print means paying for the message on every
call, forever, so the instrumentation gets deleted instead of left in place.
This is the usual answer: a level, off by default, and a payload that is not
built while the level is off.
`(pragma! log-level <off|error|warn|info|debug|trace>)` sets the enabled
severity. `(log! <level> <payload>)` emits `(Log <level> <value>)` through
println! when the level admits it, and returns unit when it does not.
`(log-enabled? <level>)` answers the same question for a caller that wants its
own sink, recording events as atoms in a space rather than printing them.
Two things make the off path cheap. The level is a field on the world, so
log-enabled? is an interpreter form reading it rather than a match against a
space. And log!'s payload is declared Atom, so if's unevaluated branches leave
it alone: the expression that would build the message is never reduced. The
lazy argument is what the log crate gets from a closure and Log4j2 from a
lambda; MeTTa's parameter types give it directly.
Measured on a 200-iteration loop with tabling off, the off path is flat across
a hundredfold change in payload cost, 68.6 ms at (work 10) and 68.7 ms at
(work 1000), while the same loop with the level set scales with it, 145 ms to
4765 ms. The same guard written in MeTTa over a space is flat too but costs
about 40% more per call, which is what the world field buys. The test states it
without timing: the payload of an unadmitted call is a function with no base
case, so anything that reduced it would hang rather than fail.
log-enabled? returns a grounded Bool, not a `False` symbol, which is what lets
it drive `if` directly.1 parent 970bc28 commit 73a63a9
3 files changed
Lines changed: 191 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
406 | 407 | | |
407 | 408 | | |
408 | 409 | | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
409 | 413 | | |
410 | 414 | | |
411 | 415 | | |
| |||
1619 | 1623 | | |
1620 | 1624 | | |
1621 | 1625 | | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
| 1631 | + | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
| 1639 | + | |
1622 | 1640 | | |
1623 | 1641 | | |
1624 | 1642 | | |
| |||
1654 | 1672 | | |
1655 | 1673 | | |
1656 | 1674 | | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
1657 | 1680 | | |
1658 | 1681 | | |
1659 | 1682 | | |
| |||
1688 | 1711 | | |
1689 | 1712 | | |
1690 | 1713 | | |
| 1714 | + | |
1691 | 1715 | | |
1692 | 1716 | | |
1693 | 1717 | | |
| |||
1708 | 1732 | | |
1709 | 1733 | | |
1710 | 1734 | | |
| 1735 | + | |
1711 | 1736 | | |
1712 | 1737 | | |
1713 | 1738 | | |
| |||
1783 | 1808 | | |
1784 | 1809 | | |
1785 | 1810 | | |
| 1811 | + | |
1786 | 1812 | | |
1787 | 1813 | | |
1788 | 1814 | | |
| |||
4277 | 4303 | | |
4278 | 4304 | | |
4279 | 4305 | | |
| 4306 | + | |
| 4307 | + | |
| 4308 | + | |
| 4309 | + | |
| 4310 | + | |
| 4311 | + | |
| 4312 | + | |
| 4313 | + | |
4280 | 4314 | | |
4281 | 4315 | | |
| 4316 | + | |
| 4317 | + | |
| 4318 | + | |
| 4319 | + | |
| 4320 | + | |
| 4321 | + | |
| 4322 | + | |
| 4323 | + | |
| 4324 | + | |
| 4325 | + | |
| 4326 | + | |
| 4327 | + | |
| 4328 | + | |
| 4329 | + | |
4282 | 4330 | | |
4283 | 4331 | | |
4284 | 4332 | | |
| |||
4398 | 4446 | | |
4399 | 4447 | | |
4400 | 4448 | | |
| 4449 | + | |
4401 | 4450 | | |
4402 | 4451 | | |
4403 | 4452 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
107 | 122 | | |
108 | 123 | | |
109 | 124 | | |
| |||
375 | 390 | | |
376 | 391 | | |
377 | 392 | | |
378 | | - | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
379 | 396 | | |
380 | 397 | | |
381 | 398 | | |
| |||
0 commit comments