You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/12/05 09:31:19 UTC

[GitHub] andrzej-kaczmarek closed pull request #1548: sys/log: Add pretty printing for CBOR log entries

andrzej-kaczmarek closed pull request #1548: sys/log: Add pretty printing for CBOR log entries
URL: https://github.com/apache/mynewt-core/pull/1548
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/sys/log/full/src/log.c b/sys/log/full/src/log.c
index c432beea12..343cb42579 100644
--- a/sys/log/full/src/log.c
+++ b/sys/log/full/src/log.c
@@ -40,10 +40,10 @@ static const char *g_log_module_list[ MYNEWT_VAL(LOG_MAX_USER_MODULES) ];
 static uint8_t log_written;
 
 #if MYNEWT_VAL(LOG_CLI)
-int shell_log_dump_all_cmd(int, char **);
+int shell_log_dump_cmd(int, char **);
 struct shell_cmd g_shell_log_cmd = {
     .sc_cmd = "log",
-    .sc_cmd_func = shell_log_dump_all_cmd
+    .sc_cmd_func = shell_log_dump_cmd
 };
 
 #if MYNEWT_VAL(LOG_FCB_SLOT1)
diff --git a/sys/log/full/src/log_shell.c b/sys/log/full/src/log_shell.c
index c2b46082a4..0252552180 100644
--- a/sys/log/full/src/log_shell.c
+++ b/sys/log/full/src/log_shell.c
@@ -36,6 +36,115 @@
 #include "shell/shell.h"
 #include "console/console.h"
 #include "base64/hex.h"
+#if MYNEWT_VAL(LOG_VERSION) > 2
+#include "tinycbor/cbor.h"
+#endif
+
+#if MYNEWT_VAL(LOG_VERSION) > 2
+struct log_shell_cbor_reader {
+    struct cbor_decoder_reader r;
+    struct log *log;
+    void *dptr;
+};
+
+static uint8_t
+log_shell_cbor_reader_get8(struct cbor_decoder_reader *d, int offset)
+{
+    struct log_shell_cbor_reader *cbr = (struct log_shell_cbor_reader *)d;
+    uint8_t val = 0;
+
+    (void)log_read_body(cbr->log, cbr->dptr, &val, offset, sizeof(val));
+
+    return val;
+}
+
+static uint16_t
+log_shell_cbor_reader_get16(struct cbor_decoder_reader *d, int offset)
+{
+    struct log_shell_cbor_reader *cbr = (struct log_shell_cbor_reader *)d;
+    uint16_t val = 0;
+
+    (void)log_read_body(cbr->log, cbr->dptr, &val, offset, sizeof(val));
+
+    return val;
+}
+
+static uint32_t
+log_shell_cbor_reader_get32(struct cbor_decoder_reader *d, int offset)
+{
+    struct log_shell_cbor_reader *cbr = (struct log_shell_cbor_reader *)d;
+    uint8_t val = 0;
+
+    (void)log_read_body(cbr->log, cbr->dptr, &val, offset, sizeof(val));
+
+    return val;
+}
+
+static uint64_t
+log_shell_cbor_reader_get64(struct cbor_decoder_reader *d, int offset)
+{
+    struct log_shell_cbor_reader *cbr = (struct log_shell_cbor_reader *)d;
+    uint64_t val = 0;
+
+    (void)log_read_body(cbr->log, cbr->dptr, &val, offset, sizeof(val));
+
+    return val;
+}
+
+static uintptr_t
+log_shell_cbor_reader_cmp(struct cbor_decoder_reader *d, char *dst,
+                          int src_offset, size_t len)
+{
+    struct log_shell_cbor_reader *cbr = (struct log_shell_cbor_reader *)d;
+    uint8_t buf[16];
+    int chunk_len;
+    int offset;
+    int rc;
+
+    offset = 0;
+
+    while (offset < len) {
+        chunk_len = min(len - offset, sizeof(buf));
+
+        log_read_body(cbr->log, cbr->dptr, buf, src_offset + offset, chunk_len);
+
+        rc = memcmp(&dst[offset], buf, chunk_len);
+        if (rc) {
+            return rc;
+        }
+
+        offset += chunk_len;
+    }
+
+    return 0;
+}
+
+static uintptr_t
+log_shell_cbor_reader_cpy(struct cbor_decoder_reader *d, char *dst,
+                          int src_offset, size_t len)
+{
+    struct log_shell_cbor_reader *cbr = (struct log_shell_cbor_reader *)d;
+
+    log_read_body(cbr->log, cbr->dptr, dst, src_offset, len);
+
+    return (uintptr_t)dst;
+}
+
+static void
+log_shell_cbor_reader_init(struct log_shell_cbor_reader *cbr, struct log *log,
+                           void *dptr, uint16_t len)
+{
+    cbr->r.get8 = &log_shell_cbor_reader_get8;
+    cbr->r.get16 = &log_shell_cbor_reader_get16;
+    cbr->r.get32 = &log_shell_cbor_reader_get32;
+    cbr->r.get64 = &log_shell_cbor_reader_get64;
+    cbr->r.cmp = &log_shell_cbor_reader_cmp;
+    cbr->r.cpy = &log_shell_cbor_reader_cpy;
+    cbr->r.message_size = len;
+    cbr->log = log;
+    cbr->dptr = dptr;
+}
+#endif
 
 static int
 shell_log_dump_entry(struct log *log, struct log_offset *log_offset,
@@ -45,51 +154,73 @@ shell_log_dump_entry(struct log *log, struct log_offset *log_offset,
     int dlen;
     int rc;
 #if MYNEWT_VAL(LOG_VERSION) > 2
+    struct CborParser cbor_parser;
+    struct CborValue cbor_value;
+    struct log_shell_cbor_reader cbor_reader;
     char tmp[32 + 1];
     int off;
     int blksz;
+    bool read_data = ueh->ue_etype != LOG_ETYPE_CBOR;
+#else
+    bool read_data = true;
 #endif
 
     dlen = min(len, 128);
 
-    rc = log_read_body(log, dptr, data, 0, dlen);
-    if (rc < 0) {
-        return rc;
+    if (read_data) {
+        rc = log_read_body(log, dptr, data, 0, dlen);
+        if (rc < 0) {
+            return rc;
+        }
+        data[rc] = 0;
     }
-    data[rc] = 0;
+
+    console_printf("[%llu] ", ueh->ue_ts);
 
 #if MYNEWT_VAL(LOG_VERSION) <= 2
-    console_printf("[%llu] %s\n", ueh->ue_ts, data);
+    console_write(data, strlen(data));
 #else
     switch (ueh->ue_etype) {
     case LOG_ETYPE_STRING:
-        console_printf("[%llu] %s\n", ueh->ue_ts, data);
+        console_write(data, strlen(data));
+        break;
+    case LOG_ETYPE_CBOR:
+        log_shell_cbor_reader_init(&cbor_reader, log, dptr, len);
+        cbor_parser_init(&cbor_reader.r, 0, &cbor_parser, &cbor_value);
+        cbor_value_to_pretty(stdout, &cbor_value);
         break;
     default:
-        console_printf("[%llu] ", ueh->ue_ts);
         for (off = 0; off < rc; off += blksz) {
             blksz = dlen - off;
             if (blksz > sizeof(tmp) >> 1) {
                 blksz = sizeof(tmp) >> 1;
             }
             hex_format(&data[off], blksz, tmp, sizeof(tmp));
-            console_printf("%s", tmp);
+            console_write(tmp, strlen(tmp));
+        }
+        if (rc < len) {
+            console_write("...", 3);
         }
-        console_printf("%s\n", rc < len ? "..." : "");
     }
 #endif
+
+    console_write("\n", 1);
     return 0;
 }
 
 int
-shell_log_dump_all_cmd(int argc, char **argv)
+shell_log_dump_cmd(int argc, char **argv)
 {
     struct log *log;
     struct log_offset log_offset;
+    bool last = false;
+    bool list_only;
     int rc;
 
+    list_only = ((argc > 1) && !strcmp(argv[1], "-l"));
+
     log = NULL;
-    while (1) {
+    do {
         log = log_list_get_next(log);
         if (log == NULL) {
             break;
@@ -99,6 +230,18 @@ shell_log_dump_all_cmd(int argc, char **argv)
             continue;
         }
 
+        if (list_only) {
+            console_printf("%s\n", log->l_name);
+            continue;
+        }
+
+        if (argc > 1) {
+            if (strcmp(log->l_name, argv[1])) {
+                continue;
+            }
+            last = true;
+        }
+
         console_printf("Dumping log %s\n", log->l_name);
 
         log_offset.lo_arg = NULL;
@@ -110,7 +253,7 @@ shell_log_dump_all_cmd(int argc, char **argv)
         if (rc != 0) {
             goto err;
         }
-    }
+    } while (!last);
 
     return (0);
 err:


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services