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