]> git.sesse.net Git - vlc/blob - modules/misc/logger.c
3652d94a02a3af93f6810b9fdfe5a9befe917ac0
[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 #include <vlc_fs.h>
36 #include <vlc_charset.h>
37
38 #include <stdarg.h>
39 #include <assert.h>
40
41 #ifdef __ANDROID__
42 # include <android/log.h>
43 #endif
44
45 #define LOG_FILE_TEXT "vlc-log.txt"
46 #define LOG_FILE_HTML "vlc-log.html"
47
48 #define TEXT_HEADER "\xEF\xBB\xBF-- logger module started --\n"
49 #define TEXT_FOOTER "-- logger module stopped --\n"
50
51 #define HTML_HEADER \
52     "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
53     "  \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
54     "<html>\n" \
55     "  <head>\n" \
56     "    <title>vlc log</title>\n" \
57     "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" \
58     "  </head>\n" \
59     "  <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
60     "    <pre>\n" \
61     "      <strong>-- logger module started --</strong>\n"
62 #define HTML_FOOTER \
63     "      <strong>-- logger module stopped --</strong>\n" \
64     "    </pre>\n" \
65     "  </body>\n" \
66     "</html>\n"
67
68 #ifdef HAVE_SYSLOG_H
69 #include <syslog.h>
70 #endif
71
72 /*****************************************************************************
73  * intf_sys_t: description and status of log interface
74  *****************************************************************************/
75 struct intf_sys_t
76 {
77     msg_subscription_t *p_sub;
78     FILE *p_file;
79     const char *footer;
80 };
81
82 /*****************************************************************************
83  * Local prototypes
84  *****************************************************************************/
85 static int  Open    ( vlc_object_t * );
86 static void Close   ( vlc_object_t * );
87
88 static void TextPrint(void *, int, const msg_item_t *, const char *, va_list);
89 static void HtmlPrint(void *, int, const msg_item_t *, const char *, va_list);
90 #ifdef HAVE_SYSLOG_H
91 static void SyslogPrint(void *, int, const msg_item_t *, const char *,
92                         va_list);
93 #endif
94 #ifdef __ANDROID__
95 static void AndroidPrint(void *, int, const msg_item_t *, const char *, va_list);
96 #endif
97
98 /*****************************************************************************
99  * Module descriptor
100  *****************************************************************************/
101 static const char *const mode_list[] = { "text", "html"
102 #ifdef HAVE_SYSLOG_H
103 ,"syslog"
104 #endif
105 #ifdef __ANDROID__
106 ,"android"
107 #endif
108 };
109 static const char *const mode_list_text[] = { N_("Text"), "HTML"
110 #ifdef HAVE_SYSLOG_H
111 , "syslog"
112 #endif
113 #ifdef __ANDROID__
114 ,"android"
115 #endif
116 };
117
118 #define LOGMODE_TEXT N_("Log format")
119 #define LOGMODE_LONGTEXT N_("Specify the logging format.")
120
121 #ifdef HAVE_SYSLOG_H
122 #define SYSLOG_FACILITY_TEXT N_("Syslog facility")
123 #define SYSLOG_FACILITY_LONGTEXT N_("Select the syslog facility where logs " \
124   "will be forwarded.")
125
126 /* First in list is the default facility used. */
127 #define DEFINE_SYSLOG_FACILITY \
128   DEF( "user",   LOG_USER ), \
129   DEF( "daemon", LOG_DAEMON ), \
130   DEF( "local0", LOG_LOCAL0 ), \
131   DEF( "local1", LOG_LOCAL1 ), \
132   DEF( "local2", LOG_LOCAL2 ), \
133   DEF( "local3", LOG_LOCAL3 ), \
134   DEF( "local4", LOG_LOCAL4 ), \
135   DEF( "local5", LOG_LOCAL5 ), \
136   DEF( "local6", LOG_LOCAL6 ), \
137   DEF( "local7", LOG_LOCAL7 )
138
139 #define DEF( a, b ) a
140 static const char *const fac_name[]   = { DEFINE_SYSLOG_FACILITY };
141 #undef  DEF
142 #define DEF( a, b ) b
143 static const int         fac_number[] = { DEFINE_SYSLOG_FACILITY };
144 #undef  DEF
145 enum                   { fac_entries = sizeof(fac_name)/sizeof(fac_name[0]) };
146 #undef  DEFINE_SYSLOG_FACILITY
147
148 #endif
149
150 #define LOGVERBOSE_TEXT N_("Verbosity")
151 #define LOGVERBOSE_LONGTEXT N_("Select the verbosity to use for log or -1 to " \
152 "use the same verbosity given by --verbose.")
153
154 vlc_module_begin ()
155     set_shortname( N_( "Logging" ) )
156     set_description( N_("File logging") )
157
158     set_category( CAT_ADVANCED )
159     set_subcategory( SUBCAT_ADVANCED_MISC )
160
161     add_savefile( "logfile", NULL,
162              N_("Log filename"), N_("Specify the log filename."), false )
163     add_string( "logmode", "text", LOGMODE_TEXT, LOGMODE_LONGTEXT,
164                 false )
165         change_string_list( mode_list, mode_list_text, 0 )
166 #ifdef HAVE_SYSLOG_H
167     add_string( "syslog-facility", fac_name[0], SYSLOG_FACILITY_TEXT,
168                 SYSLOG_FACILITY_LONGTEXT, true )
169         change_string_list( fac_name, fac_name, 0 )
170 #endif
171     add_integer( "log-verbose", -1, LOGVERBOSE_TEXT, LOGVERBOSE_LONGTEXT,
172            false )
173     
174     add_obsolete_string( "rrd-file" )
175
176     set_capability( "interface", 0 )
177     set_callbacks( Open, Close )
178 vlc_module_end ()
179
180 /*****************************************************************************
181  * Open: initialize and create stuff
182  *****************************************************************************/
183 static int Open( vlc_object_t *p_this )
184 {
185     intf_thread_t *p_intf = (intf_thread_t *)p_this;
186     intf_sys_t *p_sys;
187
188     CONSOLE_INTRO_MSG;
189     msg_Info( p_intf, "using logger." );
190
191     /* Allocate instance and initialize some members */
192     p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
193     if( p_sys == NULL )
194         return VLC_ENOMEM;
195
196     p_sys->p_file = NULL;
197     msg_callback_t cb = TextPrint;
198     const char *filename = LOG_FILE_TEXT, *header = TEXT_HEADER;
199     p_sys->footer = TEXT_FOOTER;
200
201     char *mode = var_InheritString( p_intf, "logmode" );
202     if( mode != NULL )
203     {
204         if( !strcmp( mode, "html" ) )
205         {
206             p_sys->footer = HTML_FOOTER;
207             header = HTML_HEADER;
208             cb = HtmlPrint;
209         }
210 #ifdef HAVE_SYSLOG_H
211         else if( !strcmp( mode, "syslog" ) )
212             cb = SyslogPrint;
213 #endif
214 #ifdef __ANDROID__
215         else if( !strcmp( mode, "android" ) )
216             cb = AndroidPrint;
217 #endif
218         else if( strcmp( mode, "text" ) )
219             msg_Warn( p_intf, "invalid log mode `%s', using `text'", mode );
220         free( mode );
221     }
222
223 #ifdef HAVE_SYSLOG_H
224     if( cb == SyslogPrint )
225     {
226         int i_facility;
227         char *psz_facility = var_InheritString( p_intf, "syslog-facility" );
228         if( psz_facility )
229         {
230             bool b_valid = 0;
231             for( size_t i = 0; i < fac_entries; ++i )
232             {
233                 if( !strcmp( psz_facility, fac_name[i] ) )
234                 {
235                     i_facility = fac_number[i];
236                     b_valid = 1;
237                     break;
238                 }
239             }
240             if( !b_valid )
241             {
242                 msg_Warn( p_intf, "invalid syslog facility `%s', using `%s'",
243                           psz_facility, fac_name[0] );
244                 i_facility = fac_number[0];
245             }
246             free( psz_facility );
247         }
248         else
249         {
250             msg_Warn( p_intf, "no syslog facility specified, using `%s'",
251                       fac_name[0] );
252             i_facility = fac_number[0];
253         }
254
255         openlog( "vlc", LOG_PID|LOG_NDELAY, i_facility );
256         p_sys->p_file = NULL;
257     }
258     else
259 #endif
260 #ifdef __ANDROID__
261     if( cb == AndroidPrint )
262     {
263         /* nothing to do */
264     }
265     else
266 #endif
267     {
268         char *psz_file = var_InheritString( p_intf, "logfile" );
269         if( !psz_file )
270         {
271 #ifdef __APPLE__
272 # define LOG_DIR "Library/Logs"
273             char *home = config_GetUserDir(VLC_HOME_DIR);
274             if( home == NULL
275              || asprintf( &psz_file, "%s/"LOG_DIR"/%s", home,
276                           filename ) == -1 )
277                 psz_file = NULL;
278             free(home);
279             filename = psz_file;
280 #endif
281             msg_Warn( p_intf, "no log filename provided, using `%s'",
282                       filename );
283         }
284         else
285             filename = psz_file;
286
287         /* Open the log file and remove any buffering for the stream */
288         msg_Dbg( p_intf, "opening logfile `%s'", filename );
289         p_sys->p_file = vlc_fopen( filename, "at" );
290         free( psz_file );
291         if( p_sys->p_file == NULL )
292         {
293             msg_Err( p_intf, "error opening logfile `%s': %m", filename );
294             free( p_sys );
295             return VLC_EGENERIC;
296         }
297         setvbuf( p_sys->p_file, NULL, _IONBF, 0 );
298         fputs( header, p_sys->p_file );
299     }
300
301     p_sys->p_sub = vlc_Subscribe( cb, p_intf );
302     return VLC_SUCCESS;
303 }
304
305 /*****************************************************************************
306  * Close: destroy interface stuff
307  *****************************************************************************/
308 static void Close( vlc_object_t *p_this )
309 {
310     intf_thread_t *p_intf = (intf_thread_t *)p_this;
311     intf_sys_t *p_sys = p_intf->p_sys;
312
313     /* Flush the queue and unsubscribe from the message queue */
314     vlc_Unsubscribe( p_sys->p_sub );
315
316     /* Close the log file */
317 #ifdef HAVE_SYSLOG_H
318     if( p_sys->p_file == NULL )
319         closelog();
320     else
321 #endif
322     if( p_sys->p_file )
323     {
324         fputs( p_sys->footer, p_sys->p_file );
325         fclose( p_sys->p_file );
326     }
327
328     /* Destroy structure */
329     free( p_sys );
330 }
331
332 static bool IgnoreMessage( intf_thread_t *p_intf, int type )
333 {
334     /* TODO: cache value... */
335     int verbosity = var_InheritInteger( p_intf, "log-verbose" );
336     if (verbosity == -1)
337         verbosity = var_InheritInteger( p_intf, "verbose" );
338
339     return verbosity < 0 || verbosity < (type - VLC_MSG_ERR);
340 }
341
342 /*
343  * Logging callbacks
344  */
345
346 static const char ppsz_type[4][9] = {
347     "",
348     " error",
349     " warning",
350     " debug",
351 };
352
353 #ifdef __ANDROID__
354 static const android_LogPriority prioritytype[4] = {
355     ANDROID_LOG_INFO,
356     ANDROID_LOG_ERROR,
357     ANDROID_LOG_WARN,
358     ANDROID_LOG_DEBUG
359 };
360
361 static void AndroidPrint( void *opaque, int type, const msg_item_t *item,
362                        const char *fmt, va_list ap )
363 {
364     (void)item;
365     intf_thread_t *p_intf = opaque;
366
367     if( IgnoreMessage( p_intf, type ) )
368         return;
369
370     int canc = vlc_savecancel();
371     __android_log_vprint(prioritytype[type], "VLC", fmt, ap);
372     vlc_restorecancel( canc );
373 }
374 #endif
375
376 static void TextPrint( void *opaque, int type, const msg_item_t *item,
377                        const char *fmt, va_list ap )
378 {
379     intf_thread_t *p_intf = opaque;
380     FILE *stream = p_intf->p_sys->p_file;
381
382     if( IgnoreMessage( p_intf, type ) )
383         return;
384
385     int canc = vlc_savecancel();
386     flockfile( stream );
387     fprintf( stream, "%s%s: ", item->psz_module, ppsz_type[type] );
388     vfprintf( stream, fmt, ap );
389     putc_unlocked( '\n', stream );
390     funlockfile( stream );
391     vlc_restorecancel( canc );
392 }
393
394 #ifdef HAVE_SYSLOG_H
395 static void SyslogPrint( void *opaque, int type, const msg_item_t *item,
396                          const char *fmt, va_list ap )
397 {
398     static const int i_prio[4] = { LOG_INFO, LOG_ERR, LOG_WARNING, LOG_DEBUG };
399
400     intf_thread_t *p_intf = opaque;
401     char *str;
402     int i_priority = i_prio[type];
403
404     if( IgnoreMessage( p_intf, type )
405      || unlikely(vasprintf( &str, fmt, ap ) == -1) )
406         return;
407
408     int canc = vlc_savecancel();
409     if( item->psz_header != NULL )
410         syslog( i_priority, "[%s] %s%s: %s", item->psz_header,
411                 item->psz_module, ppsz_type[type], str );
412     else
413         syslog( i_priority, "%s%s: %s",
414                 item->psz_module, ppsz_type[type], str );
415     vlc_restorecancel( canc );
416     free( str );
417 }
418 #endif
419
420 static void HtmlPrint( void *opaque, int type, const msg_item_t *item,
421                        const char *fmt, va_list ap )
422 {
423     static const unsigned color[4] = {
424         0xffffff, 0xff6666, 0xffff66, 0xaaaaaa,
425     };
426
427     intf_thread_t *p_intf = opaque;
428     FILE *stream = p_intf->p_sys->p_file;
429
430     if( IgnoreMessage( p_intf, type ) )
431         return;
432
433     int canc = vlc_savecancel();
434     flockfile( stream );
435     fprintf( stream, "%s%s: <span style=\"color: #%06x\">",
436              item->psz_module, ppsz_type[type], color[type] );
437     /* FIXME: encode special ASCII characters */
438     vfprintf( stream, fmt, ap );
439     fputs( "</span>\n", stream );
440     funlockfile( stream );
441     vlc_restorecancel( canc );
442 }