tinydtls: add android logging support
authorHauke Mehrtens <hauke.mehrtens@lantiq.com>
Tue, 22 Sep 2015 09:46:53 +0000 (11:46 +0200)
committerSachin Agrawal <sachin.agrawal@intel.com>
Fri, 2 Oct 2015 22:50:54 +0000 (22:50 +0000)
This patch forwards the logging messages form tinydtls to the normal
android logging system instead of writing them to stdout and stderr.
This is activated in non release builds.

Change-Id: I56cf6752267dc512bf8252dfaaf957d0f87e5bf1
Signed-off-by: Hauke Mehrtens <hauke.mehrtens@lantiq.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2925
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
(cherry picked from commit f713811ed4f6c7ecbb0d328759ef37c12bfca4a6)
Reviewed-on: https://gerrit.iotivity.org/gerrit/3433

extlibs/tinydtls/debug.c

index 48c865d..589c091 100644 (file)
 #include <time.h>
 #endif
 
+#ifdef __ANDROID__
+#include <android/log.h>
+#endif
+
 #include "global.h"
 #include "debug.h"
 
@@ -65,9 +69,21 @@ dtls_set_log_level(log_t level) {
 }
 
 /* this array has the same order as the type log_t */
+#ifdef __ANDROID__
+static android_LogPriority loglevels_android[] = {
+  ANDROID_LOG_FATAL,
+  ANDROID_LOG_ERROR,
+  ANDROID_LOG_ERROR,
+  ANDROID_LOG_WARN,
+  ANDROID_LOG_INFO,
+  ANDROID_LOG_INFO,
+  ANDROID_LOG_DEBUG
+};
+#else
 static char *loglevels[] = {
   "EMRG", "ALRT", "CRIT", "WARN", "NOTE", "INFO", "DEBG" 
 };
+#endif
 
 #ifdef HAVE_TIME_H
 
@@ -207,7 +223,19 @@ dsrv_print_addr(const session_t *addr, char *buf, size_t len) {
 #endif
 }
 
-#ifndef WITH_CONTIKI
+#ifdef __ANDROID__
+void
+dsrv_log(log_t level, char *format, ...) {
+  va_list ap;
+
+  if (maxlog < level)
+    return;
+
+  va_start(ap, format);
+  __android_log_vprint(loglevels_android[level], PACKAGE_NAME, format, ap);
+  va_end(ap);
+}
+#elif !defined (WITH_CONTIKI)
 void 
 dsrv_log(log_t level, char *format, ...) {
   static char timebuf[32];
@@ -289,7 +317,41 @@ void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr)
   dsrv_log(level, "%s: %s\n", name, addrbuf);
 }
 
-#ifndef WITH_CONTIKI
+#ifdef __ANDROID__
+void
+dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, size_t length, int extend) {
+  char *hex_dump_text;
+  char *p;
+  int ret;
+  int size;
+
+  if (maxlog < level)
+    return;
+
+  size = length * 3 + strlen(name) + 22;
+  hex_dump_text = malloc(size);
+  if (!hex_dump_text)
+    return;
+
+  p = hex_dump_text;
+
+  ret = snprintf(p, size, "%s: (%zu bytes): ", name, length);
+  if (ret >= size)
+    goto print;
+  p += ret;
+  size -= ret;
+  while (length--) {
+    ret = snprintf(p, size, "%02X ", *buf++);
+    if (ret >= size)
+      goto print;
+    p += ret;
+    size -= ret;
+  }
+print:
+  __android_log_print(loglevels_android[level], PACKAGE_NAME, "%s\n", hex_dump_text);
+  free(hex_dump_text);
+}
+#elif !defined (WITH_CONTIKI)
 void 
 dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, size_t length, int extend) {
   static char timebuf[32];