Skip to content

Commit e20edbd

Browse files
committed
Add mbed_error_puts
This is potentially useful for printing long strings such as filenames from assert messages, avoiding the buffer limit inherent in mbed_error_printf.
1 parent c40d860 commit e20edbd

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

platform/mbed_board.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,35 @@ void mbed_error_printf(const char *format, ...)
5656

5757
void mbed_error_vprintf(const char *format, va_list arg)
5858
{
59-
core_util_critical_section_enter();
6059
char buffer[132];
6160
int size = vsnprintf(buffer, sizeof buffer, format, arg);
6261
if (size >= sizeof buffer) {
63-
/* Output was truncated - indicate by overwriting last 4 bytes of buffer
64-
* with ellipsis and newline.
65-
* (Note that although vsnprintf always leaves a NUL terminator, we
66-
* don't need a terminator and can use the entire buffer)
62+
/* Output was truncated - indicate by overwriting tail of buffer
63+
* with ellipsis, newline and null terminator.
6764
*/
68-
memcpy(&buffer[sizeof buffer - 4], "...\n", 4);
69-
size = sizeof buffer;
65+
static const char ellipsis[] = "...\n";
66+
memcpy(&buffer[sizeof buffer - sizeof ellipsis], ellipsis, sizeof ellipsis);
67+
}
68+
if (size > 0) {
69+
mbed_error_puts(buffer);
7070
}
71+
}
72+
73+
void mbed_error_puts(const char *str)
74+
{
75+
core_util_critical_section_enter();
7176
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES || MBED_CONF_PLATFORM_STDIO_CONVERT_TTY_NEWLINES
7277
char stdio_out_prev = '\0';
73-
for (int i = 0; i < size; i++) {
74-
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
78+
for (; *str != '\0'; str++) {
79+
if (*str == '\n' && stdio_out_prev != '\r') {
7580
const char cr = '\r';
7681
write(STDERR_FILENO, &cr, 1);
7782
}
78-
write(STDERR_FILENO, &buffer[i], 1);
79-
stdio_out_prev = buffer[i];
83+
write(STDERR_FILENO, str, 1);
84+
stdio_out_prev = *str;
8085
}
8186
#else
82-
write(STDERR_FILENO, buffer, size);
87+
write(STDERR_FILENO, str, strlen(str));
8388
#endif
8489
core_util_critical_section_exit();
8590
}

platform/mbed_interface.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ void mbed_die(void);
127127
* handling a crash.
128128
*
129129
* @note Synchronization level: Interrupt safe
130+
* @note This uses an internal 128-byte buffer to format the string,
131+
* so the output may be truncated. If you need to write a potentially
132+
* long string, use mbed_error_puts.
130133
*
131134
* @param format C string that contains data stream to be printed.
132135
* Code snippets below show valid format.
@@ -149,6 +152,20 @@ void mbed_error_printf(const char *format, ...);
149152
*/
150153
void mbed_error_vprintf(const char *format, va_list arg);
151154

155+
/** Print out an error message. This is typically called when
156+
* handling a crash.
157+
*
158+
* Unlike mbed_error_printf, there is no limit to the maximum output
159+
* length. Unlike standard puts, but like standard fputs, this does not
160+
* append a '\n' character.
161+
*
162+
* @note Synchronization level: Interrupt safe
163+
*
164+
* @param str C string that contains data stream to be printed.
165+
*
166+
*/
167+
void mbed_error_puts(const char *str);
168+
152169
/** @deprecated Renamed to mbed_error_vprintf to match functionality */
153170
MBED_DEPRECATED_SINCE("mbed-os-5.11",
154171
"Renamed to mbed_error_vprintf to match functionality.")

0 commit comments

Comments
 (0)