mirror of https://github.com/ARMmbed/mbed-os.git
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
parent
c40d86038c
commit
e20edbdb46
|
|
@ -56,30 +56,35 @@ void mbed_error_printf(const char *format, ...)
|
||||||
|
|
||||||
void mbed_error_vprintf(const char *format, va_list arg)
|
void mbed_error_vprintf(const char *format, va_list arg)
|
||||||
{
|
{
|
||||||
core_util_critical_section_enter();
|
|
||||||
char buffer[132];
|
char buffer[132];
|
||||||
int size = vsnprintf(buffer, sizeof buffer, format, arg);
|
int size = vsnprintf(buffer, sizeof buffer, format, arg);
|
||||||
if (size >= sizeof buffer) {
|
if (size >= sizeof buffer) {
|
||||||
/* Output was truncated - indicate by overwriting last 4 bytes of buffer
|
/* Output was truncated - indicate by overwriting tail of buffer
|
||||||
* with ellipsis and newline.
|
* with ellipsis, newline and null terminator.
|
||||||
* (Note that although vsnprintf always leaves a NUL terminator, we
|
|
||||||
* don't need a terminator and can use the entire buffer)
|
|
||||||
*/
|
*/
|
||||||
memcpy(&buffer[sizeof buffer - 4], "...\n", 4);
|
static const char ellipsis[] = "...\n";
|
||||||
size = sizeof buffer;
|
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
|
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES || MBED_CONF_PLATFORM_STDIO_CONVERT_TTY_NEWLINES
|
||||||
char stdio_out_prev = '\0';
|
char stdio_out_prev = '\0';
|
||||||
for (int i = 0; i < size; i++) {
|
for (; *str != '\0'; str++) {
|
||||||
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
|
if (*str == '\n' && stdio_out_prev != '\r') {
|
||||||
const char cr = '\r';
|
const char cr = '\r';
|
||||||
write(STDERR_FILENO, &cr, 1);
|
write(STDERR_FILENO, &cr, 1);
|
||||||
}
|
}
|
||||||
write(STDERR_FILENO, &buffer[i], 1);
|
write(STDERR_FILENO, str, 1);
|
||||||
stdio_out_prev = buffer[i];
|
stdio_out_prev = *str;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
write(STDERR_FILENO, buffer, size);
|
write(STDERR_FILENO, str, strlen(str));
|
||||||
#endif
|
#endif
|
||||||
core_util_critical_section_exit();
|
core_util_critical_section_exit();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -127,6 +127,9 @@ void mbed_die(void);
|
||||||
* handling a crash.
|
* handling a crash.
|
||||||
*
|
*
|
||||||
* @note Synchronization level: Interrupt safe
|
* @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.
|
* @param format C string that contains data stream to be printed.
|
||||||
* Code snippets below show valid format.
|
* 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);
|
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 */
|
/** @deprecated Renamed to mbed_error_vprintf to match functionality */
|
||||||
MBED_DEPRECATED_SINCE("mbed-os-5.11",
|
MBED_DEPRECATED_SINCE("mbed-os-5.11",
|
||||||
"Renamed to mbed_error_vprintf to match functionality.")
|
"Renamed to mbed_error_vprintf to match functionality.")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue