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
46 #define LOG_DIR "Library/Logs/"
49 #define LOG_FILE_TEXT "vlc-log.txt"
50 #define LOG_FILE_HTML "vlc-log.html"
52 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
54 #define TEXT_HEADER "-- logger module started --\n"
55 #define TEXT_FOOTER "-- logger module stopped --\n"
60 " <title>vlc log</title>\n" \
62 " <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
64 " <b>-- logger module started --</b>\n"
66 " <b>-- logger module stopped --</b>\n" \
75 /*****************************************************************************
76 * intf_sys_t: description and status of log interface
77 *****************************************************************************/
84 FILE * p_file; /* The log file */
85 msg_subscription_t *p_sub;
88 /*****************************************************************************
90 *****************************************************************************/
91 static int Open ( vlc_object_t * );
92 static void Close ( vlc_object_t * );
93 static void Run ( intf_thread_t * );
95 static void FlushQueue ( msg_subscription_t *, FILE *, int );
96 static void TextPrint ( const msg_item_t *, FILE * );
97 static void HtmlPrint ( const msg_item_t *, FILE * );
99 static void SyslogPrint ( const msg_item_t *);
102 static void DoRRD( intf_thread_t *p_intf );
104 /*****************************************************************************
106 *****************************************************************************/
107 static char *mode_list[] = { "text", "html"
112 static char *mode_list_text[] = { N_("Text"), "HTML"
118 #define LOGMODE_TEXT N_("Log format")
120 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default), \"html\", and \"syslog\".")
122 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default) and \"html\".")
126 set_shortname( N_( "Logging" ) );
127 set_description( _("File logging") );
129 add_file( "logfile", NULL, NULL,
130 N_("Log filename"), N_("Specify the log filename."), VLC_FALSE );
131 add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
133 change_string_list( mode_list, mode_list_text, 0 );
135 add_string( "rrd-file", NULL, NULL, N_("RRD output file") ,
136 N_("Output data for RRDTool in this file" ), VLC_TRUE );
138 set_capability( "interface", 0 );
139 set_callbacks( Open, Close );
142 /*****************************************************************************
143 * Open: initialize and create stuff
144 *****************************************************************************/
145 static int Open( vlc_object_t *p_this )
147 intf_thread_t *p_intf = (intf_thread_t *)p_this;
148 char *psz_mode, *psz_file, *psz_rrd_file;
151 msg_Info( p_intf, "Using logger..." );
153 /* Allocate instance and initialize some members */
154 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
155 if( p_intf->p_sys == NULL )
157 msg_Err( p_intf, "out of memory" );
161 psz_mode = var_CreateGetString( p_intf, "logmode" );
164 if( !strcmp( psz_mode, "text" ) )
166 p_intf->p_sys->i_mode = MODE_TEXT;
168 else if( !strcmp( psz_mode, "html" ) )
170 p_intf->p_sys->i_mode = MODE_HTML;
173 else if( !strcmp( psz_mode, "syslog" ) )
175 p_intf->p_sys->i_mode = MODE_SYSLOG;
180 msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
181 p_intf->p_sys->i_mode = MODE_TEXT;
188 msg_Warn( p_intf, "no log mode specified, using `text'" );
189 p_intf->p_sys->i_mode = MODE_TEXT;
192 if( p_intf->p_sys->i_mode != MODE_SYSLOG )
194 psz_file = config_GetPsz( p_intf, "logfile" );
198 char *psz_homedir = p_this->p_vlc->psz_homedir;
202 msg_Err( p_this, "psz_homedir is null" );
205 psz_file = (char *)malloc( sizeof("/" LOG_DIR "/" LOG_FILE_HTML) +
206 strlen(psz_homedir) );
209 switch( p_intf->p_sys->i_mode )
212 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_HTML,
217 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_TEXT,
223 switch( p_intf->p_sys->i_mode )
226 psz_file = strdup( LOG_FILE_HTML );
230 psz_file = strdup( LOG_FILE_TEXT );
234 msg_Warn( p_intf, "no log filename provided, using `%s'",
238 /* Open the log file and remove any buffering for the stream */
239 msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
240 p_intf->p_sys->p_file = utf8_fopen( psz_file, "wt" );
241 if( p_intf->p_sys->p_file == NULL )
243 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
244 free( p_intf->p_sys );
248 setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
252 switch( p_intf->p_sys->i_mode )
255 LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
259 LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
266 p_intf->p_sys->p_file = NULL;
268 openlog( "VLC", 0, LOG_DAEMON );
272 p_intf->p_sys->last_update = 0;
273 p_intf->p_sys->p_rrd = NULL;
275 psz_rrd_file = config_GetPsz( p_intf, "rrd-file" );
276 if( psz_rrd_file && *psz_rrd_file )
278 p_intf->p_sys->p_rrd = utf8_fopen( psz_rrd_file, "w" );
281 p_intf->p_sys->p_sub = msg_Subscribe( p_intf , MSG_QUEUE_NORMAL );
282 p_intf->pf_run = Run;
287 /*****************************************************************************
288 * Close: destroy interface stuff
289 *****************************************************************************/
290 static void Close( vlc_object_t *p_this )
292 intf_thread_t *p_intf = (intf_thread_t *)p_this;
294 /* Flush the queue and unsubscribe from the message queue */
295 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
296 p_intf->p_sys->i_mode );
297 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
299 switch( p_intf->p_sys->i_mode )
302 LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
311 LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
315 /* Close the log file */
316 if( p_intf->p_sys->i_mode != MODE_SYSLOG )
317 fclose( p_intf->p_sys->p_file );
319 /* Destroy structure */
320 free( p_intf->p_sys );
323 /*****************************************************************************
325 *****************************************************************************
326 * This part of the interface is in a separate thread so that we can call
327 * exec() from within it without annoying the rest of the program.
328 *****************************************************************************/
329 static void Run( intf_thread_t *p_intf )
331 while( !p_intf->b_die )
333 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
334 p_intf->p_sys->i_mode );
336 if( p_intf->p_sys->p_rrd )
339 msleep( INTF_IDLE_SLEEP );
343 /*****************************************************************************
344 * FlushQueue: flush the message queue into the log
345 *****************************************************************************/
346 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
350 vlc_mutex_lock( p_sub->p_lock );
351 i_stop = *p_sub->pi_stop;
352 vlc_mutex_unlock( p_sub->p_lock );
354 if( p_sub->i_start != i_stop )
356 /* Append all messages to log file */
357 for( i_start = p_sub->i_start;
359 i_start = (i_start+1) % VLC_MSG_QSIZE )
364 HtmlPrint( &p_sub->p_msg[i_start], p_file );
368 SyslogPrint( &p_sub->p_msg[i_start] );
373 TextPrint( &p_sub->p_msg[i_start], p_file );
378 vlc_mutex_lock( p_sub->p_lock );
379 p_sub->i_start = i_start;
380 vlc_mutex_unlock( p_sub->p_lock );
384 static const char *ppsz_type[4] = { ": ", " error: ",
385 " warning: ", " debug: " };
387 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
389 LOG_STRING( p_msg->psz_module, p_file );
390 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
391 LOG_STRING( p_msg->psz_msg, p_file );
392 LOG_STRING( "\n", p_file );
396 static void SyslogPrint( const msg_item_t *p_msg )
398 int i_priority = LOG_INFO;
400 if( p_msg->i_type == 0 ) i_priority = LOG_INFO;
401 if( p_msg->i_type == 1 ) i_priority = LOG_ERR;
402 if( p_msg->i_type == 2 ) i_priority = LOG_WARNING;
403 if( p_msg->i_type == 3 ) i_priority = LOG_DEBUG;
405 if( p_msg->psz_header )
406 syslog( i_priority, "%s %s: %s", p_msg->psz_header,
407 p_msg->psz_module, p_msg->psz_msg );
409 syslog( i_priority, "%s: %s", p_msg->psz_module, p_msg->psz_msg );
414 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
416 static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
417 "<font color=\"#ff6666\">",
418 "<font color=\"#ffff66\">",
419 "<font color=\"#aaaaaa\">" };
421 LOG_STRING( p_msg->psz_module, p_file );
422 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
423 LOG_STRING( ppsz_color[p_msg->i_type], p_file );
424 LOG_STRING( p_msg->psz_msg, p_file );
425 LOG_STRING( "</font>\n", p_file );
428 static void DoRRD( intf_thread_t *p_intf )
430 playlist_t *p_playlist;
431 float f_input_bitrate;
432 if( mdate() - p_intf->p_sys->last_update < 1000000 )
434 p_intf->p_sys->last_update = mdate();
436 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
438 if( p_playlist && p_playlist->p_stats )
440 fprintf( p_intf->p_sys->p_rrd, I64Fi":%f:%f:%f\n",
441 p_intf->p_sys->last_update/1000000,
442 (float)(p_playlist->p_stats->f_input_bitrate)*1000,
443 (float)(p_playlist->p_stats->f_demux_bitrate)*1000,
444 (float)(p_playlist->p_stats->f_output_bitrate)*1000 );
445 fflush( p_intf->p_sys->p_rrd );
446 vlc_object_release( p_playlist );