lib: cbprintf: add libc f/printf substitutes

This allows applications that may not use minimal libc avoid the cost
of a second printf-like formatting infrastructure by using printfcb()
instead of printf() for output.  It also helps make sure that the
formatting support (e.g. floats) is consistent between user-directed
output and the logging infrastructure.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2020-11-29 06:59:24 -06:00 committed by Anas Nashif
commit 8528e45897
3 changed files with 121 additions and 0 deletions

View file

@ -22,6 +22,8 @@ int cbprintf(cbprintf_cb out, void *ctx, const char *format, ...)
#if defined(CONFIG_CBPRINTF_LIBC_SUBSTS)
#include <stdio.h>
/* Context for sn* variants is the next space in the buffer, and the buffer
* end.
*/
@ -46,6 +48,40 @@ static int str_out(int c,
return c;
}
int fprintfcb(FILE *stream, const char *format, ...)
{
va_list ap;
int rc;
va_start(ap, format);
rc = vfprintfcb(stream, format, ap);
va_end(ap);
return rc;
}
int vfprintfcb(FILE *stream, const char *format, va_list ap)
{
return cbvprintf(fputc, stream, format, ap);
}
int printfcb(const char *format, ...)
{
va_list ap;
int rc;
va_start(ap, format);
rc = vprintfcb(format, ap);
va_end(ap);
return rc;
}
int vprintfcb(const char *format, va_list ap)
{
return cbvprintf(fputc, stdout, format, ap);
}
int snprintfcb(char *str, size_t size, const char *format, ...)
{
va_list ap;