You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by pa...@apache.org on 2016/09/21 17:49:37 UTC

[2/3] incubator-mynewt-core git commit: fix logging so that we use printf during sim, and use logs when debug is defined

fix logging so that we use printf during sim, and use logs when debug is defined


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

Branch: refs/heads/develop
Commit: 29a11be746551e3c2b8c8f17407274669d739b5b
Parents: 7f682f6
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Fri Sep 16 15:54:27 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Wed Sep 21 10:48:28 2016 -0700

----------------------------------------------------------------------
 libs/iotivity/pkg.yml                      |  1 +
 libs/iotivity/src/port/mynewt/ip_adaptor.c | 79 +++++++------------------
 libs/iotivity/src/port/mynewt/log.c        | 43 ++++++++++++++
 libs/iotivity/src/port/oc_log.h            | 27 ++++++++-
 4 files changed, 90 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/29a11be7/libs/iotivity/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/iotivity/pkg.yml b/libs/iotivity/pkg.yml
index e5c95fb..87b4b45 100644
--- a/libs/iotivity/pkg.yml
+++ b/libs/iotivity/pkg.yml
@@ -29,6 +29,7 @@ pkg.deps:
     - "@apache-mynewt-core/sys/mn_socket"
     - "@apache-mynewt-core/sys/log"
 
+# remove debug option to save logging 
 pkg.cflags: -std=c99
         -DDEBUG=1
         -DSECURE=0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/29a11be7/libs/iotivity/src/port/mynewt/ip_adaptor.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/ip_adaptor.c b/libs/iotivity/src/port/mynewt/ip_adaptor.c
index dfb1f9c..9b8bbff 100644
--- a/libs/iotivity/src/port/mynewt/ip_adaptor.c
+++ b/libs/iotivity/src/port/mynewt/ip_adaptor.c
@@ -27,6 +27,7 @@
 #include "../oc_network_events_mutex.h"
 #include "../oc_connectivity.h"
 #include "oc_buffer.h"
+#include "../oc_log.h"
 
 #ifdef OC_SECURITY
 #error This implementation does not yet support security
@@ -53,29 +54,6 @@ struct mn_socket *ucast;
 struct os_sem oc_read_sem;
 struct os_sem oc_write_sem;
 
-/* logging data for this module. TODO, the application should
- * define the logging strategy for this module */
-#define MAX_CBMEM_BUF   (600)
-static uint32_t *cbmem_buf;
-static struct cbmem cbmem;
-struct log oc_log;
-
-static int oc_log_init(void) {
-
-    log_init();
-
-    cbmem_buf = malloc(sizeof(uint32_t) * MAX_CBMEM_BUF);
-    if (cbmem_buf == NULL) {
-        return -1;
-    }
-
-    cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF);
-    log_register("iot", &oc_log, &log_cbmem_handler, &cbmem);
-
-    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "OC Init");
-    return 0;
-}
-
 /* not sure if these semaphores are necessary yet.  If we are running
  * all of this from one task, we may not need these */
 static struct os_mutex oc_net_mutex;
@@ -109,8 +87,7 @@ oc_send_buffer(oc_message_t *message)
     int rc;
 
     while (1) {
-        LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,
-                 "attempt send buffer %u\n", message->length);
+        LOG("attempt send buffer %lu\n", message->length);
 
         to.msin6_len = sizeof(to);
         to.msin6_family = MN_AF_INET6;
@@ -128,8 +105,7 @@ oc_send_buffer(oc_message_t *message)
         rc = mn_sendto(send_sock, &m, (struct mn_sockaddr *) &to);
         /* TODO what to do if this fails, we can't keep the buffer */
         if (rc != 0) {
-            LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                      "Failed sending buffer %u\n", message->length);
+            ERROR("Failed sending buffer %lu\n", message->length);
         } else {
             break;
         }
@@ -146,7 +122,7 @@ oc_attempt_rx(struct mn_socket * rxsock) {
     oc_message_t *message = NULL;
     struct mn_sockaddr_in6 from;
 
-    LOG_DEBUG(&oc_log, LOG_MODULE_IOTIVITY, "attempt rx from %u\n", rxsock);
+    LOG("attempt rx from %p\n", rxsock);
 
     rc= mn_recvfrom(rxsock, &m, (struct mn_sockaddr *) &from);
 
@@ -160,23 +136,22 @@ oc_attempt_rx(struct mn_socket * rxsock) {
 
     pkt = OS_MBUF_PKTHDR(m);
 
-    LOG_DEBUG(&oc_log, LOG_MODULE_IOTIVITY,
-              "rx from %u %p-%u\n", rxsock, pkt, pkt->omp_len);
+    LOG("rx from %p %p-%u\n", rxsock, pkt, pkt->omp_len);
 
     message = oc_allocate_message();
     if (NULL == message) {
-        /* TODO log an error that we dropped a frame */
+        ERROR("Could not allocate OC message buffer\n");
         goto rx_attempt_err;
     }
 
     if (pkt->omp_len > MAX_PAYLOAD_SIZE) {
-        /* TODO what do we do with this */
+        ERROR("Message to large for OC message buffer\n");
         goto rx_attempt_err;
     }
     /* copy to message from mbuf chain */
     rc = os_mbuf_copydata(m, 0, pkt->omp_len, message->data);
     if (rc != 0) {
-        /* TODO what do we do with this */
+        ERROR("Failed to copy message from mbuf to OC message buffer \n");
         goto rx_attempt_err;
     }
 
@@ -189,9 +164,7 @@ oc_attempt_rx(struct mn_socket * rxsock) {
     message->endpoint.ipv6_addr.scope = from.msin6_scope_id;
     message->endpoint.ipv6_addr.port = ntohs(from.msin6_port);
 
-    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "rx from %u len %u\n",
-             rxsock, message->length);
-
+    LOG("Successfully rx from %p len %lu\n", rxsock, message->length);
     return message;
 
     /* add the addr info to the message */
@@ -261,22 +234,19 @@ oc_init_net_task(void) {
     /* start this thing running to check right away */
     rc = os_sem_init(&oc_read_sem, 1);
     if (0 != rc) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not initialize oc read sem\n");
+        ERROR("Could not initialize oc read sem\n");
         return rc;
     }
 
     rc = os_sem_init(&oc_write_sem, 1);
     if (0 != rc) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not initialize oc write sem\n");
+        ERROR("Could not initialize oc write sem\n");
         return rc;
     }
 
     oc_stack = (os_stack_t*) malloc(sizeof(os_stack_t)*OC_NET_TASK_STACK_SIZE);
     if (NULL == oc_stack) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not malloc oc stack\n");
+        ERROR("Could not malloc oc stack\n");
         return -1;
     }
 
@@ -285,7 +255,7 @@ oc_init_net_task(void) {
             oc_stack, OC_NET_TASK_STACK_SIZE);
 
     if (rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY, "Could not start oc task\n");
+        ERROR("Could not start oc task\n");
         free(oc_stack);
     }
 
@@ -295,7 +265,7 @@ oc_init_net_task(void) {
 void
 oc_connectivity_shutdown(void)
 {
-    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "OC shutdown");
+    LOG("OC shutdown");
 
     if (ucast) {
         mn_close(ucast);
@@ -317,21 +287,18 @@ oc_connectivity_init(void)
 
     rc = oc_log_init();
     if ( rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not create oc logging\n");
+        ERROR("Could not create oc logging\n");
         return rc;    }
 
     rc = mn_socket(&ucast, MN_PF_INET6, MN_SOCK_DGRAM, 0);
     if ( rc != 0 || !ucast ) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not create oc unicast socket\n");
+        ERROR("Could not create oc unicast socket\n");
         return rc;
     }
     rc = mn_socket(&mcast, MN_PF_INET6, MN_SOCK_DGRAM, 0);
     if ( rc != 0 || !mcast ) {
         mn_close(ucast);
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not create oc multicast socket\n");
+        ERROR("Could not create oc multicast socket\n");
         return rc;
     }
     mn_socket_set_cbs(ucast, ucast, &oc_sock_cbs);
@@ -345,8 +312,7 @@ oc_connectivity_init(void)
 
     rc = mn_bind(ucast, (struct mn_sockaddr *)&sin);
     if (rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not bind oc unicast socket\n");
+        ERROR("Could not bind oc unicast socket\n");
         goto oc_connectivity_init_err;
     }
 
@@ -369,20 +335,17 @@ oc_connectivity_init(void)
 
         rc = mn_setsockopt(mcast, MN_SO_LEVEL, MN_MCAST_JOIN_GROUP, &join);
         if (rc != 0) {
-            LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                 "Could not join multicast group on %s\n", itf.mif_name);
+            ERROR("Could not join multicast group on %s\n", itf.mif_name);
             continue;
         }
 
-        LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Joined Coap multicast grop on %s\n", itf.mif_name);
+        LOG("Joined Coap multicast grop on %s\n", itf.mif_name);
     }
 
     sin.msin6_port = htons(COAP_PORT_UNSECURED);
     rc = mn_bind(mcast, (struct mn_sockaddr *)&sin);
     if (rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not bind oc multicast socket\n");
+        ERROR("Could not bind oc multicast socket\n");
         goto oc_connectivity_init_err;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/29a11be7/libs/iotivity/src/port/mynewt/log.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/log.c b/libs/iotivity/src/port/mynewt/log.c
new file mode 100644
index 0000000..8c76fc9
--- /dev/null
+++ b/libs/iotivity/src/port/mynewt/log.c
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 <log/log.h>
+
+/* logging data for this module. TODO, the application should
+ * define the logging strategy for this module */
+#define MAX_CBMEM_BUF   (600)
+static uint32_t *cbmem_buf;
+static struct cbmem cbmem;
+struct log oc_log;
+
+int
+oc_log_init(void) {
+
+    log_init();
+
+    cbmem_buf = malloc(sizeof(uint32_t) * MAX_CBMEM_BUF);
+    if (cbmem_buf == NULL) {
+        return -1;
+    }
+
+    cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF);
+    log_register("iot", &oc_log, &log_cbmem_handler, &cbmem);
+
+    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "OC Init");
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/29a11be7/libs/iotivity/src/port/oc_log.h
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/oc_log.h b/libs/iotivity/src/port/oc_log.h
index c03233e..6744499 100644
--- a/libs/iotivity/src/port/oc_log.h
+++ b/libs/iotivity/src/port/oc_log.h
@@ -23,6 +23,20 @@
 #define PRINT(...) printf(__VA_ARGS__)
 
 #define PRINTipaddr(endpoint)                                                  \
+  PRINT(                                                                      \
+    "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%"    \
+    "02x]:%d",                                                                 \
+    ((endpoint).ipv6_addr.address)[0], ((endpoint).ipv6_addr.address)[1],      \
+    ((endpoint).ipv6_addr.address)[2], ((endpoint).ipv6_addr.address)[3],      \
+    ((endpoint).ipv6_addr.address)[4], ((endpoint).ipv6_addr.address)[5],      \
+    ((endpoint).ipv6_addr.address)[6], ((endpoint).ipv6_addr.address)[7],      \
+    ((endpoint).ipv6_addr.address)[8], ((endpoint).ipv6_addr.address)[9],      \
+    ((endpoint).ipv6_addr.address)[10], ((endpoint).ipv6_addr.address)[11],    \
+    ((endpoint).ipv6_addr.address)[12], ((endpoint).ipv6_addr.address)[13],    \
+    ((endpoint).ipv6_addr.address)[14], ((endpoint).ipv6_addr.address)[15],    \
+    (endpoint).ipv6_addr.port)
+
+#define LogMynewtipaddr(endpoint)                                                  \
 {\
   char tmp_buf[16*3+6]; /* 16 octets plus port */                              \
   sprintf(tmp_buf,                                                             \
@@ -40,12 +54,21 @@
     LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "%s", tmp_buf);                     \
 }
 
-#if DEBUG
-#define LOG(...) LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,__VA_ARGS__)
+#if defined(ARCH_sim)
+#define LOG(...) PRINT(__VA_ARGS__)
+#define ERROR(...) LOG(__VA_ARGS__)
 #define LOGipaddr(endpoint) PRINTipaddr(endpoint)
+#define oc_log_init()   0
+#elif defined(DEBUG)
+int oc_log_init(void);
+#define LOG(...) LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,__VA_ARGS__)
+#define ERROR(...) LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,__VA_ARGS__)
+#define LOGipaddr(endpoint) LogMynewtipaddr(endpoint)
 #else
 #define LOG(...)
+#define ERROR(...)
 #define LOGipaddr(endpoint)
+#define oc_log_init()   0
 #endif
 
 #endif /* OC_LOG_H */