]> git.sesse.net Git - vlc/blob - modules/misc/logger.c
bc171287a254be8c04c98f20f60b20237682f595
[vlc] / modules / misc / logger.c
1 /*****************************************************************************
2  * logger.c : file logging plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35
36 #include <stdarg.h>
37
38 #ifdef __ANDROID__
39 # include <android/log.h>
40 #endif
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  Open    ( vlc_object_t * );
46 static void Close   ( vlc_object_t * );
47
48 #ifdef __ANDROID__
49 static void AndroidPrint(void *, int, const vlc_log_t *, const char *, va_list);
50 #endif
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 vlc_module_begin ()
56     set_shortname( N_( "Logging" ) )
57     set_description( N_("File logging") )
58
59     set_category( CAT_ADVANCED )
60     set_subcategory( SUBCAT_ADVANCED_MISC )
61
62     add_obsolete_string( "rrd-file" )
63
64     set_capability( "interface", 0 )
65     set_callbacks( Open, Close )
66 vlc_module_end ()
67
68 /*****************************************************************************
69  * Open: initialize and create stuff
70  *****************************************************************************/
71 static int Open( vlc_object_t *p_this )
72 {
73     intf_thread_t *p_intf = (intf_thread_t *)p_this;
74
75 #ifdef __ANDROID__
76     msg_Info( p_this, "using logger." );
77
78     vlc_LogSet( p_intf->p_libvlc, AndroidPrint, p_intf );
79     return VLC_SUCCESS;
80 #else
81     msg_Err( p_intf, "The logger interface no longer exists." );
82     msg_Info( p_intf, "As of VLC version 0.9.0, use --file-logging to write "
83               "logs to a file." );
84 # ifndef _WIN32
85     msg_Info( p_intf, "Use --syslog to send logs to the system logger." );
86 # endif
87     return VLC_EGENERIC;
88 #endif
89 }
90
91 /*****************************************************************************
92  * Close: destroy interface stuff
93  *****************************************************************************/
94 static void Close( vlc_object_t *p_this )
95 {
96     /* Flush the queue and unsubscribe from the message queue */
97     vlc_LogSet( p_this->p_libvlc, NULL, NULL );
98 }
99
100 #ifdef __ANDROID__
101 static bool IgnoreMessage( intf_thread_t *p_intf, int type )
102 {
103     /* TODO: cache value... */
104     int verbosity = var_InheritInteger( p_intf, "log-verbose" );
105     if (verbosity == -1)
106         verbosity = var_InheritInteger( p_intf, "verbose" );
107
108     return verbosity < 0 || verbosity < (type - VLC_MSG_ERR);
109 }
110
111 /*
112  * Logging callbacks
113  */
114
115 static const char ppsz_type[4][9] = {
116     "",
117     " error",
118     " warning",
119     " debug",
120 };
121
122 static const android_LogPriority prioritytype[4] = {
123     ANDROID_LOG_INFO,
124     ANDROID_LOG_ERROR,
125     ANDROID_LOG_WARN,
126     ANDROID_LOG_DEBUG
127 };
128
129 static void AndroidPrint( void *opaque, int type, const vlc_log_t *item,
130                        const char *fmt, va_list ap )
131 {
132     (void)item;
133     intf_thread_t *p_intf = opaque;
134
135     if( IgnoreMessage( p_intf, type ) )
136         return;
137
138     int canc = vlc_savecancel();
139     __android_log_vprint(prioritytype[type], "VLC", fmt, ap);
140     vlc_restorecancel( canc );
141 }
142 #endif