You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/08/24 08:58:15 UTC

[incubator-nuttx] branch master updated: libc: add lib_dump utils

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 4ee39f6e9a libc: add lib_dump utils
4ee39f6e9a is described below

commit 4ee39f6e9a8d0446a962d76dc6b8fc87f322ac6a
Author: chengkai <ch...@xiaomi.com>
AuthorDate: Tue Jul 19 22:07:10 2022 +0800

    libc: add lib_dump utils
    
    Signed-off-by: chengkai <ch...@xiaomi.com>
---
 include/debug.h                  | 30 ++++++++++++++
 libs/libc/misc/lib_dumpbuffer.c  | 39 ++++++++++++++++++
 libs/libc/misc/lib_dumpvbuffer.c | 86 +++++++++++++++++++++++++++++++++++++---
 3 files changed, 149 insertions(+), 6 deletions(-)

diff --git a/include/debug.h b/include/debug.h
index 610531582b..f81b82b34e 100644
--- a/include/debug.h
+++ b/include/debug.h
@@ -1071,6 +1071,26 @@ extern "C"
 {
 #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 */
 
 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,
                      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
  * provided above.  If the cross-compiler's C pre-processor supports a
  * variable number of macro arguments, then those macros below will map all
diff --git a/libs/libc/misc/lib_dumpbuffer.c b/libs/libc/misc/lib_dumpbuffer.c
index db23cbf9b7..6c38377796 100644
--- a/libs/libc/misc/lib_dumpbuffer.c
+++ b/libs/libc/misc/lib_dumpbuffer.c
@@ -32,6 +32,26 @@
  * 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
  *
@@ -53,3 +73,22 @@ void lib_dumpbuffer(FAR const char *msg, FAR const uint8_t *buffer,
 
   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);
+}
diff --git a/libs/libc/misc/lib_dumpvbuffer.c b/libs/libc/misc/lib_dumpvbuffer.c
index 47b35d4938..b52d2c2866 100644
--- a/libs/libc/misc/lib_dumpvbuffer.c
+++ b/libs/libc/misc/lib_dumpvbuffer.c
@@ -28,6 +28,7 @@
 #include <string.h>
 #include <stdint.h>
 #include <debug.h>
+#include <stdio.h>
 
 /****************************************************************************
  * 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
  ****************************************************************************/
 
 /****************************************************************************
- * Name: lib_dumpvbuffer
+ * Name: lib_dumpvhandler
  *
  * 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
  *  applied to each line.
  *
  ****************************************************************************/
 
-void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
-                     int iovcnt)
+void lib_dumpvhandler(FAR const char *msg, FAR const struct iovec *iov,
+                      int iovcnt, lib_dump_handler_t handler,
+                      FAR void *arg)
 {
   FAR const struct iovec *piov = iov;
   unsigned int len = 0;
@@ -91,9 +130,14 @@ void lib_dumpvbuffer(FAR const char *msg, FAR const struct iovec *iov,
   unsigned int i = 0;
   FAR char *ptr;
 
+  if (!handler)
+    {
+      return;
+    }
+
   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 */
@@ -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);
+}