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