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.
pull/8441/head
Kevin Bracey 2018-10-15 14:14:21 +03:00
parent c40d86038c
commit e20edbdb46
2 changed files with 34 additions and 12 deletions

View File

@ -56,30 +56,35 @@ void mbed_error_printf(const char *format, ...)
void mbed_error_vprintf(const char *format, va_list arg)
{
core_util_critical_section_enter();
char buffer[132];
int size = vsnprintf(buffer, sizeof buffer, format, arg);
if (size >= sizeof buffer) {
/* Output was truncated - indicate by overwriting last 4 bytes of buffer
* with ellipsis and newline.
* (Note that although vsnprintf always leaves a NUL terminator, we
* don't need a terminator and can use the entire buffer)
/* Output was truncated - indicate by overwriting tail of buffer
* with ellipsis, newline and null terminator.
*/
memcpy(&buffer[sizeof buffer - 4], "...\n", 4);
size = sizeof buffer;
static const char ellipsis[] = "...\n";
memcpy(&buffer[sizeof buffer - sizeof ellipsis], ellipsis, sizeof ellipsis);
}
if (size > 0) {
mbed_error_puts(buffer);
}
}
void mbed_error_puts(const char *str)
{
core_util_critical_section_enter();
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES || MBED_CONF_PLATFORM_STDIO_CONVERT_TTY_NEWLINES
char stdio_out_prev = '\0';
for (int i = 0; i < size; i++) {
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
for (; *str != '\0'; str++) {
if (*str == '\n' && stdio_out_prev != '\r') {
const char cr = '\r';
write(STDERR_FILENO, &cr, 1);
}
write(STDERR_FILENO, &buffer[i], 1);
stdio_out_prev = buffer[i];
write(STDERR_FILENO, str, 1);
stdio_out_prev = *str;
}
#else
write(STDERR_FILENO, buffer, size);
write(STDERR_FILENO, str, strlen(str));
#endif
core_util_critical_section_exit();
}

View File

@ -127,6 +127,9 @@ void mbed_die(void);
* handling a crash.
*
* @note Synchronization level: Interrupt safe
* @note This uses an internal 128-byte buffer to format the string,
* so the output may be truncated. If you need to write a potentially
* long string, use mbed_error_puts.
*
* @param format C string that contains data stream to be printed.
* Code snippets below show valid format.
@ -149,6 +152,20 @@ void mbed_error_printf(const char *format, ...);
*/
void mbed_error_vprintf(const char *format, va_list arg);
/** Print out an error message. This is typically called when
* handling a crash.
*
* Unlike mbed_error_printf, there is no limit to the maximum output
* length. Unlike standard puts, but like standard fputs, this does not
* append a '\n' character.
*
* @note Synchronization level: Interrupt safe
*
* @param str C string that contains data stream to be printed.
*
*/
void mbed_error_puts(const char *str);
/** @deprecated Renamed to mbed_error_vprintf to match functionality */
MBED_DEPRECATED_SINCE("mbed-os-5.11",
"Renamed to mbed_error_vprintf to match functionality.")