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