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