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