You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2016/02/01 01:34:19 UTC

incubator-mynewt-larva git commit: begin log refactor. break out log into a set of files for all the functions. remove util_* prefix from log. Fix stats display at shell when wrong statistic entered -- need to jump to error to avoid error in stats_gro

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master e66ce8989 -> d4e843a65


begin log refactor.  break out log into a set of files for all the functions.  remove util_* prefix from log.  Fix stats display at shell when wrong statistic entered -- need to jump to error to avoid error in stats_group_find().  More to come.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/d4e843a6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/d4e843a6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/d4e843a6

Branch: refs/heads/master
Commit: d4e843a65f680cedcfa85628ebfac24de7f2e362
Parents: e66ce89
Author: Sterling Hughes <st...@apache.org>
Authored: Sun Jan 31 16:34:04 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Sun Jan 31 16:34:04 2016 -0800

----------------------------------------------------------------------
 project/slinky/src/main.c   |  18 ++--
 sys/log/include/log/log.h   |  72 ++++++++------
 sys/log/src/log.c           | 209 +++++++--------------------------------
 sys/log/src/log_cbmem.c     | 126 +++++++++++++++++++++++
 sys/log/src/log_shell.c     | 100 +++++++++++++++++++
 sys/stats/src/stats_shell.c |   1 +
 6 files changed, 314 insertions(+), 212 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/d4e843a6/project/slinky/src/main.c
----------------------------------------------------------------------
diff --git a/project/slinky/src/main.c b/project/slinky/src/main.c
index 511b19a..319fad4 100755
--- a/project/slinky/src/main.c
+++ b/project/slinky/src/main.c
@@ -61,8 +61,8 @@ os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
 os_stack_t newtmgr_stack[NEWTMGR_TASK_STACK_SIZE];
 
 struct cbmem log_mem;
-struct ul_handler log_mem_handler;
-struct util_log my_log;
+struct log_handler log_mem_handler;
+struct log my_log;
 uint8_t log_buf[12 * 1024];
 
 static volatile int g_task2_loops;
@@ -228,16 +228,18 @@ main(int argc, char **argv)
     rc = conf_register(&test_conf_handler);
     assert(rc == 0);
 
+    log_init();
+
     cbmem_init(&log_mem, log_buf, sizeof(log_buf));
-    util_log_cbmem_handler_init(&log_mem_handler, &log_mem);
-    util_log_register("log", &my_log, &log_mem_handler);
+    log_cbmem_handler_init(&log_mem_handler, &log_mem);
+    log_register("log", &my_log, &log_mem_handler);
 
     memset(entry, 0xff, 128);
-    memcpy(entry + sizeof(struct ul_entry_hdr), "bla", sizeof("bla")-1);
-    util_log_append(&my_log, entry, sizeof("bla")-1);
+    memcpy(entry + sizeof(struct log_entry_hdr), "bla", sizeof("bla")-1);
+    log_append(&my_log, entry, sizeof("bla")-1);
     memset(entry, 0xff, 128);
-    memcpy(entry + sizeof(struct ul_entry_hdr), "bab", sizeof("bab")-1);
-    util_log_append(&my_log, entry, sizeof("bab")-1);
+    memcpy(entry + sizeof(struct log_entry_hdr), "bab", sizeof("bab")-1);
+    log_append(&my_log, entry, sizeof("bab")-1);
 
     os_init();
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/d4e843a6/sys/log/include/log/log.h
----------------------------------------------------------------------
diff --git a/sys/log/include/log/log.h b/sys/log/include/log/log.h
index b1b64d7..83d11da 100644
--- a/sys/log/include/log/log.h
+++ b/sys/log/include/log/log.h
@@ -13,49 +13,65 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#ifndef __UTIL_LOG_H__ 
-#define __UTIL_LOG_H__
+#ifndef __LOG_H__ 
+#define __LOG_H__
 
 #include "util/cbmem.h"
+
 #include <os/queue.h>
 
-struct util_log;
+struct log;
 
-typedef int (*util_log_walk_func_t)(struct util_log *, void *arg, void *offset, 
+typedef int (*log_walk_func_t)(struct log *, void *arg, void *offset, 
         uint16_t len);
 
-typedef int (*ulh_read_func_t)(struct util_log *, void *dptr, void *buf, 
+typedef int (*lh_read_func_t)(struct log *, void *dptr, void *buf, 
         uint16_t offset, uint16_t len);
-typedef int (*ulh_append_func_t)(struct util_log *, void *buf, int len);
-typedef int (*ulh_walk_func_t)(struct util_log *, 
-        util_log_walk_func_t walk_func, void *arg);
-typedef int (*ulh_flush_func_t)(struct util_log *);
-
-struct ul_handler {
-    ulh_read_func_t ulh_read;
-    ulh_append_func_t ulh_append;
-    ulh_walk_func_t ulh_walk;
-    ulh_flush_func_t ulh_flush;
-    void *ulh_arg;
+typedef int (*lh_append_func_t)(struct log *, void *buf, int len);
+typedef int (*lh_walk_func_t)(struct log *, 
+        log_walk_func_t walk_func, void *arg);
+typedef int (*lh_flush_func_t)(struct log *);
+
+#define LOG_TYPE_STREAM  (0)
+#define LOG_TYPE_MEMORY  (1) 
+#define LOG_TYPE_STORAGE (2) 
+
+struct log_handler {
+    int log_type;
+    lh_read_func_t log_read;
+    lh_append_func_t log_append;
+    lh_walk_func_t log_walk;
+    lh_flush_func_t log_flush;
+    void *log_arg;
 };
 
-struct ul_entry_hdr {
+struct log_entry_hdr {
     int64_t ue_ts;
 }; 
 
-struct util_log {
-    char *ul_name;
-    struct ul_handler *ul_ulh;
-    STAILQ_ENTRY(util_log) ul_next;
+struct log {
+    char *l_name;
+    struct log_handler *l_log;
+    STAILQ_ENTRY(log) l_next;
 };
 
-int util_log_cbmem_handler_init(struct ul_handler *, struct cbmem *);
-int util_log_register(char *name, struct util_log *log, struct ul_handler *);
-int util_log_append(struct util_log *log, uint8_t *data, uint16_t len);
-int util_log_read(struct util_log *log, void *dptr, void *buf, uint16_t off, 
+/* Log system level functions (for all logs.) */
+int log_init(void);
+struct log *log_list_get_next(struct log *);
+
+/* Log functions, manipulate a single log */
+int log_register(char *name, struct log *log, struct log_handler *);
+int log_append(struct log *log, uint8_t *data, uint16_t len);
+int log_read(struct log *log, void *dptr, void *buf, uint16_t off, 
         uint16_t len);
-int util_log_walk(struct util_log *log, util_log_walk_func_t walk_func, 
+int log_walk(struct log *log, log_walk_func_t walk_func, 
         void *arg);
-int util_log_flush(struct util_log *log);
+int log_flush(struct log *log);
+
+
+/* CBMEM exports */
+
+int log_cbmem_handler_init(struct log_handler *, struct cbmem *);
+
 
-#endif /* __UTIL_LOG_H__ */
+#endif /* __LOG_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/d4e843a6/sys/log/src/log.c
----------------------------------------------------------------------
diff --git a/sys/log/src/log.c b/sys/log/src/log.c
index a6ebfa7..012cbe2 100644
--- a/sys/log/src/log.c
+++ b/sys/log/src/log.c
@@ -18,211 +18,68 @@
 
 #include <string.h>
 
-#include "log/log.h"
 #include <util/cbmem.h>
 
+#include "log/log.h"
+
 #include <stdio.h>
 
 #ifdef SHELL_PRESENT
-
 #include <shell/shell.h>
-#include <console/console.h> 
-
-struct shell_cmd shell_log_cmd;
-uint8_t shell_registered;
-
 #endif 
 
-static STAILQ_HEAD(, util_log) g_util_log_list = 
-    STAILQ_HEAD_INITIALIZER(g_util_log_list);
-
-
-#ifdef SHELL_PRESENT 
-
-static int 
-shell_log_dump_entry(struct util_log *log, void *arg, void *dptr, uint16_t len) 
-{
-    struct ul_entry_hdr ueh;
-    char data[128];
-    int dlen;
-    int rc;
-
-    rc = util_log_read(log, dptr, &ueh, 0, sizeof(ueh)); 
-    if (rc != sizeof(ueh)) {
-        goto err;
-    }
-
-    dlen = min(len-sizeof(ueh), 128);
-
-    rc = util_log_read(log, dptr, data, sizeof(ueh), dlen);
-    if (rc < 0) {
-        goto err;
-    }
-    data[rc] = 0;
-
-    /* XXX: This is evil.  newlib printf does not like 64-bit 
-     * values, and this causes memory to be overwritten.  Cast to a 
-     * unsigned 32-bit value for now.
-     */
-    console_printf("[%lu] %s\n", (unsigned long) ueh.ue_ts, data);
-
-    return (0);
-err:
-    return (rc);
-}
-
-static int 
-shell_log_dump_all(int argc, char **argv)
-{
-    struct util_log *log;
-    int rc;
-
-    STAILQ_FOREACH(log, &g_util_log_list, ul_next) {
-        rc = util_log_walk(log, shell_log_dump_entry, NULL);
-        if (rc != 0) {
-            goto err;
-        }
-    }
-
-    return (0);
-err:
-    return (rc);
-}
+static STAILQ_HEAD(, log) g_log_list = STAILQ_HEAD_INITIALIZER(g_log_list);
 
+#ifdef SHELL_PRESENT
+struct shell_cmd g_shell_log_cmd;
+int shell_log_dump_all_cmd(int, char **);
 #endif 
 
-static int 
-ulh_cbmem_append(struct util_log *log, void *buf, int len) 
-{
-    struct cbmem *cbmem;
-    int rc;
-
-    cbmem = (struct cbmem *) log->ul_ulh->ulh_arg;
-
-    rc = cbmem_append(cbmem, buf, len);
-    if (rc != 0) {
-        goto err;
-    }
-
-    return (0);
-err:
-    return (rc);
-}
-
-static int 
-ulh_cbmem_read(struct util_log *log, void *dptr, void *buf, uint16_t offset, 
-        uint16_t len) 
-{
-    struct cbmem *cbmem;
-    struct cbmem_entry_hdr *hdr;
-    int rc;
-
-    cbmem = (struct cbmem *) log->ul_ulh->ulh_arg;
-    hdr = (struct cbmem_entry_hdr *) dptr;
-
-    rc = cbmem_read(cbmem, hdr, buf, offset, len);
-
-    return (rc);
-}
-
-static int 
-ulh_cbmem_walk(struct util_log *log, util_log_walk_func_t walk_func, void *arg)
+int 
+log_init(void)
 {
-    struct cbmem *cbmem;
-    struct cbmem_entry_hdr *hdr;
-    struct cbmem_iter iter;
-    int rc;
-
-    cbmem = (struct cbmem *) log->ul_ulh->ulh_arg;
-
-    rc = cbmem_lock_acquire(cbmem);
-    if (rc != 0) {
-        goto err;
-    }
-    
-    cbmem_iter_start(cbmem, &iter);
-    while (1) {
-        hdr = cbmem_iter_next(cbmem, &iter);
-        if (!hdr) {
-            break;
-        }
-
-        rc = walk_func(log, arg, (void *) hdr, hdr->ceh_len);
-        if (rc == 1) {
-            break;
-        }
-    }
-
-    rc = cbmem_lock_release(cbmem);
-    if (rc != 0) {
-        goto err;
-    }
+#ifdef SHELL_PRESENT 
+    shell_cmd_register(&g_shell_log_cmd, "log", shell_log_dump_all_cmd);
+#endif
 
     return (0);
-err:
-    return (rc);
 }
 
-static int 
-ulh_cbmem_flush(struct util_log *log)
+struct log *
+log_list_get_next(struct log *log)
 {
-    struct cbmem *cbmem;
-    int rc;
+    struct log *next;
 
-    cbmem = (struct cbmem *) log->ul_ulh->ulh_arg;
-    
-    rc = cbmem_flush(cbmem);
-    if (rc != 0) {
-        goto err;
+    if (log == NULL) {
+        next = STAILQ_FIRST(&g_log_list);
+    } else {
+        next = STAILQ_NEXT(log, l_next);
     }
 
-    return (0);
-err:
-    return (rc);
-}
-
-int 
-util_log_cbmem_handler_init(struct ul_handler *handler, struct cbmem *cbmem)
-{
-    handler->ulh_read = ulh_cbmem_read;
-    handler->ulh_append = ulh_cbmem_append;
-    handler->ulh_walk = ulh_cbmem_walk;
-    handler->ulh_flush = ulh_cbmem_flush;
-    handler->ulh_arg = (void *) cbmem;
-
-    return (0);
+    return (next);
 }
 
 int 
-util_log_register(char *name, struct util_log *log, struct ul_handler *ulh)
+log_register(char *name, struct log *log, struct log_handler *lh)
 {
-#ifdef SHELL_PRESENT 
-    if (!shell_registered) {
-        /* register the shell */
-        
-        shell_registered = 1;
-        shell_cmd_register(&shell_log_cmd, "log", shell_log_dump_all);
-    }
-#endif
-
-    log->ul_name = name;
-    log->ul_ulh = ulh;
+    log->l_name = name;
+    log->l_log = lh;
 
-    STAILQ_INSERT_TAIL(&g_util_log_list, log, ul_next);
+    STAILQ_INSERT_TAIL(&g_log_list, log, l_next);
 
     return (0);
 }
 
 int
-util_log_append(struct util_log *log, uint8_t *data, uint16_t len)
+log_append(struct log *log, uint8_t *data, uint16_t len)
 {
-    struct ul_entry_hdr *ue;
+    struct log_entry_hdr *ue;
     int rc;
 
-    ue = (struct ul_entry_hdr *) data;
+    ue = (struct log_entry_hdr *) data;
     ue->ue_ts = (int64_t) os_time_get();
 
-    rc = log->ul_ulh->ulh_append(log, data, len + sizeof(*ue));
+    rc = log->l_log->log_append(log, data, len + sizeof(*ue));
     if (rc != 0) {
         goto err;
     }
@@ -233,11 +90,11 @@ err:
 }
 
 int 
-util_log_walk(struct util_log *log, util_log_walk_func_t walk_func, void *arg)
+log_walk(struct log *log, log_walk_func_t walk_func, void *arg)
 {
     int rc;
 
-    rc = log->ul_ulh->ulh_walk(log, walk_func, arg);
+    rc = log->l_log->log_walk(log, walk_func, arg);
     if (rc != 0) {
         goto err;
     }
@@ -248,22 +105,22 @@ err:
 }
 
 int 
-util_log_read(struct util_log *log, void *dptr, void *buf, uint16_t off, 
+log_read(struct log *log, void *dptr, void *buf, uint16_t off, 
         uint16_t len)
 {
     int rc;
 
-    rc = log->ul_ulh->ulh_read(log, dptr, buf, off, len);
+    rc = log->l_log->log_read(log, dptr, buf, off, len);
 
     return (rc);
 }
 
 int 
-util_log_flush(struct util_log *log)
+log_flush(struct log *log)
 {
     int rc;
 
-    rc = log->ul_ulh->ulh_flush(log);
+    rc = log->l_log->log_flush(log);
     if (rc != 0) {
         goto err;
     } 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/d4e843a6/sys/log/src/log_cbmem.c
----------------------------------------------------------------------
diff --git a/sys/log/src/log_cbmem.c b/sys/log/src/log_cbmem.c
new file mode 100644
index 0000000..19ae417
--- /dev/null
+++ b/sys/log/src/log_cbmem.c
@@ -0,0 +1,126 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <os/os.h>
+
+#include <util/cbmem.h>
+
+#include "log/log.h"
+
+
+static int 
+log_cbmem_append(struct log *log, void *buf, int len) 
+{
+    struct cbmem *cbmem;
+    int rc;
+
+    cbmem = (struct cbmem *) log->l_log->log_arg;
+
+    rc = cbmem_append(cbmem, buf, len);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+static int 
+log_cbmem_read(struct log *log, void *dptr, void *buf, uint16_t offset, 
+        uint16_t len) 
+{
+    struct cbmem *cbmem;
+    struct cbmem_entry_hdr *hdr;
+    int rc;
+
+    cbmem = (struct cbmem *) log->l_log->log_arg;
+    hdr = (struct cbmem_entry_hdr *) dptr;
+
+    rc = cbmem_read(cbmem, hdr, buf, offset, len);
+
+    return (rc);
+}
+
+static int 
+log_cbmem_walk(struct log *log, log_walk_func_t walk_func, void *arg)
+{
+    struct cbmem *cbmem;
+    struct cbmem_entry_hdr *hdr;
+    struct cbmem_iter iter;
+    int rc;
+
+    cbmem = (struct cbmem *) log->l_log->log_arg;
+
+    rc = cbmem_lock_acquire(cbmem);
+    if (rc != 0) {
+        goto err;
+    }
+    
+    cbmem_iter_start(cbmem, &iter);
+    while (1) {
+        hdr = cbmem_iter_next(cbmem, &iter);
+        if (!hdr) {
+            break;
+        }
+
+        rc = walk_func(log, arg, (void *) hdr, hdr->ceh_len);
+        if (rc == 1) {
+            break;
+        }
+    }
+
+    rc = cbmem_lock_release(cbmem);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+static int 
+log_cbmem_flush(struct log *log)
+{
+    struct cbmem *cbmem;
+    int rc;
+
+    cbmem = (struct cbmem *) log->l_log->log_arg;
+    
+    rc = cbmem_flush(cbmem);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+int 
+log_cbmem_handler_init(struct log_handler *handler, struct cbmem *cbmem)
+{
+    handler->log_type = LOG_TYPE_MEMORY;
+    handler->log_read = log_cbmem_read;
+    handler->log_append = log_cbmem_append;
+    handler->log_walk = log_cbmem_walk;
+    handler->log_flush = log_cbmem_flush;
+    handler->log_arg = (void *) cbmem;
+
+    return (0);
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/d4e843a6/sys/log/src/log_shell.c
----------------------------------------------------------------------
diff --git a/sys/log/src/log_shell.c b/sys/log/src/log_shell.c
new file mode 100644
index 0000000..bcf8767
--- /dev/null
+++ b/sys/log/src/log_shell.c
@@ -0,0 +1,100 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* This whole file is conditionally compiled based on whether the 
+ * SHELL_PRESENT identity is provided.
+ */
+
+#ifdef SHELL_PRESENT
+
+#include <os/os.h>
+
+#include <string.h>
+
+#include <util/cbmem.h>
+
+#include "log/log.h"
+
+#include <stdio.h>
+
+#include <shell/shell.h>
+#include <console/console.h> 
+
+static int 
+shell_log_dump_entry(struct log *log, void *arg, void *dptr, uint16_t len) 
+{
+    struct log_entry_hdr ueh;
+    char data[128];
+    int dlen;
+    int rc;
+
+    rc = log_read(log, dptr, &ueh, 0, sizeof(ueh)); 
+    if (rc != sizeof(ueh)) {
+        goto err;
+    }
+
+    dlen = min(len-sizeof(ueh), 128);
+
+    rc = log_read(log, dptr, data, sizeof(ueh), dlen);
+    if (rc < 0) {
+        goto err;
+    }
+    data[rc] = 0;
+
+    /* XXX: This is evil.  newlib printf does not like 64-bit 
+     * values, and this causes memory to be overwritten.  Cast to a 
+     * unsigned 32-bit value for now.
+     */
+    console_printf("[%lu] %s\n", (unsigned long) ueh.ue_ts, data);
+
+    return (0);
+err:
+    return (rc);
+}
+
+int 
+shell_log_dump_all_cmd(int argc, char **argv)
+{
+    struct log *log;
+    int rc;
+
+    log = NULL;
+    while (1) {
+        log = log_list_get_next(log);
+        if (log == NULL) {
+            break;
+        }
+
+        if (log->l_log->log_type == LOG_TYPE_STREAM) {
+            continue;
+        }
+
+        console_printf("Dumping log %s\n", log->l_name);
+
+        rc = log_walk(log, shell_log_dump_entry, NULL);
+        if (rc != 0) {
+            goto err;
+        }
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+#endif 
+
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/d4e843a6/sys/stats/src/stats_shell.c
----------------------------------------------------------------------
diff --git a/sys/stats/src/stats_shell.c b/sys/stats/src/stats_shell.c
index 66469c6..20f3bd7 100644
--- a/sys/stats/src/stats_shell.c
+++ b/sys/stats/src/stats_shell.c
@@ -74,6 +74,7 @@ shell_stats_display(int argc, char **argv)
                 "possible names are:\n");
         stats_group_walk(stats_shell_display_group, NULL);
         rc = OS_EINVAL;
+        goto err;
     }
 
     hdr = stats_group_find(name);