]> git.sesse.net Git - vlc/blob - modules/logger/android.c
qt4: fix COM leak and handle errors
[vlc] / modules / logger / android.c
1 /*****************************************************************************
2  * android.c: Android logger using logcat
3  *****************************************************************************
4  * Copyright © 2015 VLC authors and VideoLAN
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifndef __ANDROID__
22 #error __ANDROID__ not defined
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <android/log.h>
30
31 #include <stdarg.h>
32 #include <stdio.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36
37 static const int ptr_width = 2 * /* hex digits */ sizeof (uintptr_t);
38
39 static void AndroidPrintMsg(void *opaque, int type, const vlc_log_t *p_item,
40                             const char *format, va_list ap)
41 {
42     int prio;
43     char *format2;
44     int verbose = (intptr_t)opaque;
45
46     if (verbose < type)
47         return;
48
49     int canc = vlc_savecancel();
50
51     if (asprintf(&format2, "[%0*"PRIxPTR"] %s %s: %s",
52                  ptr_width, p_item->i_object_id, p_item->psz_module,
53                  p_item->psz_object_type, format) < 0)
54         return;
55     switch (type) {
56         case VLC_MSG_INFO:
57             prio = ANDROID_LOG_INFO;
58             break;
59         case VLC_MSG_ERR:
60             prio = ANDROID_LOG_ERROR;
61             break;
62         case VLC_MSG_WARN:
63             prio = ANDROID_LOG_WARN;
64             break;
65         default:
66         case VLC_MSG_DBG:
67             prio = ANDROID_LOG_DEBUG;
68     }
69     __android_log_vprint(prio, "VLC", format2, ap);
70     free(format2);
71     vlc_restorecancel(canc);
72 }
73
74 static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
75 {
76     int verbosity = var_InheritInteger(obj, "verbose");
77
78     if (verbosity < 0)
79         return NULL;
80
81     *sysp = (void *)(uintptr_t)verbosity;
82
83     return AndroidPrintMsg;
84 }
85
86 vlc_module_begin()
87     set_shortname(N_("Android log"))
88     set_description(N_("Android log using logcat"))
89     set_category(CAT_ADVANCED)
90     set_subcategory(SUBCAT_ADVANCED_MISC)
91     set_capability("logger", 30)
92     set_callbacks(Open, NULL)
93 vlc_module_end ()