Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not possible to write multi-line errors to console #492

Open
stephencelis opened this issue Aug 5, 2024 · 3 comments
Open

Not possible to write multi-line errors to console #492

stephencelis opened this issue Aug 5, 2024 · 3 comments

Comments

@stephencelis
Copy link

stephencelis commented Aug 5, 2024

It seems that carton feeds each onStderrLine result to its own console.error, so if a Swift app writes multiple lines at once to stderr to format things, rather than emitting a single browser error with the formatted message, the browser emits a unique error per line:

IMG_1784

@kateinoigakukun
Copy link
Member

This is one of the limitations in the WASI syscall interface. The fd_write syscall used as the underlying mechanism of print does not guarantee to flush per-line. It might give us a string in the middle of a line, so we need to buffer them by JS side, and we need to call console.error every time we get a CR/LF. For example, if we get the following two calls

fd_write("Hello,\nSwi")
fd_write("ft\n")

Our current way results:

Hello,
Swift

However, if we call console.error for each fd_write call, it results in:

Hello,
Swi
ft

Unless we have a way to write message in the last line without newline, we can't avoid such chopped up messages.

Therefore, if you really want to control when the message should be flushed, you need to use console API directly instead of relying on WASI-backed printing methods like Swift.print

@stephencelis
Copy link
Author

stephencelis commented Aug 6, 2024

Understood! Do you think it would be an option to buffer logs a single render tick and then combine any entries with the same stack trace before flushing to the web console? Or would that be problematic in its own right?

Feel free to close if it's completely unreasonable to fix 😄 We've customized our error reporter to report directly to the console, but wanted to flag this if it was possible to address in carton itself.

@kateinoigakukun
Copy link
Member

Do you think it would be an option to buffer logs a single render tick and then combine any entries with the same stack trace before flushing to the web console?

Interesting idea. It still potentially chops up messages when printing across frames (see below example), but I think it should solve the problem in 99% cases.

print("Hello, ", terminator: "")
await JSPromise.resolve(.undefined).value
print("Swift")

I'll make time to explore how other communities address this problem later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants