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