Add workaround for silently dropped console output.

This commit is contained in:
Jade Mattsson 2024-10-24 12:05:52 +11:00
parent 21122827e6
commit a001e41dd8
1 changed files with 11 additions and 3 deletions

View File

@ -171,7 +171,17 @@ static int retrying_write(const char *buf, size_t len)
size_t written = 0; size_t written = 0;
while (written < len) while (written < len)
{ {
size_t n = fwrite(buf + written, 1, len - written, stdout); // At least the USB-Serial-JTAG appears to silently drop characters
// sometimes when writing more than 255 bytes, so we break such strings
// up into multiple calls as a workaround.
const size_t MAX_LEN = 255;
size_t left = len - written;
size_t to_write = left > MAX_LEN ? MAX_LEN : left;
size_t n = fwrite(buf + written, 1, to_write, stdout);
// Additionally, we have to explicitly flush after each chunk we've written.
fflush(stdout);
fsync(fileno(stdout));
if (n > 0) if (n > 0)
written += n; written += n;
else if (ferror(stdout)) else if (ferror(stdout))
@ -224,8 +234,6 @@ static int console_write(lua_State *L)
retrying_write(&ch, 1); retrying_write(&ch, 1);
} }
} }
fflush(stdout);
fsync(fileno(stdout));
return 0; return 0;
} }