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 *****************************************************************************/
29 #include <vlc_interface.h>
30 #include <vlc_playlist.h>
31 #include <vlc_charset.h>
33 #include <errno.h> /* ENOMEM */
36 # define _IONBF 0x0004
44 #define LOG_DIR "Library/Logs/"
47 #define LOG_FILE_TEXT "vlc-log.txt"
48 #define LOG_FILE_HTML "vlc-log.html"
50 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
52 #define TEXT_HEADER "-- logger module started --\n"
53 #define TEXT_FOOTER "-- logger module stopped --\n"
58 " <title>vlc log</title>\n" \
60 " <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
62 " <b>-- logger module started --</b>\n"
64 " <b>-- logger module stopped --</b>\n" \
73 /*****************************************************************************
74 * intf_sys_t: description and status of log interface
75 *****************************************************************************/
82 FILE * p_file; /* The log file */
83 msg_subscription_t *p_sub;
86 /*****************************************************************************
88 *****************************************************************************/
89 static int Open ( vlc_object_t * );
90 static void Close ( vlc_object_t * );
91 static void Run ( intf_thread_t * );
93 static void FlushQueue ( msg_subscription_t *, FILE *, int );
94 static void TextPrint ( const msg_item_t *, FILE * );
95 static void HtmlPrint ( const msg_item_t *, FILE * );
97 static void SyslogPrint ( const msg_item_t *);
100 static void DoRRD( intf_thread_t *p_intf );
102 /*****************************************************************************
104 *****************************************************************************/
105 static const char *mode_list[] = { "text", "html"
110 static const char *mode_list_text[] = { N_("Text"), "HTML"
116 #define LOGMODE_TEXT N_("Log format")
118 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
119 "\"text\" (default), \"html\", and \"syslog\" (special mode to send to " \
120 "syslog instead of file.")
122 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
123 "\"text\" (default) and \"html\".")
127 set_shortname( _( "Logging" ) );
128 set_description( _("File logging") );
130 set_category( CAT_ADVANCED );
131 set_subcategory( SUBCAT_ADVANCED_MISC );
133 add_file( "logfile", NULL, NULL,
134 N_("Log filename"), N_("Specify the log filename."), VLC_FALSE );
135 add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
137 change_string_list( mode_list, mode_list_text, 0 );
139 add_file( "rrd-file", NULL, NULL, N_("RRD output file") ,
140 N_("Output data for RRDTool in this file." ), VLC_TRUE );
142 set_capability( "interface", 0 );
143 set_callbacks( Open, Close );
146 /*****************************************************************************
147 * Open: initialize and create stuff
148 *****************************************************************************/
149 static int Open( vlc_object_t *p_this )
151 intf_thread_t *p_intf = (intf_thread_t *)p_this;
152 char *psz_mode, *psz_file, *psz_rrd_file;
155 msg_Info( p_intf, "using logger..." );
157 /* Allocate instance and initialize some members */
158 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
159 if( p_intf->p_sys == NULL )
161 msg_Err( p_intf, "out of memory" );
165 psz_mode = var_CreateGetString( p_intf, "logmode" );
168 if( !strcmp( psz_mode, "text" ) )
170 p_intf->p_sys->i_mode = MODE_TEXT;
172 else if( !strcmp( psz_mode, "html" ) )
174 p_intf->p_sys->i_mode = MODE_HTML;
177 else if( !strcmp( psz_mode, "syslog" ) )
179 p_intf->p_sys->i_mode = MODE_SYSLOG;
184 msg_Warn( p_intf, "invalid log mode `%s', using `text'", psz_mode );
185 p_intf->p_sys->i_mode = MODE_TEXT;
192 msg_Warn( p_intf, "no log mode specified, using `text'" );
193 p_intf->p_sys->i_mode = MODE_TEXT;
196 if( p_intf->p_sys->i_mode != MODE_SYSLOG )
198 psz_file = config_GetPsz( p_intf, "logfile" );
202 char *psz_homedir = p_this->p_libvlc->psz_homedir;
204 if( !psz_homedir ) /* XXX: This should never happen */
206 msg_Err( p_this, "unable to find home directory" );
209 psz_file = (char *)malloc( sizeof("/" LOG_DIR "/" LOG_FILE_HTML) +
210 strlen(psz_homedir) );
213 switch( p_intf->p_sys->i_mode )
216 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_HTML,
221 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_TEXT,
227 switch( p_intf->p_sys->i_mode )
230 psz_file = strdup( LOG_FILE_HTML );
234 psz_file = strdup( LOG_FILE_TEXT );
238 msg_Warn( p_intf, "no log filename provided, using `%s'",
242 /* Open the log file and remove any buffering for the stream */
243 msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
244 p_intf->p_sys->p_file = utf8_fopen( psz_file, "at" );
245 if( p_intf->p_sys->p_file == NULL )
247 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
248 free( p_intf->p_sys );
252 setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
256 switch( p_intf->p_sys->i_mode )
259 LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
263 LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
270 p_intf->p_sys->p_file = NULL;
272 openlog( "VLC", 0, LOG_DAEMON );
276 p_intf->p_sys->last_update = 0;
277 p_intf->p_sys->p_rrd = NULL;
279 psz_rrd_file = config_GetPsz( p_intf, "rrd-file" );
280 if( psz_rrd_file && *psz_rrd_file )
282 p_intf->p_sys->p_rrd = utf8_fopen( psz_rrd_file, "w" );
285 p_intf->p_sys->p_sub = msg_Subscribe( p_intf , MSG_QUEUE_NORMAL );
286 p_intf->pf_run = Run;
291 /*****************************************************************************
292 * Close: destroy interface stuff
293 *****************************************************************************/
294 static void Close( vlc_object_t *p_this )
296 intf_thread_t *p_intf = (intf_thread_t *)p_this;
298 /* Flush the queue and unsubscribe from the message queue */
299 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
300 p_intf->p_sys->i_mode );
301 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
303 switch( p_intf->p_sys->i_mode )
306 LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
315 LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
319 /* Close the log file */
320 if( p_intf->p_sys->i_mode != MODE_SYSLOG )
321 fclose( p_intf->p_sys->p_file );
323 /* Destroy structure */
324 free( p_intf->p_sys );
327 /*****************************************************************************
329 *****************************************************************************
330 * This part of the interface is in a separate thread so that we can call
331 * exec() from within it without annoying the rest of the program.
332 *****************************************************************************/
333 static void Run( intf_thread_t *p_intf )
335 while( !p_intf->b_die )
337 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
338 p_intf->p_sys->i_mode );
340 if( p_intf->p_sys->p_rrd )
343 msleep( INTF_IDLE_SLEEP );
347 /*****************************************************************************
348 * FlushQueue: flush the message queue into the log
349 *****************************************************************************/
350 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
354 vlc_mutex_lock( p_sub->p_lock );
355 i_stop = *p_sub->pi_stop;
356 vlc_mutex_unlock( p_sub->p_lock );
358 if( p_sub->i_start != i_stop )
360 /* Append all messages to log file */
361 for( i_start = p_sub->i_start;
363 i_start = (i_start+1) % VLC_MSG_QSIZE )
368 HtmlPrint( &p_sub->p_msg[i_start], p_file );
372 SyslogPrint( &p_sub->p_msg[i_start] );
377 TextPrint( &p_sub->p_msg[i_start], p_file );
382 vlc_mutex_lock( p_sub->p_lock );
383 p_sub->i_start = i_start;
384 vlc_mutex_unlock( p_sub->p_lock );
388 static const char *ppsz_type[4] = { ": ", " error: ",
389 " warning: ", " debug: " };
391 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
393 LOG_STRING( p_msg->psz_module, p_file );
394 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
395 LOG_STRING( p_msg->psz_msg, p_file );
396 LOG_STRING( "\n", p_file );
400 static void SyslogPrint( const msg_item_t *p_msg )
402 int i_priority = LOG_INFO;
404 if( p_msg->i_type == 0 ) i_priority = LOG_INFO;
405 if( p_msg->i_type == 1 ) i_priority = LOG_ERR;
406 if( p_msg->i_type == 2 ) i_priority = LOG_WARNING;
407 if( p_msg->i_type == 3 ) i_priority = LOG_DEBUG;
409 if( p_msg->psz_header )
410 syslog( i_priority, "%s %s: %s", p_msg->psz_header,
411 p_msg->psz_module, p_msg->psz_msg );
413 syslog( i_priority, "%s: %s", p_msg->psz_module, p_msg->psz_msg );
418 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
420 static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
421 "<font color=\"#ff6666\">",
422 "<font color=\"#ffff66\">",
423 "<font color=\"#aaaaaa\">" };
425 LOG_STRING( p_msg->psz_module, p_file );
426 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
427 LOG_STRING( ppsz_color[p_msg->i_type], p_file );
428 LOG_STRING( p_msg->psz_msg, p_file );
429 LOG_STRING( "</font>\n", p_file );
432 static void DoRRD( intf_thread_t *p_intf )
434 playlist_t *p_playlist;
435 if( mdate() - p_intf->p_sys->last_update < 1000000 )
437 p_intf->p_sys->last_update = mdate();
439 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
441 if( p_playlist && p_playlist->p_stats )
443 lldiv_t din = lldiv( p_playlist->p_stats->f_input_bitrate * 1000000,
445 lldiv_t ddm = lldiv( p_playlist->p_stats->f_demux_bitrate * 1000000,
447 lldiv_t dout = lldiv( p_playlist->p_stats->f_output_bitrate * 1000000,
449 fprintf( p_intf->p_sys->p_rrd,
450 I64Fi":%lld.%03u:%lld.%03u:%lld.%03u\n",
451 p_intf->p_sys->last_update/1000000,
452 din.quot, (unsigned int)din.rem,
453 ddm.quot, (unsigned int)ddm.rem,
454 dout.quot, (unsigned int)dout.rem );
455 fflush( p_intf->p_sys->p_rrd );
456 vlc_object_release( p_playlist );