Skip to content

Instantly share code, notes, and snippets.

@lukewagner
Last active July 10, 2025 19:25
Show Gist options
  • Save lukewagner/6fe7b01bba8fdbdbbc851be08496c65e to your computer and use it in GitHub Desktop.
Save lukewagner/6fe7b01bba8fdbdbbc851be08496c65e to your computer and use it in GitHub Desktop.
canon log maybe??
(component
(canon log (level info) (num-strings 2) (core func $basic-log))
(canon log (level warn) (num-strings 2) (format 1 "at {0} I thought {1}") (core func $formatted-warning))
(core module $M
(import "" "basic-log" (func $basic-log (param $str1-ptr i32) (param $str1-len i32)
(param $str2-ptr i32) (param $str2-len i32))
(import "" "formatted-warning" (func $fancy-log (param $str1-ptr i32) (param $str1-len i32)
(param $str2.1-ptr i32) (param $str2.1-len i32)
(param $str2.2-ptr i32) (param $str2.2-len i32))
...
)
(core instance $m (instantiate $M (with "" (instance
(export "basic-log" (func $basic-log))
(export "formatted-warning" (func $formatted-warning))
))))
)
@lukewagner
Copy link
Author

lukewagner commented Jul 10, 2025

  • I don't know if it's too general to allow canon log to declare how many strings it outputs, but it nicely avoids the arbitrariness of baking in "2".
  • The reason for having a formatting string as a string immediate of canon log (instead of having the wasm code do it itself) is that, when logging is turned off or dialed down (only error/warn, not info, etc), you don't have the wasm code wasting cycles formatting strings that are then ignored.
  • While it's tempting to consider a canon get-log-level built-in that lets you query the current log level at runtime (and avoid unnecessary formatting if not needed), I think mayyybe a useful property is that enabling/disabling logging has zero semantic effect on the execution which should help avoid Heisenbugs or "crashes when logging enabled". A key part of this is that if you pass garbage i32 pointers/lengths to canon log it's specified never to trap (just maybe produce garbled output or some "" message).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment