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 *****************************************************************************/
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_charset.h>
38 #include <errno.h> /* ENOMEM */
41 # define _IONBF 0x0004
49 #define LOG_DIR "Library/Logs/"
52 #define LOG_FILE_TEXT "vlc-log.txt"
53 #define LOG_FILE_HTML "vlc-log.html"
55 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
57 #define TEXT_HEADER "-- logger module started --\n"
58 #define TEXT_FOOTER "-- logger module stopped --\n"
61 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
62 " \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
65 " <title>vlc log</title>\n" \
66 " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" \
68 " <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
70 " <b>-- logger module started --</b>\n"
72 " <b>-- logger module stopped --</b>\n" \
81 /*****************************************************************************
82 * intf_sys_t: description and status of log interface
83 *****************************************************************************/
89 time_t now; /* timestamp for rrd-log */
91 FILE * p_file; /* The log file */
92 msg_subscription_t *p_sub;
95 /*****************************************************************************
97 *****************************************************************************/
98 static int Open ( vlc_object_t * );
99 static void Close ( vlc_object_t * );
100 static void Run ( intf_thread_t * );
102 static void FlushQueue ( msg_subscription_t *, FILE *, int, int );
103 static void TextPrint ( const msg_item_t *, FILE * );
104 static void HtmlPrint ( const msg_item_t *, FILE * );
106 static void SyslogPrint ( const msg_item_t *);
109 static void DoRRD( intf_thread_t *p_intf );
111 /*****************************************************************************
113 *****************************************************************************/
114 static const char *const mode_list[] = { "text", "html"
119 static const char *const mode_list_text[] = { N_("Text"), "HTML"
125 #define LOGMODE_TEXT N_("Log format")
127 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
128 "\"text\" (default), \"html\", and \"syslog\" (special mode to send to " \
129 "syslog instead of file.")
131 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
132 "\"text\" (default) and \"html\".")
136 set_shortname( N_( "Logging" ) );
137 set_description( N_("File logging") );
139 set_category( CAT_ADVANCED );
140 set_subcategory( SUBCAT_ADVANCED_MISC );
142 add_file( "logfile", NULL, NULL,
143 N_("Log filename"), N_("Specify the log filename."), false );
145 add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
147 change_string_list( mode_list, mode_list_text, 0 );
149 add_file( "rrd-file", NULL, NULL, N_("RRD output file") ,
150 N_("Output data for RRDTool in this file." ), true );
152 set_capability( "interface", 0 );
153 set_callbacks( Open, Close );
156 /*****************************************************************************
157 * Open: initialize and create stuff
158 *****************************************************************************/
159 static int Open( vlc_object_t *p_this )
161 intf_thread_t *p_intf = (intf_thread_t *)p_this;
162 char *psz_mode, *psz_file, *psz_rrd_file;
165 msg_Info( p_intf, "using logger..." );
167 /* Allocate instance and initialize some members */
168 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
169 if( p_intf->p_sys == NULL )
171 msg_Err( p_intf, "out of memory" );
175 psz_mode = var_CreateGetString( p_intf, "logmode" );
178 if( !strcmp( psz_mode, "text" ) )
180 p_intf->p_sys->i_mode = MODE_TEXT;
182 else if( !strcmp( psz_mode, "html" ) )
184 p_intf->p_sys->i_mode = MODE_HTML;
187 else if( !strcmp( psz_mode, "syslog" ) )
189 p_intf->p_sys->i_mode = MODE_SYSLOG;
194 msg_Warn( p_intf, "invalid log mode `%s', using `text'", psz_mode );
195 p_intf->p_sys->i_mode = MODE_TEXT;
202 msg_Warn( p_intf, "no log mode specified, using `text'" );
203 p_intf->p_sys->i_mode = MODE_TEXT;
206 if( p_intf->p_sys->i_mode != MODE_SYSLOG )
208 psz_file = config_GetPsz( p_intf, "logfile" );
212 if( asprintf( &psz_file, "%s/"LOG_DIR"/%s", config_GetHomeDir(),
213 (p_intf->p_sys->i_mode == MODE_HTML) ? LOG_FILE_HTML
214 : LOG_FILE_TEXT ) == -1 )
217 switch( p_intf->p_sys->i_mode )
220 psz_file = strdup( LOG_FILE_HTML );
224 psz_file = strdup( LOG_FILE_TEXT );
228 msg_Warn( p_intf, "no log filename provided, using `%s'",
232 /* Open the log file and remove any buffering for the stream */
233 msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
234 p_intf->p_sys->p_file = utf8_fopen( psz_file, "at" );
235 if( p_intf->p_sys->p_file == NULL )
237 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
238 free( p_intf->p_sys );
242 setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
246 switch( p_intf->p_sys->i_mode )
249 LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
253 LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
260 p_intf->p_sys->p_file = NULL;
262 openlog( "vlc", LOG_PID|LOG_NDELAY, LOG_DAEMON );
266 p_intf->p_sys->last_update = 0;
267 p_intf->p_sys->p_rrd = NULL;
269 psz_rrd_file = config_GetPsz( p_intf, "rrd-file" );
270 if( psz_rrd_file && *psz_rrd_file )
272 p_intf->p_sys->p_rrd = utf8_fopen( psz_rrd_file, "w" );
275 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
276 p_intf->pf_run = Run;
281 /*****************************************************************************
282 * Close: destroy interface stuff
283 *****************************************************************************/
284 static void Close( vlc_object_t *p_this )
286 intf_thread_t *p_intf = (intf_thread_t *)p_this;
288 /* Flush the queue and unsubscribe from the message queue */
289 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
290 p_intf->p_sys->i_mode,
291 var_CreateGetInteger( p_intf, "verbose" ) );
292 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
294 switch( p_intf->p_sys->i_mode )
297 LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
306 LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
310 /* Close the log file */
311 if( p_intf->p_sys->i_mode != MODE_SYSLOG )
312 fclose( p_intf->p_sys->p_file );
314 /* Destroy structure */
315 free( p_intf->p_sys );
318 /*****************************************************************************
320 *****************************************************************************
321 * This part of the interface is in a separate thread so that we can call
322 * exec() from within it without annoying the rest of the program.
323 *****************************************************************************/
324 static void Run( intf_thread_t *p_intf )
326 while( !p_intf->b_die )
328 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
329 p_intf->p_sys->i_mode,
330 var_CreateGetInteger( p_intf, "verbose" ) );
331 if( p_intf->p_sys->p_rrd )
334 msleep( INTF_IDLE_SLEEP );
338 /*****************************************************************************
339 * FlushQueue: flush the message queue into the log
340 *****************************************************************************/
341 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode,
346 vlc_mutex_lock( p_sub->p_lock );
347 i_stop = *p_sub->pi_stop;
348 vlc_mutex_unlock( p_sub->p_lock );
350 if( p_sub->i_start != i_stop )
352 /* Append all messages to log file */
353 for( i_start = p_sub->i_start;
355 i_start = (i_start+1) % VLC_MSG_QSIZE )
357 switch( p_sub->p_msg[i_start].i_type )
360 if( i_verbose < 0 ) continue;
363 if( i_verbose < 0 ) continue;
366 if( i_verbose < 1 ) continue;
369 if( i_verbose < 2 ) continue;
376 HtmlPrint( &p_sub->p_msg[i_start], p_file );
380 SyslogPrint( &p_sub->p_msg[i_start] );
385 TextPrint( &p_sub->p_msg[i_start], p_file );
390 vlc_mutex_lock( p_sub->p_lock );
391 p_sub->i_start = i_start;
392 vlc_mutex_unlock( p_sub->p_lock );
396 static const char *ppsz_type[4] = { ": ", " error: ",
397 " warning: ", " debug: " };
399 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
401 LOG_STRING( p_msg->psz_module, p_file );
402 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
403 LOG_STRING( p_msg->psz_msg, p_file );
404 LOG_STRING( "\n", p_file );
408 static void SyslogPrint( const msg_item_t *p_msg )
410 static const int i_prio[4] = { LOG_INFO, LOG_ERR, LOG_WARNING, LOG_DEBUG };
411 int i_priority = i_prio[p_msg->i_type];
413 if( p_msg->psz_header )
414 syslog( i_priority, "%s%s %s: %s", p_msg->psz_header,
415 ppsz_type[p_msg->i_type],
416 p_msg->psz_module, p_msg->psz_msg );
418 syslog( i_priority, "%s%s: %s", p_msg->psz_module,
419 ppsz_type[p_msg->i_type], p_msg->psz_msg );
424 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
426 static const char *ppsz_color[4] = { "<span style=\"color: #ffffff\">",
427 "<span style=\"color: #ff6666\">",
428 "<span style=\"color: #ffff66\">",
429 "<span style=\"color: #aaaaaa\">" };
431 LOG_STRING( p_msg->psz_module, p_file );
432 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
433 LOG_STRING( ppsz_color[p_msg->i_type], p_file );
434 LOG_STRING( p_msg->psz_msg, p_file );
435 LOG_STRING( "</span>\n", p_file );
438 static void DoRRD( intf_thread_t *p_intf )
440 if( mdate() - p_intf->p_sys->last_update < 1000000 )
442 p_intf->p_sys->last_update = mdate();
444 if( p_intf->p_libvlc->p_stats )
446 time(&p_intf->p_sys->now);
447 lldiv_t din = lldiv( p_intf->p_libvlc->p_stats->f_input_bitrate * 1000000,
449 lldiv_t ddm = lldiv( p_intf->p_libvlc->p_stats->f_demux_bitrate * 1000000,
451 lldiv_t dout = lldiv( p_intf->p_libvlc->p_stats->f_output_bitrate * 1000000,
453 fprintf( p_intf->p_sys->p_rrd,
454 "%"PRIi64":%lld.%03u:%lld.%03u:%lld.%03u\n",
455 (uintmax_t)p_intf->p_sys->now,
456 din.quot, (unsigned int)din.rem,
457 ddm.quot, (unsigned int)ddm.rem,
458 dout.quot, (unsigned int)dout.rem );
459 fflush( p_intf->p_sys->p_rrd );