]> git.sesse.net Git - vlc/blob - modules/misc/logger.c
Remove message unused "queue" parameter
[vlc] / modules / misc / logger.c
1 /*****************************************************************************
2  * logger.c : file logging plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 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/vlc.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_charset.h>
37
38 #include <errno.h>                                                 /* ENOMEM */
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     FILE *p_rrd;
88     mtime_t last_update;
89
90     FILE *    p_file; /* The log file */
91     msg_subscription_t *p_sub;
92 };
93
94 /*****************************************************************************
95  * Local prototypes
96  *****************************************************************************/
97 static int  Open    ( vlc_object_t * );
98 static void Close   ( vlc_object_t * );
99 static void Run     ( intf_thread_t * );
100
101 static void FlushQueue        ( msg_subscription_t *, FILE *, int, int );
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 static void DoRRD( intf_thread_t *p_intf );
109
110 /*****************************************************************************
111  * Module descriptor
112  *****************************************************************************/
113 static const char *mode_list[] = { "text", "html"
114 #ifdef HAVE_SYSLOG_H
115 ,"syslog"
116 #endif
117 };
118 static const char *mode_list_text[] = { N_("Text"), "HTML"
119 #ifdef HAVE_SYSLOG_H
120 , "syslog"
121 #endif
122 };
123
124 #define LOGMODE_TEXT N_("Log format")
125 #ifdef HAVE_SYSLOG_H
126 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
127   "\"text\" (default), \"html\", and \"syslog\" (special mode to send to " \
128   "syslog instead of file.")
129 #else
130 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
131   "\"text\" (default) and \"html\".")
132 #endif
133
134 vlc_module_begin();
135     set_shortname( _( "Logging" ) );
136     set_description( _("File logging") );
137
138     set_category( CAT_ADVANCED );
139     set_subcategory( SUBCAT_ADVANCED_MISC );
140
141     add_file( "logfile", NULL, NULL,
142              N_("Log filename"), N_("Specify the log filename."), false );
143         change_unsafe();
144     add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
145                 false );
146         change_string_list( mode_list, mode_list_text, 0 );
147
148     add_file( "rrd-file", NULL, NULL, N_("RRD output file") ,
149                     N_("Output data for RRDTool in this file." ), true );
150
151     set_capability( "interface", 0 );
152     set_callbacks( Open, Close );
153 vlc_module_end();
154
155 /*****************************************************************************
156  * Open: initialize and create stuff
157  *****************************************************************************/
158 static int Open( vlc_object_t *p_this )
159 {
160     intf_thread_t *p_intf = (intf_thread_t *)p_this;
161     char *psz_mode, *psz_file, *psz_rrd_file;
162
163     CONSOLE_INTRO_MSG;
164     msg_Info( p_intf, "using logger..." );
165
166     /* Allocate instance and initialize some members */
167     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
168     if( p_intf->p_sys == NULL )
169     {
170         msg_Err( p_intf, "out of memory" );
171         return -1;
172     }
173
174     psz_mode = var_CreateGetString( p_intf, "logmode" );
175     if( psz_mode )
176     {
177         if( !strcmp( psz_mode, "text" ) )
178         {
179             p_intf->p_sys->i_mode = MODE_TEXT;
180         }
181         else if( !strcmp( psz_mode, "html" ) )
182         {
183             p_intf->p_sys->i_mode = MODE_HTML;
184         }
185 #ifdef HAVE_SYSLOG_H
186         else if( !strcmp( psz_mode, "syslog" ) )
187         {
188             p_intf->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_intf->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_intf->p_sys->i_mode != MODE_SYSLOG )
206     {
207         psz_file = config_GetPsz( p_intf, "logfile" );
208         if( !psz_file )
209         {
210 #ifdef __APPLE__
211             char *psz_homedir = p_this->p_libvlc->psz_homedir;
212
213             if( !psz_homedir ) /* XXX: This should never happen */
214             {
215                 msg_Err( p_this, "unable to find home directory" );
216                 return -1;
217             }
218             psz_file = (char *)malloc( sizeof("/" LOG_DIR "/" LOG_FILE_HTML) +
219                                            strlen(psz_homedir) );
220             if( psz_file )
221             {
222                 switch( p_intf->p_sys->i_mode )
223                 {
224                 case MODE_HTML:
225                     sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_HTML,
226                          psz_homedir );
227                     break;
228                 case MODE_TEXT:
229                 default:
230                     sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_TEXT,
231                          psz_homedir );
232                     break;
233                 }
234             }
235 #else
236             switch( p_intf->p_sys->i_mode )
237             {
238             case MODE_HTML:
239                 psz_file = strdup( LOG_FILE_HTML );
240                 break;
241             case MODE_TEXT:
242             default:
243                 psz_file = strdup( LOG_FILE_TEXT );
244                 break;
245             }
246 #endif
247             msg_Warn( p_intf, "no log filename provided, using `%s'",
248                                psz_file );
249         }
250
251         /* Open the log file and remove any buffering for the stream */
252         msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
253         p_intf->p_sys->p_file = utf8_fopen( psz_file, "at" );
254         if( p_intf->p_sys->p_file == NULL )
255         {
256             msg_Err( p_intf, "error opening logfile `%s'", psz_file );
257             free( p_intf->p_sys );
258             free( psz_file );
259             return -1;
260         }
261         setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
262
263         free( psz_file );
264
265         switch( p_intf->p_sys->i_mode )
266         {
267         case MODE_HTML:
268             LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
269             break;
270         case MODE_TEXT:
271         default:
272             LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
273             break;
274         }
275
276     }
277     else
278     {
279         p_intf->p_sys->p_file = NULL;
280 #ifdef HAVE_SYSLOG_H
281         openlog( "vlc", LOG_PID|LOG_NDELAY, LOG_DAEMON );
282 #endif
283     }
284
285     p_intf->p_sys->last_update = 0;
286     p_intf->p_sys->p_rrd = NULL;
287
288     psz_rrd_file = config_GetPsz( p_intf, "rrd-file" );
289     if( psz_rrd_file && *psz_rrd_file )
290     {
291         p_intf->p_sys->p_rrd = utf8_fopen( psz_rrd_file, "w" );
292     }
293
294     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
295     p_intf->pf_run = Run;
296
297     return 0;
298 }
299
300 /*****************************************************************************
301  * Close: destroy interface stuff
302  *****************************************************************************/
303 static void Close( vlc_object_t *p_this )
304 {
305     intf_thread_t *p_intf = (intf_thread_t *)p_this;
306
307     /* Flush the queue and unsubscribe from the message queue */
308     FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
309                 p_intf->p_sys->i_mode,
310                 var_CreateGetInteger( p_intf, "verbose" ) );
311     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
312
313     switch( p_intf->p_sys->i_mode )
314     {
315     case MODE_HTML:
316         LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
317         break;
318     case MODE_TEXT:
319 #ifdef HAVE_SYSLOG_H
320     case MODE_SYSLOG:
321         closelog();
322         break;
323 #endif
324     default:
325         LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
326         break;
327     }
328
329     /* Close the log file */
330     if( p_intf->p_sys->i_mode != MODE_SYSLOG )
331         fclose( p_intf->p_sys->p_file );
332
333     /* Destroy structure */
334     free( p_intf->p_sys );
335 }
336
337 /*****************************************************************************
338  * Run: rc thread
339  *****************************************************************************
340  * This part of the interface is in a separate thread so that we can call
341  * exec() from within it without annoying the rest of the program.
342  *****************************************************************************/
343 static void Run( intf_thread_t *p_intf )
344 {
345     while( !p_intf->b_die )
346     {
347         FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
348                     p_intf->p_sys->i_mode,
349                     var_CreateGetInteger( p_intf, "verbose" ) );
350         if( p_intf->p_sys->p_rrd )
351             DoRRD( p_intf );
352
353         msleep( INTF_IDLE_SLEEP );
354     }
355 }
356
357 /*****************************************************************************
358  * FlushQueue: flush the message queue into the log
359  *****************************************************************************/
360 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode,
361                         int i_verbose )
362 {
363     int i_start, i_stop;
364
365     vlc_mutex_lock( p_sub->p_lock );
366     i_stop = *p_sub->pi_stop;
367     vlc_mutex_unlock( p_sub->p_lock );
368
369     if( p_sub->i_start != i_stop )
370     {
371         /* Append all messages to log file */
372         for( i_start = p_sub->i_start;
373              i_start != i_stop;
374              i_start = (i_start+1) % VLC_MSG_QSIZE )
375         {
376             switch( p_sub->p_msg[i_start].i_type )
377             {
378             case VLC_MSG_ERR:
379                 if( i_verbose < 0 ) continue;
380                 break;
381             case VLC_MSG_INFO:
382                 if( i_verbose < 0 ) continue;
383                 break;
384             case VLC_MSG_WARN:
385                 if( i_verbose < 1 ) continue;
386                 break;
387             case VLC_MSG_DBG:
388                 if( i_verbose < 2 ) continue;
389                 break;
390             }
391
392             switch( i_mode )
393             {
394             case MODE_HTML:
395                 HtmlPrint( &p_sub->p_msg[i_start], p_file );
396                 break;
397 #ifdef HAVE_SYSLOG_H
398             case MODE_SYSLOG:
399                 SyslogPrint( &p_sub->p_msg[i_start] );
400                 break;
401 #endif
402             case MODE_TEXT:
403             default:
404                 TextPrint( &p_sub->p_msg[i_start], p_file );
405                 break;
406             }
407         }
408
409         vlc_mutex_lock( p_sub->p_lock );
410         p_sub->i_start = i_start;
411         vlc_mutex_unlock( p_sub->p_lock );
412     }
413 }
414
415 static const char *ppsz_type[4] = { ": ", " error: ",
416                                     " warning: ", " debug: " };
417
418 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
419 {
420     LOG_STRING( p_msg->psz_module, p_file );
421     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
422     LOG_STRING( p_msg->psz_msg, p_file );
423     LOG_STRING( "\n", p_file );
424 }
425
426 #ifdef HAVE_SYSLOG_H
427 static void SyslogPrint( const msg_item_t *p_msg )
428 {
429     static const int i_prio[4] = { LOG_INFO, LOG_ERR, LOG_WARNING, LOG_DEBUG };
430     int i_priority = i_prio[p_msg->i_type];
431
432     if( p_msg->psz_header )
433         syslog( i_priority, "%s%s %s: %s", p_msg->psz_header,
434                 ppsz_type[p_msg->i_type],
435                 p_msg->psz_module, p_msg->psz_msg );
436     else
437         syslog( i_priority, "%s%s: %s", p_msg->psz_module, 
438                 ppsz_type[p_msg->i_type], p_msg->psz_msg );
439  
440 }
441 #endif
442
443 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
444 {
445     static const char *ppsz_color[4] = { "<span style=\"color: #ffffff\">",
446                                          "<span style=\"color: #ff6666\">",
447                                          "<span style=\"color: #ffff66\">",
448                                          "<span style=\"color: #aaaaaa\">" };
449
450     LOG_STRING( p_msg->psz_module, p_file );
451     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
452     LOG_STRING( ppsz_color[p_msg->i_type], p_file );
453     LOG_STRING( p_msg->psz_msg, p_file );
454     LOG_STRING( "</span>\n", p_file );
455 }
456
457 static void DoRRD( intf_thread_t *p_intf )
458 {
459     if( mdate() - p_intf->p_sys->last_update < 1000000 )
460         return;
461     p_intf->p_sys->last_update = mdate();
462
463     if( p_intf->p_libvlc->p_stats )
464     {
465         lldiv_t din = lldiv( p_intf->p_libvlc->p_stats->f_input_bitrate * 1000000,
466                              1000 );
467         lldiv_t ddm = lldiv( p_intf->p_libvlc->p_stats->f_demux_bitrate * 1000000,
468                              1000 );
469         lldiv_t dout = lldiv( p_intf->p_libvlc->p_stats->f_output_bitrate * 1000000,
470                              1000 );
471         fprintf( p_intf->p_sys->p_rrd,
472                    "%"PRIi64":%lld.%03u:%lld.%03u:%lld.%03u\n",
473                    p_intf->p_sys->last_update/1000000,
474                    din.quot, (unsigned int)din.rem,
475                    ddm.quot, (unsigned int)ddm.rem,
476                    dout.quot, (unsigned int)dout.rem );
477         fflush( p_intf->p_sys->p_rrd );
478     }
479 }