libc: add lib_dump utils

Signed-off-by: chengkai <chengkai@xiaomi.com>
This commit is contained in:
chengkai 2022-07-19 22:07:10 +08:00 committed by Xiang Xiao
parent a93b703994
commit 4ee39f6e9a
3 changed files with 149 additions and 6 deletions

View File

@ -1071,6 +1071,26 @@ extern "C"
{ {
#endif #endif
/* Type of the call out function pointer provided to
* lib_dumphandler() or lib_dumpvhandler()
*/
typedef CODE void (*lib_dump_handler_t)(FAR void *arg,
FAR const char *fmt, ...)
printflike(2, 3);
/* Dump a buffer of data with handler */
void lib_dumphandler(FAR const char *msg, FAR const uint8_t *buffer,
unsigned int buflen, lib_dump_handler_t handler,
FAR void *arg);
/* Do a pretty buffer dump from multiple buffers with handler. */
void lib_dumpvhandler(FAR const char *msg, FAR const struct iovec *iov,
int iovcnt, lib_dump_handler_t handler,
FAR void *arg);
/* Dump a buffer of data */ /* Dump a buffer of data */
void lib_dumpbuffer(FAR const char *msg, FAR const uint8_t *buffer, void lib_dumpbuffer(FAR const char *msg, FAR const uint8_t *buffer,
@ -1081,6 +1101,16 @@ void lib_dumpbuffer(FAR const char *msg, FAR const uint8_t *buffer,
void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov, void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
int iovcnt); int iovcnt);
/* Dump a buffer of data with fd */
void lib_dumpfile(int fd, FAR const char *msg, FAR const uint8_t *buffer,
unsigned int buflen);
/* Do a pretty buffer dump from multiple buffers with fd. */
void lib_dumpvfile(int fd, FAR const char *msg, FAR const struct iovec *iov,
int iovcnt);
/* The system logging interfaces are normally accessed via the macros /* The system logging interfaces are normally accessed via the macros
* provided above. If the cross-compiler's C pre-processor supports a * provided above. If the cross-compiler's C pre-processor supports a
* variable number of macro arguments, then those macros below will map all * variable number of macro arguments, then those macros below will map all

View File

@ -32,6 +32,26 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: lib_dumphandler
*
* Description:
* Do a pretty buffer dump with handler output.
*
****************************************************************************/
void lib_dumphandler(FAR const char *msg, FAR const uint8_t *buffer,
unsigned int buflen, lib_dump_handler_t handler,
FAR void *arg)
{
struct iovec buf;
buf.iov_base = (FAR void *)buffer;
buf.iov_len = buflen;
lib_dumpvhandler(msg, &buf, 1, handler, arg);
}
/**************************************************************************** /****************************************************************************
* Name: lib_dumpbuffer * Name: lib_dumpbuffer
* *
@ -53,3 +73,22 @@ void lib_dumpbuffer(FAR const char *msg, FAR const uint8_t *buffer,
lib_dumpvbuffer(msg, &buf, 1); lib_dumpvbuffer(msg, &buf, 1);
} }
/****************************************************************************
* Name: lib_dumpfile
*
* Description:
* Do a pretty buffer dump with fd output.
*
****************************************************************************/
void lib_dumpfile(int fd, FAR const char *msg, FAR const uint8_t *buffer,
unsigned int buflen)
{
struct iovec buf;
buf.iov_base = (FAR void *)buffer;
buf.iov_len = buflen;
lib_dumpvfile(fd, msg, &buf, 1);
}

View File

@ -28,6 +28,7 @@
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <debug.h> #include <debug.h>
#include <stdio.h>
/**************************************************************************** /****************************************************************************
* Pre-processor definitions * Pre-processor definitions
@ -66,23 +67,61 @@ static char lib_nibble(unsigned char nibble)
} }
} }
/****************************************************************************
* Name: lib_dumpvbuffer_handler
*
* Description:
* Do a pretty buffer dump from multiple buffers with syslog output.
*
****************************************************************************/
static void lib_dumpvbuffer_handler(FAR void *arg, FAR const char *fmt,
...)
{
va_list ap;
va_start(ap, fmt);
vsyslog(LOG_INFO, fmt, ap);
va_end(ap);
}
/****************************************************************************
* Name: lib_dumpvfile_handler
*
* Description:
* Do a pretty buffer dump from multiple buffers with file output.
*
****************************************************************************/
static void lib_dumpvfile_handler(FAR void *arg, FAR const char *fmt,
...)
{
va_list ap;
int *fd = (int *)arg;
va_start(ap, fmt);
vdprintf(*fd, fmt, ap);
va_end(ap);
}
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: lib_dumpvbuffer * Name: lib_dumpvhandler
* *
* Description: * Description:
* Do a pretty buffer dump from multiple buffers. * Do a pretty buffer dump from multiple buffers with handler output.
* *
* A fairly large on-stack buffer is used for the case where timestamps are * A fairly large on-stack buffer is used for the case where timestamps are
* applied to each line. * applied to each line.
* *
****************************************************************************/ ****************************************************************************/
void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov, void lib_dumpvhandler(FAR const char *msg, FAR const struct iovec *iov,
int iovcnt) int iovcnt, lib_dump_handler_t handler,
FAR void *arg)
{ {
FAR const struct iovec *piov = iov; FAR const struct iovec *piov = iov;
unsigned int len = 0; unsigned int len = 0;
@ -91,9 +130,14 @@ void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
unsigned int i = 0; unsigned int i = 0;
FAR char *ptr; FAR char *ptr;
if (!handler)
{
return;
}
if (msg) if (msg)
{ {
syslog(LOG_INFO, "%s (%p):\n", msg, iov->iov_base); (*handler)(arg, "%s (%p):\n", msg, iov->iov_base);
} }
/* Initialize the separator and terminator */ /* Initialize the separator and terminator */
@ -137,6 +181,36 @@ void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
} }
} }
syslog(LOG_INFO, "%04x %s\n", i++ * _NITEMS, line); (*handler)(arg, "%04x %s\n", i++ * _NITEMS, line);
} }
} }
/****************************************************************************
* Name: lib_dumpvbuffer
*
* Description:
* Do a pretty buffer dump from multiple buffers with
* lib_dumpvbuffer_handler output.
*
****************************************************************************/
void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
int iovcnt)
{
lib_dumpvhandler(msg, iov, iovcnt, lib_dumpvbuffer_handler, NULL);
}
/****************************************************************************
* Name: lib_dumpvfile
*
* Description:
* Do a pretty buffer dump from multiple buffers with lib_dumpvfile_handler
* output.
*
****************************************************************************/
void lib_dumpvfile(int fd, FAR const char *msg, FAR const struct iovec *iov,
int iovcnt)
{
lib_dumpvhandler(msg, iov, iovcnt, lib_dumpvfile_handler, &fd);
}