Detect android liblog and use android's log functions for debug output
authorJan Schmidt <jan@centricular.com>
Tue, 26 Aug 2014 03:10:04 +0000 (13:10 +1000)
committerJan Schmidt <jan@centricular.com>
Tue, 26 Aug 2014 05:32:02 +0000 (15:32 +1000)
configure.ac
orc/Makefile.am
orc/orcdebug.c

index 32791fe..69492c9 100644 (file)
@@ -77,6 +77,13 @@ AC_CHECK_LIB(rt, clock_gettime,
    )
 AC_SUBST(LIBRT)
 
+dnl Android liblog
+AC_CHECK_LIB(log, __android_log_print,
+   AC_DEFINE(HAVE_ANDROID_LIBLOG, 1, [Defined if we have __android_log_print()])
+   LIBLOG=-llog
+   )
+AC_SUBST(LIBLOG)
+
 AC_CACHE_CHECK(for monotonic clocks,
     orc_cv_monotonic_clock,AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
 #include <time.h>
index 82973e6..84c36cd 100644 (file)
@@ -3,7 +3,7 @@ pkgincludedir = $(includedir)/orc-@ORC_MAJORMINOR@/orc
 
 lib_LTLIBRARIES = liborc-@ORC_MAJORMINOR@.la
 
-liborc_@ORC_MAJORMINOR@_la_LIBADD = $(LIBM) $(LIBRT) $(PTHREAD_LIBS)
+liborc_@ORC_MAJORMINOR@_la_LIBADD = $(LIBM) $(LIBRT) $(PTHREAD_LIBS) $(LIBLOG)
 liborc_@ORC_MAJORMINOR@_la_LDFLAGS = $(ORC_LDFLAGS)
 liborc_@ORC_MAJORMINOR@_la_CFLAGS = $(ORC_CFLAGS) \
        -DORC_ENABLE_UNSTABLE_API
index c4aa7be..2e69464 100644 (file)
 #include <stdarg.h>
 #include <stdlib.h>
 
+#ifdef HAVE_ANDROID_LIBLOG
+#include <android/log.h>
+#endif
+
 /**
  * SECTION:orcdebug
  * @title: OrcDebug
@@ -66,6 +70,46 @@ _orc_debug_init(void)
   ORC_INFO ("orc-" VERSION " debug init");
 }
 
+#ifdef HAVE_ANDROID_LIBLOG
+static void
+orc_debug_print_valist (int level, const char *file, const char *func,
+        int line, const char *format, va_list args)
+{
+  int android_log_level;
+  char *dbg;
+
+  if (level > orc_debug_get_level())
+    return;
+
+  switch (level) {
+    case ORC_DEBUG_ERROR:
+      android_log_level = ANDROID_LOG_ERROR;
+      break;
+    case ORC_DEBUG_WARNING:
+      android_log_level = ANDROID_LOG_WARN;
+      break;
+    case ORC_DEBUG_INFO:
+      android_log_level = ANDROID_LOG_INFO;
+      break;
+    case ORC_DEBUG_LOG:
+      android_log_level = ANDROID_LOG_DEBUG;
+      break;
+    default:
+      android_log_level = ANDROID_LOG_VERBOSE;
+      break;
+  }
+
+  if (vasprintf (&dbg, format, args) < 0) {
+    __android_log_print (android_log_level, "Orc", "Failed to alloc debug string....");
+    return;
+  }
+
+  __android_log_print (android_log_level, "Orc",
+        "%s:%d:%s %s\n", file, line, func, dbg);
+
+  free (dbg);
+}
+#else
 static void
 orc_debug_print_valist (int level, const char *file, const char *func,
         int line, const char *format, va_list args)
@@ -84,6 +128,7 @@ orc_debug_print_valist (int level, const char *file, const char *func,
   vfprintf (stderr, format, args);
   fprintf (stderr, "\n");
 }
+#endif
 
 void
 orc_debug_print (int level, const char *file, const char *func,