]> git.sesse.net Git - vlc/blob - modules/misc/logger.c
* Got rid of SYS_DARWIN everywhere.
[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 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdio.h>
32
33 #ifdef UNDER_CE
34 #   define _IONBF 0x0004
35 #endif
36
37 #include <vlc/vlc.h>
38 #include <vlc/intf.h>
39
40 #define MODE_TEXT 0
41 #define MODE_HTML 1
42 #define MODE_SYSLOG 2
43
44 #ifdef __APPLE__
45 #define LOG_DIR "Library/Logs/"
46 #endif
47
48 #define LOG_FILE_TEXT "vlc-log.txt"
49 #define LOG_FILE_HTML "vlc-log.html"
50
51 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
52
53 #define TEXT_HEADER "-- logger module started --\n"
54 #define TEXT_FOOTER "-- logger module stopped --\n"
55
56 #define HTML_HEADER \
57     "<html>\n" \
58     "  <head>\n" \
59     "    <title>vlc log</title>\n" \
60     "  </head>\n" \
61     "  <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
62     "    <pre>\n" \
63     "      <b>-- logger module started --</b>\n"
64 #define HTML_FOOTER \
65     "      <b>-- logger module stopped --</b>\n" \
66     "    </pre>\n" \
67     "  </body>\n" \
68     "</html>\n"
69
70 #if HAVE_SYSLOG_H
71 #include <syslog.h>
72 #endif
73
74 /*****************************************************************************
75  * intf_sys_t: description and status of log interface
76  *****************************************************************************/
77 struct intf_sys_t
78 {
79     int i_mode;
80     FILE *p_rrd;
81     mtime_t last_update;
82
83     FILE *    p_file; /* The log file */
84     msg_subscription_t *p_sub;
85 };
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90 static int  Open    ( vlc_object_t * );
91 static void Close   ( vlc_object_t * );
92 static void Run     ( intf_thread_t * );
93
94 static void FlushQueue        ( msg_subscription_t *, FILE *, int );
95 static void TextPrint         ( const msg_item_t *, FILE * );
96 static void HtmlPrint         ( const msg_item_t *, FILE * );
97 #ifdef HAVE_SYSLOG_H
98 static void SyslogPrint       ( const msg_item_t *);
99 #endif
100
101 static void DoRRD( intf_thread_t *p_intf );
102
103 /*****************************************************************************
104  * Module descriptor
105  *****************************************************************************/
106 static char *mode_list[] = { "text", "html"
107 #ifdef HAVE_SYSLOG_H
108 ,"syslog"
109 #endif
110 };
111 static char *mode_list_text[] = { N_("Text"), "HTML"
112 #ifdef HAVE_SYSLOG_H
113 , "syslog"
114 #endif
115 };
116
117 #define LOGMODE_TEXT N_("Log format")
118 #ifdef HAVE_SYSLOG_H
119 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default), \"html\", and \"syslog\".")
120 #else
121 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default) and \"html\".")
122 #endif
123
124 vlc_module_begin();
125     set_shortname( N_( "Logging" ) );
126     set_description( _("File logging") );
127
128     add_file( "logfile", NULL, NULL,
129              N_("Log filename"), N_("Specify the log filename."), VLC_FALSE );
130     add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
131                 VLC_FALSE );
132         change_string_list( mode_list, mode_list_text, 0 );
133
134     add_string( "rrd-file", NULL, NULL, N_("RRD output file") ,
135                     N_("Output data for RRDTool in this file" ), VLC_TRUE );
136
137     set_capability( "interface", 0 );
138     set_callbacks( Open, Close );
139 vlc_module_end();
140
141 /*****************************************************************************
142  * Open: initialize and create stuff
143  *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
145 {
146     intf_thread_t *p_intf = (intf_thread_t *)p_this;
147     char *psz_mode, *psz_file, *psz_rrd_file;
148
149     CONSOLE_INTRO_MSG;
150     msg_Info( p_intf, "Using logger..." );
151
152     /* Allocate instance and initialize some members */
153     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
154     if( p_intf->p_sys == NULL )
155     {
156         msg_Err( p_intf, "out of memory" );
157         return -1;
158     }
159
160     psz_mode = var_CreateGetString( p_intf, "logmode" );
161     if( psz_mode )
162     {
163         if( !strcmp( psz_mode, "text" ) )
164         {
165             p_intf->p_sys->i_mode = MODE_TEXT;
166         }
167         else if( !strcmp( psz_mode, "html" ) )
168         {
169             p_intf->p_sys->i_mode = MODE_HTML;
170         }
171 #ifdef HAVE_SYSLOG_H
172         else if( !strcmp( psz_mode, "syslog" ) )
173         {
174             p_intf->p_sys->i_mode = MODE_SYSLOG;
175         }
176 #endif
177         else
178         {
179             msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
180             p_intf->p_sys->i_mode = MODE_TEXT;
181         }
182
183         free( psz_mode );
184     }
185     else
186     {
187         msg_Warn( p_intf, "no log mode specified, using `text'" );
188         p_intf->p_sys->i_mode = MODE_TEXT;
189     }
190
191     if( p_intf->p_sys->i_mode != MODE_SYSLOG )
192     {
193         psz_file = config_GetPsz( p_intf, "logfile" );
194         if( !psz_file )
195         {
196 #ifdef __APPLE__
197             char *psz_homedir = p_this->p_vlc->psz_homedir;
198
199             if( !psz_homedir )
200             {
201                 msg_Err( p_this, "psz_homedir is null" );
202                 return -1;
203             }
204             psz_file = (char *)malloc( sizeof("/" LOG_DIR "/" LOG_FILE_HTML) +
205                                            strlen(psz_homedir) );
206             if( psz_file )
207             {
208                 switch( p_intf->p_sys->i_mode )
209                 {
210                 case MODE_HTML:
211                     sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_HTML,
212                          psz_homedir );
213                     break;
214                 case MODE_TEXT:
215                 default:
216                     sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_TEXT,
217                          psz_homedir );
218                     break;
219                 }
220             }
221 #else
222             switch( p_intf->p_sys->i_mode )
223             {
224             case MODE_HTML:
225                 psz_file = strdup( LOG_FILE_HTML );
226                 break;
227             case MODE_TEXT:
228             default:
229                 psz_file = strdup( LOG_FILE_TEXT );
230                 break;
231             }
232 #endif
233             msg_Warn( p_intf, "no log filename provided, using `%s'",
234                                psz_file );
235         }
236
237         /* Open the log file and remove any buffering for the stream */
238         msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
239         p_intf->p_sys->p_file = fopen( psz_file, "wt" );
240         if( p_intf->p_sys->p_file == NULL )
241         {
242             msg_Err( p_intf, "error opening logfile `%s'", psz_file );
243             free( p_intf->p_sys );
244             free( psz_file );
245             return -1;
246         }
247         setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
248
249         free( psz_file );
250
251         switch( p_intf->p_sys->i_mode )
252         {
253         case MODE_HTML:
254             LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
255             break;
256         case MODE_TEXT:
257         default:
258             LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
259             break;
260         }
261
262     }
263     else
264     {
265         p_intf->p_sys->p_file = NULL;
266 #ifdef HAVE_SYSLOG_H
267         openlog( "VLC", 0, LOG_DAEMON );
268 #endif
269     }
270
271     p_intf->p_sys->last_update = 0;
272     p_intf->p_sys->p_rrd = NULL;
273
274     psz_rrd_file = config_GetPsz( p_intf, "rrd-file" );
275     if( psz_rrd_file && *psz_rrd_file )
276     {
277         p_intf->p_sys->p_rrd = fopen( psz_rrd_file, "w" );
278     }
279
280     p_intf->p_sys->p_sub = msg_Subscribe( p_intf , MSG_QUEUE_NORMAL );
281     p_intf->pf_run = Run;
282
283     return 0;
284 }
285
286 /*****************************************************************************
287  * Close: destroy interface stuff
288  *****************************************************************************/
289 static void Close( vlc_object_t *p_this )
290 {
291     intf_thread_t *p_intf = (intf_thread_t *)p_this;
292
293     /* Flush the queue and unsubscribe from the message queue */
294     FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
295                 p_intf->p_sys->i_mode );
296     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
297
298     switch( p_intf->p_sys->i_mode )
299     {
300     case MODE_HTML:
301         LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
302         break;
303     case MODE_TEXT:
304 #ifdef HAVE_SYSLOG_H
305     case MODE_SYSLOG:
306         closelog();
307         break;
308 #endif
309     default:
310         LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
311         break;
312     }
313
314     /* Close the log file */
315     if( p_intf->p_sys->i_mode != MODE_SYSLOG )
316         fclose( p_intf->p_sys->p_file );
317
318     /* Destroy structure */
319     free( p_intf->p_sys );
320 }
321
322 /*****************************************************************************
323  * Run: rc thread
324  *****************************************************************************
325  * This part of the interface is in a separate thread so that we can call
326  * exec() from within it without annoying the rest of the program.
327  *****************************************************************************/
328 static void Run( intf_thread_t *p_intf )
329 {
330     while( !p_intf->b_die )
331     {
332         FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
333                     p_intf->p_sys->i_mode );
334
335         if( p_intf->p_sys->p_rrd )
336             DoRRD( p_intf );
337
338         msleep( INTF_IDLE_SLEEP );
339     }
340 }
341
342 /*****************************************************************************
343  * FlushQueue: flush the message queue into the log
344  *****************************************************************************/
345 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
346 {
347     int i_start, i_stop;
348
349     vlc_mutex_lock( p_sub->p_lock );
350     i_stop = *p_sub->pi_stop;
351     vlc_mutex_unlock( p_sub->p_lock );
352
353     if( p_sub->i_start != i_stop )
354     {
355         /* Append all messages to log file */
356         for( i_start = p_sub->i_start;
357              i_start != i_stop;
358              i_start = (i_start+1) % VLC_MSG_QSIZE )
359         {
360             switch( i_mode )
361             {
362             case MODE_HTML:
363                 HtmlPrint( &p_sub->p_msg[i_start], p_file );
364                 break;
365 #ifdef HAVE_SYSLOG_H
366             case MODE_SYSLOG:
367                 SyslogPrint( &p_sub->p_msg[i_start] );
368                 break;
369 #endif
370             case MODE_TEXT:
371             default:
372                 TextPrint( &p_sub->p_msg[i_start], p_file );
373                 break;
374             }
375         }
376
377         vlc_mutex_lock( p_sub->p_lock );
378         p_sub->i_start = i_start;
379         vlc_mutex_unlock( p_sub->p_lock );
380     }
381 }
382
383 static const char *ppsz_type[4] = { ": ", " error: ",
384                                     " warning: ", " debug: " };
385
386 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
387 {
388     LOG_STRING( p_msg->psz_module, p_file );
389     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
390     LOG_STRING( p_msg->psz_msg, p_file );
391     LOG_STRING( "\n", p_file );
392 }
393
394 #ifdef HAVE_SYSLOG_H
395 static void SyslogPrint( const msg_item_t *p_msg )
396 {
397     int i_priority = LOG_INFO;
398
399     if( p_msg->i_type  == 0 ) i_priority = LOG_INFO;
400     if( p_msg->i_type  == 1 ) i_priority = LOG_ERR;
401     if( p_msg->i_type  == 2 ) i_priority = LOG_WARNING;
402     if( p_msg->i_type  == 3 ) i_priority = LOG_DEBUG;
403
404     if( p_msg->psz_header )
405         syslog( i_priority, "%s %s: %s", p_msg->psz_header,
406                 p_msg->psz_module, p_msg->psz_msg );
407     else
408         syslog( i_priority, "%s: %s", p_msg->psz_module, p_msg->psz_msg );
409         
410 }
411 #endif
412
413 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
414 {
415     static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
416                                          "<font color=\"#ff6666\">",
417                                          "<font color=\"#ffff66\">",
418                                          "<font color=\"#aaaaaa\">" };
419
420     LOG_STRING( p_msg->psz_module, p_file );
421     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
422     LOG_STRING( ppsz_color[p_msg->i_type], p_file );
423     LOG_STRING( p_msg->psz_msg, p_file );
424     LOG_STRING( "</font>\n", p_file );
425 }
426
427 static void DoRRD( intf_thread_t *p_intf )
428 {
429     playlist_t *p_playlist;
430     float f_input_bitrate;
431     if( mdate() - p_intf->p_sys->last_update < 1000000 )
432         return;
433     p_intf->p_sys->last_update = mdate();
434
435     p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
436                                                 FIND_ANYWHERE );
437     if( p_playlist && p_playlist->p_stats )
438     {
439         fprintf( p_intf->p_sys->p_rrd, I64Fi":%f:%f:%f\n",
440                    p_intf->p_sys->last_update/1000000,
441                    (float)(p_playlist->p_stats->f_input_bitrate)*1000,
442                    (float)(p_playlist->p_stats->f_demux_bitrate)*1000,
443                    (float)(p_playlist->p_stats->f_output_bitrate)*1000 );
444         fflush( p_intf->p_sys->p_rrd );
445         vlc_object_release( p_playlist );
446     }
447 }