[vlc-devel] [PATCH 3/3] android: convert Android logger to module

Thomas Guillem thomas at gllm.fr
Mon Feb 9 11:10:43 CET 2015


---
 modules/logger/Makefile.am |  6 ++++
 modules/logger/android.c   | 85 ++++++++++++++++++++++++++++++++++++++++++++++
 src/misc/messages.c        | 45 ------------------------
 3 files changed, 91 insertions(+), 45 deletions(-)
 create mode 100644 modules/logger/android.c

diff --git a/modules/logger/Makefile.am b/modules/logger/Makefile.am
index 4c77999..79f5ad6 100644
--- a/modules/logger/Makefile.am
+++ b/modules/logger/Makefile.am
@@ -18,3 +18,9 @@ libsd_journal_plugin_la_LIBADD = $(SYSTEMD_LIBS)
 if HAVE_SYSTEMD
 logger_LTLIBRARIES += libsd_journal_plugin.la
 endif
+
+libandroid_logger_plugin_la_SOURCES = logger/android.c
+libandroid_logger_plugin_la_CFLAGS = $(AM_CFLAGS)
+if HAVE_ANDROID
+logger_LTLIBRARIES += libandroid_logger_plugin.la
+endif
diff --git a/modules/logger/android.c b/modules/logger/android.c
new file mode 100644
index 0000000..f8369ff
--- /dev/null
+++ b/modules/logger/android.c
@@ -0,0 +1,85 @@
+/*****************************************************************************
+ * android.c: Android logger using logcat
+ *****************************************************************************
+ * Copyright © 2015 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef __ANDROID__
+#error __ANDROID__ not defined
+#endif
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <android/log.h>
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+
+static const int ptr_width = 2 * /* hex digits */ sizeof (uintptr_t);
+
+static void AndroidPrintMsg(void *d, int type, const vlc_log_t *p_item,
+                            const char *format, va_list ap)
+{
+    int prio;
+    char *format2;
+
+    int canc = vlc_savecancel();
+
+    if (asprintf(&format2, "[%0*"PRIxPTR"] %s %s: %s",
+                 ptr_width, p_item->i_object_id, p_item->psz_module,
+                 p_item->psz_object_type, format) < 0)
+        return;
+    switch (type) {
+        case VLC_MSG_INFO:
+            prio = ANDROID_LOG_INFO;
+            break;
+        case VLC_MSG_ERR:
+            prio = ANDROID_LOG_ERROR;
+            break;
+        case VLC_MSG_WARN:
+            prio = ANDROID_LOG_WARN;
+            break;
+        default:
+        case VLC_MSG_DBG:
+            prio = ANDROID_LOG_DEBUG;
+    }
+    __android_log_vprint(prio, "VLC", format2, ap);
+    free(format2);
+    vlc_restorecancel(canc);
+}
+
+static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
+{
+    VLC_UNUSED(obj);
+    VLC_UNUSED(sysp);
+
+    return AndroidPrintMsg;
+}
+
+vlc_module_begin()
+    set_shortname(N_("Android log"))
+    set_description(N_("Android log using logcat"))
+    set_category(CAT_ADVANCED)
+    set_subcategory(SUBCAT_ADVANCED_MISC)
+    set_capability("logger", 30)
+    set_callbacks(Open, NULL)
+vlc_module_end ()
diff --git a/src/misc/messages.c b/src/misc/messages.c
index 3679ade..e22e4fe7 100644
--- a/src/misc/messages.c
+++ b/src/misc/messages.c
@@ -43,10 +43,6 @@
 #include <vlc_modules.h>
 #include "../libvlc.h"
 
-#ifdef __ANDROID__
-#include <android/log.h>
-#endif
-
 struct vlc_logger_t
 {
     VLC_COMMON_MEMBERS
@@ -205,43 +201,6 @@ static void Win32DebugOutputMsg (void* d, int type, const vlc_log_t *p_item,
 }
 #endif
 
-#ifdef __ANDROID__
-static void AndroidPrintMsg (void *d, int type, const vlc_log_t *p_item,
-                             const char *format, va_list ap)
-{
-    int verbose = (intptr_t)d;
-    int prio;
-
-    if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
-        return;
-
-    int canc = vlc_savecancel ();
-
-    char *format2;
-    if (asprintf (&format2, "[%0*"PRIxPTR"] %s %s: %s",
-                  ptr_width, p_item->i_object_id, p_item->psz_module,
-                  p_item->psz_object_type, format) < 0)
-        return;
-    switch (type) {
-        case VLC_MSG_INFO:
-            prio = ANDROID_LOG_INFO;
-            break;
-        case VLC_MSG_ERR:
-            prio = ANDROID_LOG_ERROR;
-            break;
-        case VLC_MSG_WARN:
-            prio = ANDROID_LOG_WARN;
-            break;
-        default:
-        case VLC_MSG_DBG:
-            prio = ANDROID_LOG_DEBUG;
-    }
-    __android_log_vprint (prio, "VLC", format2, ap);
-    free (format2);
-    vlc_restorecancel (canc);
-}
-#endif
-
 typedef struct vlc_log_early_t
 {
     struct vlc_log_early_t *next;
@@ -403,11 +362,7 @@ int vlc_LogInit(libvlc_int_t *vlc)
     module_t *module = vlc_module_load(logger, "logger", NULL, false,
                                        vlc_logger_load, logger, &cb, &sys);
     if (module == NULL)
-#ifdef __ANDROID__
-        cb = AndroidPrintMsg, sys = NULL;
-#else
         cb = vlc_vaLogDiscard;
-#endif
 
     vlc_rwlock_wrlock(&logger->lock);
     if (logger->log == vlc_vaLogEarly)
-- 
2.1.3




More information about the vlc-devel mailing list