1 /*****************************************************************************
2 * logger.c : file logging plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <errno.h> /* ENOMEM */
34 # define _IONBF 0x0004
45 #define LOG_DIR "Library/Logs/"
48 #define LOG_FILE_TEXT "vlc-log.txt"
49 #define LOG_FILE_HTML "vlc-log.html"
51 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
53 #define TEXT_HEADER "-- logger module started --\n"
54 #define TEXT_FOOTER "-- logger module stopped --\n"
59 " <title>vlc log</title>\n" \
61 " <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
63 " <b>-- logger module started --</b>\n"
65 " <b>-- logger module stopped --</b>\n" \
74 /*****************************************************************************
75 * intf_sys_t: description and status of log interface
76 *****************************************************************************/
83 FILE * p_file; /* The log file */
84 msg_subscription_t *p_sub;
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open ( vlc_object_t * );
91 static void Close ( vlc_object_t * );
92 static void Run ( intf_thread_t * );
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 * );
98 static void SyslogPrint ( const msg_item_t *);
101 static void DoRRD( intf_thread_t *p_intf );
103 /*****************************************************************************
105 *****************************************************************************/
106 static char *mode_list[] = { "text", "html"
111 static char *mode_list_text[] = { N_("Text"), "HTML"
117 #define LOGMODE_TEXT N_("Log format")
119 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default), \"html\", and \"syslog\".")
121 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default) and \"html\".")
125 set_shortname( N_( "Logging" ) );
126 set_description( _("File logging") );
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,
132 change_string_list( mode_list, mode_list_text, 0 );
134 add_string( "rrd-file", NULL, NULL, N_("RRD output file") ,
135 N_("Output data for RRDTool in this file" ), VLC_TRUE );
137 set_capability( "interface", 0 );
138 set_callbacks( Open, Close );
141 /*****************************************************************************
142 * Open: initialize and create stuff
143 *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
146 intf_thread_t *p_intf = (intf_thread_t *)p_this;
147 char *psz_mode, *psz_file, *psz_rrd_file;
150 msg_Info( p_intf, "Using logger..." );
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 )
156 msg_Err( p_intf, "out of memory" );
160 psz_mode = var_CreateGetString( p_intf, "logmode" );
163 if( !strcmp( psz_mode, "text" ) )
165 p_intf->p_sys->i_mode = MODE_TEXT;
167 else if( !strcmp( psz_mode, "html" ) )
169 p_intf->p_sys->i_mode = MODE_HTML;
172 else if( !strcmp( psz_mode, "syslog" ) )
174 p_intf->p_sys->i_mode = MODE_SYSLOG;
179 msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
180 p_intf->p_sys->i_mode = MODE_TEXT;
187 msg_Warn( p_intf, "no log mode specified, using `text'" );
188 p_intf->p_sys->i_mode = MODE_TEXT;
191 if( p_intf->p_sys->i_mode != MODE_SYSLOG )
193 psz_file = config_GetPsz( p_intf, "logfile" );
197 char *psz_homedir = p_this->p_vlc->psz_homedir;
201 msg_Err( p_this, "psz_homedir is null" );
204 psz_file = (char *)malloc( sizeof("/" LOG_DIR "/" LOG_FILE_HTML) +
205 strlen(psz_homedir) );
208 switch( p_intf->p_sys->i_mode )
211 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_HTML,
216 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_TEXT,
222 switch( p_intf->p_sys->i_mode )
225 psz_file = strdup( LOG_FILE_HTML );
229 psz_file = strdup( LOG_FILE_TEXT );
233 msg_Warn( p_intf, "no log filename provided, using `%s'",
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 = utf8_fopen( psz_file, "wt" );
240 if( p_intf->p_sys->p_file == NULL )
242 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
243 free( p_intf->p_sys );
247 setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
251 switch( p_intf->p_sys->i_mode )
254 LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
258 LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
265 p_intf->p_sys->p_file = NULL;
267 openlog( "VLC", 0, LOG_DAEMON );
271 p_intf->p_sys->last_update = 0;
272 p_intf->p_sys->p_rrd = NULL;
274 psz_rrd_file = config_GetPsz( p_intf, "rrd-file" );
275 if( psz_rrd_file && *psz_rrd_file )
277 p_intf->p_sys->p_rrd = utf8_fopen( psz_rrd_file, "w" );
280 p_intf->p_sys->p_sub = msg_Subscribe( p_intf , MSG_QUEUE_NORMAL );
281 p_intf->pf_run = Run;
286 /*****************************************************************************
287 * Close: destroy interface stuff
288 *****************************************************************************/
289 static void Close( vlc_object_t *p_this )
291 intf_thread_t *p_intf = (intf_thread_t *)p_this;
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 );
298 switch( p_intf->p_sys->i_mode )
301 LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
310 LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
314 /* Close the log file */
315 if( p_intf->p_sys->i_mode != MODE_SYSLOG )
316 fclose( p_intf->p_sys->p_file );
318 /* Destroy structure */
319 free( p_intf->p_sys );
322 /*****************************************************************************
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 )
330 while( !p_intf->b_die )
332 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
333 p_intf->p_sys->i_mode );
335 if( p_intf->p_sys->p_rrd )
338 msleep( INTF_IDLE_SLEEP );
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 )
349 vlc_mutex_lock( p_sub->p_lock );
350 i_stop = *p_sub->pi_stop;
351 vlc_mutex_unlock( p_sub->p_lock );
353 if( p_sub->i_start != i_stop )
355 /* Append all messages to log file */
356 for( i_start = p_sub->i_start;
358 i_start = (i_start+1) % VLC_MSG_QSIZE )
363 HtmlPrint( &p_sub->p_msg[i_start], p_file );
367 SyslogPrint( &p_sub->p_msg[i_start] );
372 TextPrint( &p_sub->p_msg[i_start], p_file );
377 vlc_mutex_lock( p_sub->p_lock );
378 p_sub->i_start = i_start;
379 vlc_mutex_unlock( p_sub->p_lock );
383 static const char *ppsz_type[4] = { ": ", " error: ",
384 " warning: ", " debug: " };
386 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
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 );
395 static void SyslogPrint( const msg_item_t *p_msg )
397 int i_priority = LOG_INFO;
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;
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 );
408 syslog( i_priority, "%s: %s", p_msg->psz_module, p_msg->psz_msg );
413 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
415 static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
416 "<font color=\"#ff6666\">",
417 "<font color=\"#ffff66\">",
418 "<font color=\"#aaaaaa\">" };
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 );
427 static void DoRRD( intf_thread_t *p_intf )
429 playlist_t *p_playlist;
430 float f_input_bitrate;
431 if( mdate() - p_intf->p_sys->last_update < 1000000 )
433 p_intf->p_sys->last_update = mdate();
435 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
437 if( p_playlist && p_playlist->p_stats )
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 );