Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forward log messages from worker thread to main thread (#1176)
Proposed fix for #1120 Note that this fix has some downsides as well. So I'm not 100% sure we should take it yet. Looking for opinions. ### Problem - When workers (i.e. the compiler and debugger workers in VS Code) write logs, these are not surfaced in the VS Code output window along with other logs. This applies to logging from JavaScript (e.g `log.info()` calls), logs from Rust (e.g. `info!()` macros), and Rust panics. ### Fix - Workers, instead of logging directly to the console, now send a message to the main thread. The main thread logs the message as normal (to the Developer Tools console for playground/website, to the Output window for VS Code). All logs are now visible in the VS Code output window. ### Concerns about this fix - Logging becomes a nontrivial code path, which may defeat the purpose of using logging to debug tricky bugs. - For log messages, the original callstack in the worker is lost. Instead the callstack from the message handler in the main thread is shown (see screenshots). This will *not* be a problem for panic messages, which capture a stack trace explicitly. Personally, I wouldn't expect to see callstacks in log messages anyway, it's just something that Chrome does. - Log messages now become part of the message traffic between the main thread and the worker thread, which _could_ be a perf concern if noisy logs start slowing down the "real" communication between the threads. This is hypothetical; I haven't been able to find an actual scenario where this is a problem. We can mitigate this by being careful we're not writing super-noisy logs at the default log level ("info" and up). ### Alternate solution - Do nothing! There's actually a way to access the console logs for the worker thread in VS Code. You can bring up the Chrome developer tools using the "Toggle Developer Tools" VS Code command. It's not as convenient as having them in the output window, but they're there. ### Notes - Once a worker is started, there's no way to set the log level for that worker. This is a preexisting issue, and probably not a big deal in practice, since most of the workers (compiler, debugger) are short-lived and can just be run again with a different log level if we're trying to repro a specific issue. - A bonus fix here is increasing the stack trace frames to 20 (from 10) when logging panics, since most of the stack trace tends to get taken up by the logging code itself. ### Screenshots These show an `error!` log and a `panic!` surfaced from the WASM module. VS Code: **Before, release build, VS Code** <details> <summary>Screenshot</summary> ![Screenshot 2024-02-18 150303](https://github.com/microsoft/qsharp/assets/16928427/ac918d85-4c8f-4076-b62a-c83b589217d5) </details> **After, release build, VS Code** <details> <summary>Screenshot</summary> ![Screenshot 2024-02-18 145822](https://github.com/microsoft/qsharp/assets/16928427/975c541d-3fc0-4248-8bc5-a3b4b11c30c6) </details> **Before, debug build, VS Code** <details> <summary>Screenshot</summary> ![Screenshot 2024-02-18 150600](https://github.com/microsoft/qsharp/assets/16928427/0b2a89df-32a1-4e44-a1d2-dbe49d6e43ff) </details> **After, debug build, VS Code** <details> <summary>Screenshot</summary> ![Screenshot 2024-02-18 145544](https://github.com/microsoft/qsharp/assets/16928427/00161a8c-4a75-452c-ae23-eb490faa5a01) </details> **Before, release build, Developer Tools window in VS Code** <details> <summary>Screenshot</summary> ![image](https://github.com/microsoft/qsharp/assets/16928427/6f7d66e5-734c-4f9d-bb18-f4fd5bfb810e) </details> Playground - no real improvement here, since the worker logs were already shown in the Developer Tools console: **Before, release build, playground** <details> <summary>Screenshot</summary> ![Screenshot 2024-02-18 150200](https://github.com/microsoft/qsharp/assets/16928427/49b25013-a830-48b7-9670-f26660966725) </details> **After, release build, playground** <details> <summary>Screenshot</summary> ![Screenshot 2024-02-18 142645](https://github.com/microsoft/qsharp/assets/16928427/87b1867d-bd38-4745-b624-83a902e63582) </details> **Before, debug build, playground** <details> <summary>Screenshot</summary> ![Screenshot 2024-02-18 150503](https://github.com/microsoft/qsharp/assets/16928427/5d7fc6b9-7c1d-453f-9261-ddf6ddac4685) ![Screenshot 2024-02-18 150519](https://github.com/microsoft/qsharp/assets/16928427/4d4a2652-c151-44ca-8c7f-8cc1f86bc454) </details> **After, debug build, playground** <details> <summary>Screenshot</summary> ![Screenshot 2024-02-18 145153](https://github.com/microsoft/qsharp/assets/16928427/9cfea79c-6f71-47c3-94aa-19b137b3a9af) </details>
- Loading branch information