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