Improve boot_out.txt truncation

* write any partial message
 * instead of "..." show a sensible (translatable) message

This does slightly lower the amount of data that can be printed,
and makes the exact amount dependent on the language. However, if boot.py
intentionally needs to produce larger amounts of output, it can
deliberately mount the filesystem in RW mode and perform any writes needed.
In that case it's up to the boot.py to choose an appropriate way to
limit the number of writes if needed for the application.
This commit is contained in:
Jeff Epler 2023-02-14 10:46:20 -06:00
parent 5f43a63a70
commit 73840f840d
2 changed files with 13 additions and 2 deletions

View File

@ -2410,6 +2410,10 @@ msgid ""
"You pressed the reset button during boot. Press again to exit safe mode." "You pressed the reset button during boot. Press again to exit safe mode."
msgstr "" msgstr ""
#: supervisor/shared/micropython.c
msgid "[truncated due to length]"
msgstr ""
#: py/objtype.c #: py/objtype.c
msgid "__init__() should return None" msgid "__init__() should return None"
msgstr "" msgstr ""

View File

@ -62,8 +62,15 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE #ifdef CIRCUITPY_BOOT_OUTPUT_FILE
if (boot_output != NULL) { if (boot_output != NULL) {
// Ensure boot_out.txt is capped at 1 filesystem block and ends with a newline // Ensure boot_out.txt is capped at 1 filesystem block and ends with a newline
if (len + boot_output->len > 508) { #define TRUNCATED translate("[truncated due to length]")
vstr_add_str(boot_output, "...\n"); size_t truncated_message_len = decompress_length(TRUNCATED);
size_t maxlen = 512 - truncated_message_len; // includes trailing '\0' so we do not need to account for trailing newline '\n' in vstr_add_byte
if (len + boot_output->len > maxlen) {
size_t remaining_len = maxlen - boot_output->len;
vstr_add_strn(boot_output, str, remaining_len);
char buf[truncated_message_len];
vstr_add_str(boot_output, decompress(TRUNCATED, buf));
vstr_add_byte(boot_output, '\n');
boot_output = NULL; boot_output = NULL;
} else { } else {
vstr_add_strn(boot_output, str, len); vstr_add_strn(boot_output, str, len);