]> git.sesse.net Git - vlc/blob - modules/misc/logger.c
logger: fix file leak
[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
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 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 LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
56
57 #define TEXT_HEADER "-- logger module started --\n"
58 #define TEXT_FOOTER "-- logger module stopped --\n"
59
60 #define HTML_HEADER \
61     "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
62     "  \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
63     "<html>\n" \
64     "  <head>\n" \
65     "    <title>vlc log</title>\n" \
66     "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" \
67     "  </head>\n" \
68     "  <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
69     "    <pre>\n" \
70     "      <b>-- logger module started --</b>\n"
71 #define HTML_FOOTER \
72     "      <b>-- logger module stopped --</b>\n" \
73     "    </pre>\n" \
74     "  </body>\n" \
75     "</html>\n"
76
77 #if HAVE_SYSLOG_H
78 #include <syslog.h>
79 #endif
80
81 /*****************************************************************************
82  * intf_sys_t: description and status of log interface
83  *****************************************************************************/
84 struct intf_sys_t
85 {
86     int i_mode;
87     struct
88     {
89         FILE *stream;
90         vlc_thread_t thread;
91     } rrd;
92
93     FILE *    p_file; /* The log file */
94     msg_subscription_t *p_sub;
95 };
96
97 /*****************************************************************************
98  * Local prototypes
99  *****************************************************************************/
100 static int  Open    ( vlc_object_t * );
101 static void Close   ( vlc_object_t * );
102 static void Run     ( intf_thread_t * );
103
104 static void FlushQueue        ( msg_subscription_t *, FILE *, int, int );
105 static void TextPrint         ( const msg_item_t *, FILE * );
106 static void HtmlPrint         ( const msg_item_t *, FILE * );
107 #ifdef HAVE_SYSLOG_H
108 static void SyslogPrint       ( const msg_item_t *);
109 #endif
110
111 static void *DoRRD( void * );
112
113 /*****************************************************************************
114  * Module descriptor
115  *****************************************************************************/
116 static const char *const mode_list[] = { "text", "html"
117 #ifdef HAVE_SYSLOG_H
118 ,"syslog"
119 #endif
120 };
121 static const char *const mode_list_text[] = { N_("Text"), "HTML"
122 #ifdef HAVE_SYSLOG_H
123 , "syslog"
124 #endif
125 };
126
127 #define LOGMODE_TEXT N_("Log format")
128 #ifdef HAVE_SYSLOG_H
129 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
130   "\"text\" (default), \"html\", and \"syslog\" (special mode to send to " \
131   "syslog instead of file.")
132 #else
133 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
134   "\"text\" (default) and \"html\".")
135 #endif
136
137 vlc_module_begin();
138     set_shortname( N_( "Logging" ) );
139     set_description( N_("File logging") );
140
141     set_category( CAT_ADVANCED );
142     set_subcategory( SUBCAT_ADVANCED_MISC );
143
144     add_file( "logfile", NULL, NULL,
145              N_("Log filename"), N_("Specify the log filename."), false );
146     add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
147                 false );
148         change_string_list( mode_list, mode_list_text, 0 );
149
150     add_file( "rrd-file", NULL, NULL, N_("RRD output file") ,
151                     N_("Output data for RRDTool in this file." ), true );
152
153     set_capability( "interface", 0 );
154     set_callbacks( Open, Close );
155 vlc_module_end();
156
157 /*****************************************************************************
158  * Open: initialize and create stuff
159  *****************************************************************************/
160 static int Open( vlc_object_t *p_this )
161 {
162     intf_thread_t *p_intf = (intf_thread_t *)p_this;
163     intf_sys_t *p_sys;
164     char *psz_mode, *psz_file, *psz_rrd_file;
165
166     CONSOLE_INTRO_MSG;
167     msg_Info( p_intf, "using logger..." );
168
169     /* Allocate instance and initialize some members */
170     p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
171     if( p_sys == NULL )
172         return VLC_ENOMEM;
173
174     psz_mode = var_CreateGetString( p_intf, "logmode" );
175     if( psz_mode )
176     {
177         if( !strcmp( psz_mode, "text" ) )
178         {
179             p_sys->i_mode = MODE_TEXT;
180         }
181         else if( !strcmp( psz_mode, "html" ) )
182         {
183             p_sys->i_mode = MODE_HTML;
184         }
185 #ifdef HAVE_SYSLOG_H
186         else if( !strcmp( psz_mode, "syslog" ) )
187         {
188             p_sys->i_mode = MODE_SYSLOG;
189         }
190 #endif
191         else
192         {
193             msg_Warn( p_intf, "invalid log mode `%s', using `text'", psz_mode );
194             p_sys->i_mode = MODE_TEXT;
195         }
196
197         free( psz_mode );
198     }
199     else
200     {
201         msg_Warn( p_intf, "no log mode specified, using `text'" );
202         p_intf->p_sys->i_mode = MODE_TEXT;
203     }
204
205     if( p_sys->i_mode != MODE_SYSLOG )
206     {
207         psz_file = config_GetPsz( p_intf, "logfile" );
208         if( !psz_file )
209         {
210 #ifdef __APPLE__
211             if( asprintf( &psz_file, "%s/"LOG_DIR"/%s", config_GetHomeDir(),
212                 (p_sys->i_mode == MODE_HTML) ? LOG_FILE_HTML
213                                              : LOG_FILE_TEXT ) == -1 )
214                 psz_file = NULL;
215 #else
216             switch( p_sys->i_mode )
217             {
218             case MODE_HTML:
219                 psz_file = strdup( LOG_FILE_HTML );
220                 break;
221             case MODE_TEXT:
222             default:
223                 psz_file = strdup( LOG_FILE_TEXT );
224                 break;
225             }
226 #endif
227             msg_Warn( p_intf, "no log filename provided, using `%s'",
228                                psz_file );
229         }
230
231         /* Open the log file and remove any buffering for the stream */
232         msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
233         p_sys->p_file = utf8_fopen( psz_file, "at" );
234         if( p_sys->p_file == NULL )
235         {
236             msg_Err( p_intf, "error opening logfile `%s'", psz_file );
237             free( p_sys );
238             free( psz_file );
239             return -1;
240         }
241         setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
242
243         free( psz_file );
244
245         switch( p_intf->p_sys->i_mode )
246         {
247         case MODE_HTML:
248             LOG_STRING( HTML_HEADER, p_sys->p_file );
249             break;
250         case MODE_TEXT:
251         default:
252             LOG_STRING( TEXT_HEADER, p_sys->p_file );
253             break;
254         }
255
256     }
257     else
258     {
259         p_sys->p_file = NULL;
260 #ifdef HAVE_SYSLOG_H
261         openlog( "vlc", LOG_PID|LOG_NDELAY, LOG_DAEMON );
262 #endif
263     }
264
265     psz_rrd_file = config_GetPsz( p_intf, "rrd-file" );
266     if( psz_rrd_file && *psz_rrd_file )
267     {
268         FILE *rrd = utf8_fopen( psz_rrd_file, "w" );
269         if (rrd != NULL)
270         {
271             setvbuf (rrd, NULL, _IOLBF, BUFSIZ);
272             if (!vlc_clone (&p_sys->rrd.thread, DoRRD, p_intf,
273                             VLC_THREAD_PRIORITY_LOW))
274                 p_sys->rrd.stream = rrd;
275             else
276                 fclose (rrd);
277         }
278     }
279     free( psz_rrd_file );
280
281     p_sys->p_sub = msg_Subscribe( p_intf );
282     p_intf->pf_run = Run;
283
284     return 0;
285 }
286
287 /*****************************************************************************
288  * Close: destroy interface stuff
289  *****************************************************************************/
290 static void Close( vlc_object_t *p_this )
291 {
292     intf_thread_t *p_intf = (intf_thread_t *)p_this;
293     intf_sys_t *p_sys = p_intf->p_sys;
294
295     if (p_sys->rrd.stream)
296     {
297         vlc_cancel (p_sys->rrd.thread);
298         vlc_join (p_sys->rrd.thread, NULL);
299     }
300
301     /* Flush the queue and unsubscribe from the message queue */
302     FlushQueue( p_sys->p_sub, p_sys->p_file,
303                 p_sys->i_mode,
304                 var_CreateGetInteger( p_intf, "verbose" ) );
305     msg_Unsubscribe( p_intf, p_sys->p_sub );
306
307     switch( p_sys->i_mode )
308     {
309     case MODE_HTML:
310         LOG_STRING( HTML_FOOTER, p_sys->p_file );
311         break;
312 #ifdef HAVE_SYSLOG_H
313     case MODE_SYSLOG:
314         closelog();
315         break;
316 #endif
317     case MODE_TEXT:
318     default:
319         LOG_STRING( TEXT_FOOTER, p_sys->p_file );
320         break;
321     }
322
323     /* Close the log file */
324     if( p_sys->i_mode != MODE_SYSLOG )
325         fclose( p_sys->p_file );
326
327     /* Destroy structure */
328     free( p_sys );
329 }
330
331 static void Run( intf_thread_t *p_intf )
332 {
333     for( ;; )
334     {
335         int canc = vlc_savecancel();
336         FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
337                     p_intf->p_sys->i_mode,
338                     var_CreateGetInteger( p_intf, "verbose" ) );
339
340         vlc_restorecancel( canc );
341         /* FIXME: this is WRONG. */
342         msleep( INTF_IDLE_SLEEP );
343     }
344 }
345
346 /*****************************************************************************
347  * FlushQueue: flush the message queue into the log
348  *****************************************************************************/
349 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode,
350                         int i_verbose )
351 {
352     int i_start, i_stop;
353
354     vlc_mutex_lock( p_sub->p_lock );
355     i_stop = *p_sub->pi_stop;
356     vlc_mutex_unlock( p_sub->p_lock );
357
358     if( p_sub->i_start != i_stop )
359     {
360         /* Append all messages to log file */
361         for( i_start = p_sub->i_start;
362              i_start != i_stop;
363              i_start = (i_start+1) % VLC_MSG_QSIZE )
364         {
365             switch( p_sub->p_msg[i_start].i_type )
366             {
367             case VLC_MSG_ERR:
368                 if( i_verbose < 0 ) continue;
369                 break;
370             case VLC_MSG_INFO:
371                 if( i_verbose < 0 ) continue;
372                 break;
373             case VLC_MSG_WARN:
374                 if( i_verbose < 1 ) continue;
375                 break;
376             case VLC_MSG_DBG:
377                 if( i_verbose < 2 ) continue;
378                 break;
379             }
380
381             switch( i_mode )
382             {
383             case MODE_HTML:
384                 HtmlPrint( &p_sub->p_msg[i_start], p_file );
385                 break;
386 #ifdef HAVE_SYSLOG_H
387             case MODE_SYSLOG:
388                 SyslogPrint( &p_sub->p_msg[i_start] );
389                 break;
390 #endif
391             case MODE_TEXT:
392             default:
393                 TextPrint( &p_sub->p_msg[i_start], p_file );
394                 break;
395             }
396         }
397
398         vlc_mutex_lock( p_sub->p_lock );
399         p_sub->i_start = i_start;
400         vlc_mutex_unlock( p_sub->p_lock );
401     }
402 }
403
404 static const char *ppsz_type[4] = { ": ", " error: ",
405                                     " warning: ", " debug: " };
406
407 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
408 {
409     LOG_STRING( p_msg->psz_module, p_file );
410     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
411     LOG_STRING( p_msg->psz_msg, p_file );
412     LOG_STRING( "\n", p_file );
413 }
414
415 #ifdef HAVE_SYSLOG_H
416 static void SyslogPrint( const msg_item_t *p_msg )
417 {
418     static const int i_prio[4] = { LOG_INFO, LOG_ERR, LOG_WARNING, LOG_DEBUG };
419     int i_priority = i_prio[p_msg->i_type];
420
421     if( p_msg->psz_header )
422         syslog( i_priority, "%s%s %s: %s", p_msg->psz_header,
423                 ppsz_type[p_msg->i_type],
424                 p_msg->psz_module, p_msg->psz_msg );
425     else
426         syslog( i_priority, "%s%s: %s", p_msg->psz_module, 
427                 ppsz_type[p_msg->i_type], p_msg->psz_msg );
428  
429 }
430 #endif
431
432 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
433 {
434     static const char *ppsz_color[4] = { "<span style=\"color: #ffffff\">",
435                                          "<span style=\"color: #ff6666\">",
436                                          "<span style=\"color: #ffff66\">",
437                                          "<span style=\"color: #aaaaaa\">" };
438
439     LOG_STRING( p_msg->psz_module, p_file );
440     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
441     LOG_STRING( ppsz_color[p_msg->i_type], p_file );
442     LOG_STRING( p_msg->psz_msg, p_file );
443     LOG_STRING( "</span>\n", p_file );
444 }
445
446 static void *DoRRD (void *data)
447 {
448     intf_thread_t *p_intf = data;
449     FILE *file = p_intf->p_sys->rrd.stream;
450
451     for (;;)
452     {
453         /* FIXME: I wonder how memory synchronization occurs here...
454          * -- Courmisch */
455         if( p_intf->p_libvlc->p_stats )
456         {
457             lldiv_t in = lldiv( p_intf->p_libvlc->p_stats->f_input_bitrate * 1000000,
458                                 1000 );
459             lldiv_t dm = lldiv( p_intf->p_libvlc->p_stats->f_demux_bitrate * 1000000,
460                                 1000 );
461             lldiv_t out = lldiv( p_intf->p_libvlc->p_stats->f_output_bitrate * 1000000,
462                                 1000 );
463             fprintf( file,
464                     "%"PRIi64":%lld.%03llu:%lld.%03llu:%lld.%03llu\n",
465                     (int64_t)time(NULL), in.quot, in.rem, dm.quot, dm.rem, out.quot, out.rem );
466         }
467 #undef msleep /* yeah, we really want to wake up every second here */
468         msleep (CLOCK_FREQ);
469     }
470     assert (0);
471 }