libs/libc/dumpbuffer: add support to dump the buffer to file descriptor.

Change-Id: I5742a69a75faf288df36da12ee5138f27a4f1532
Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an
2020-12-21 17:18:26 +08:00
parent 776a2ca8cc
commit 080b380955
3 changed files with 74 additions and 6 deletions
+12
View File
@@ -1021,11 +1021,23 @@ extern "C"
void lib_dumpbuffer(FAR const char *msg, FAR const uint8_t *buffer,
unsigned int buflen);
/* Dump a buffer of data to a specified file descriptor. */
void lib_writebuffer(int fd, FAR const char *msg,
FAR const uint8_t *buffer, unsigned int buflen);
/* Do a pretty buffer dump from multiple buffers. */
void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
int iovcnt);
/* Do a pretty buffer dump from multiple buffers
* to a specified file descriptor.
*/
void lib_writevbuffer(int fd, FAR const char *msg,
FAR const struct iovec *iov, int iovcnt);
/* The system logging interfaces are normally accessed via the macros
* provided above. If the cross-compiler's C pre-processor supports a
* variable number of macro arguments, then those macros below will map all
+23
View File
@@ -54,3 +54,26 @@ void lib_dumpbuffer(FAR const char *msg, FAR const uint8_t *buffer,
lib_dumpvbuffer(msg, &buf, 1);
}
/****************************************************************************
* Name: lib_writebuffer
*
* Description:
* Do a pretty buffer dump to a specified file descriptor.
*
* A fairly large on-stack buffer is used for the case where timestamps are
* applied to each line.
*
****************************************************************************/
void lib_writebuffer(int fd, FAR const char *msg,
FAR const uint8_t *buffer, unsigned int buflen)
{
struct iovec buf =
{
.iov_base = (FAR char *)buffer,
.iov_len = buflen,
};
lib_writevbuffer(fd, msg, &buf, 1);
}
+39 -6
View File
@@ -26,6 +26,7 @@
#include <nuttx/compiler.h>
#include <stdint.h>
#include <stdio.h>
#include <debug.h>
/****************************************************************************
@@ -70,18 +71,19 @@ static char lib_nibble(unsigned char nibble)
****************************************************************************/
/****************************************************************************
* Name: lib_dumpvbuffer
* Name: lib_writevbuffer
*
* Description:
* Do a pretty buffer dump from multiple buffers.
* Do a pretty buffer dump from multiple buffers to a specified file
* descriptor.
*
* A fairly large on-stack buffer is used for the case where timestamps are
* applied to each line.
*
****************************************************************************/
void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
int iovcnt)
void lib_writevbuffer(int fd, FAR const char *msg,
FAR const struct iovec *iov, int iovcnt)
{
FAR const struct iovec *piov = iov;
FAR const uint8_t *iov_buf;
@@ -96,7 +98,14 @@ void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
if (msg)
{
syslog(LOG_INFO, "%s (%p):\n", msg, iov->iov_base);
if (fd > 0)
{
dprintf(fd, "%s (%p):\n", msg, iov->iov_base);
}
else
{
syslog(LOG_INFO, "%s (%p):\n", msg, iov->iov_base);
}
}
for (i = 0; i < iovcnt; i++)
@@ -170,6 +179,30 @@ void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
*ptr++ = ' '; /* Plus 1 byte */
}
syslog(LOG_INFO, "%04x %s\n", i, line);
if (fd > 0)
{
dprintf(fd, "%04x %s\n", i, line);
}
else
{
syslog(LOG_INFO, "%04x %s\n", i, line);
}
}
}
/****************************************************************************
* Name: lib_dumpvbuffer
*
* Description:
* Do a pretty buffer dump from multiple buffers.
*
* A fairly large on-stack buffer is used for the case where timestamps are
* applied to each line.
*
****************************************************************************/
void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
int iovcnt)
{
lib_writevbuffer(-1, msg, iov, iovcnt);
}