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