1 /*****************************************************************************
2 * logger.c : file logging plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2008 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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_charset.h>
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 *****************************************************************************/
93 FILE * p_file; /* The log file */
94 msg_subscription_t *p_sub;
97 /*****************************************************************************
99 *****************************************************************************/
100 static int Open ( vlc_object_t * );
101 static void Close ( vlc_object_t * );
102 static void Run ( intf_thread_t * );
104 static void FlushQueue ( msg_subscription_t *, FILE *, int, int );
105 static void TextPrint ( const msg_item_t *, FILE * );
106 static void HtmlPrint ( const msg_item_t *, FILE * );
108 static void SyslogPrint ( const msg_item_t *);
111 static void *DoRRD( void * );
113 /*****************************************************************************
115 *****************************************************************************/
116 static const char *const mode_list[] = { "text", "html"
121 static const char *const mode_list_text[] = { N_("Text"), "HTML"
127 #define LOGMODE_TEXT N_("Log format")
129 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
130 "\"text\" (default), \"html\", and \"syslog\" (special mode to send to " \
131 "syslog instead of file.")
133 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
134 "\"text\" (default) and \"html\".")
138 set_shortname( N_( "Logging" ) );
139 set_description( N_("File logging") );
141 set_category( CAT_ADVANCED );
142 set_subcategory( SUBCAT_ADVANCED_MISC );
144 add_file( "logfile", NULL, NULL,
145 N_("Log filename"), N_("Specify the log filename."), false );
146 add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
148 change_string_list( mode_list, mode_list_text, 0 );
150 add_file( "rrd-file", NULL, NULL, N_("RRD output file") ,
151 N_("Output data for RRDTool in this file." ), true );
153 set_capability( "interface", 0 );
154 set_callbacks( Open, Close );
157 /*****************************************************************************
158 * Open: initialize and create stuff
159 *****************************************************************************/
160 static int Open( vlc_object_t *p_this )
162 intf_thread_t *p_intf = (intf_thread_t *)p_this;
164 char *psz_mode, *psz_file, *psz_rrd_file;
167 msg_Info( p_intf, "using logger..." );
169 /* Allocate instance and initialize some members */
170 p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
174 psz_mode = var_CreateGetString( p_intf, "logmode" );
177 if( !strcmp( psz_mode, "text" ) )
179 p_sys->i_mode = MODE_TEXT;
181 else if( !strcmp( psz_mode, "html" ) )
183 p_sys->i_mode = MODE_HTML;
186 else if( !strcmp( psz_mode, "syslog" ) )
188 p_sys->i_mode = MODE_SYSLOG;
193 msg_Warn( p_intf, "invalid log mode `%s', using `text'", psz_mode );
194 p_sys->i_mode = MODE_TEXT;
201 msg_Warn( p_intf, "no log mode specified, using `text'" );
202 p_intf->p_sys->i_mode = MODE_TEXT;
205 if( p_sys->i_mode != MODE_SYSLOG )
207 psz_file = config_GetPsz( p_intf, "logfile" );
211 if( asprintf( &psz_file, "%s/"LOG_DIR"/%s", config_GetHomeDir(),
212 (p_sys->i_mode == MODE_HTML) ? LOG_FILE_HTML
213 : LOG_FILE_TEXT ) == -1 )
216 switch( p_sys->i_mode )
219 psz_file = strdup( LOG_FILE_HTML );
223 psz_file = strdup( LOG_FILE_TEXT );
227 msg_Warn( p_intf, "no log filename provided, using `%s'",
231 /* Open the log file and remove any buffering for the stream */
232 msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
233 p_sys->p_file = utf8_fopen( psz_file, "at" );
234 if( p_sys->p_file == NULL )
236 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
241 setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
245 switch( p_intf->p_sys->i_mode )
248 LOG_STRING( HTML_HEADER, p_sys->p_file );
252 LOG_STRING( TEXT_HEADER, p_sys->p_file );
259 p_sys->p_file = NULL;
261 openlog( "vlc", LOG_PID|LOG_NDELAY, LOG_DAEMON );
265 psz_rrd_file = config_GetPsz( p_intf, "rrd-file" );
266 if( psz_rrd_file && *psz_rrd_file )
268 FILE *rrd = utf8_fopen( psz_rrd_file, "w" );
271 setvbuf (rrd, NULL, _IOLBF, BUFSIZ);
272 if (!vlc_clone (&p_sys->rrd.thread, DoRRD, p_intf,
273 VLC_THREAD_PRIORITY_LOW))
274 p_sys->rrd.stream = rrd;
279 free( psz_rrd_file );
281 p_sys->p_sub = msg_Subscribe( p_intf );
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;
293 intf_sys_t *p_sys = p_intf->p_sys;
295 if (p_sys->rrd.stream)
297 vlc_cancel (p_sys->rrd.thread);
298 vlc_join (p_sys->rrd.thread, NULL);
301 /* Flush the queue and unsubscribe from the message queue */
302 FlushQueue( p_sys->p_sub, p_sys->p_file,
304 var_CreateGetInteger( p_intf, "verbose" ) );
305 msg_Unsubscribe( p_intf, p_sys->p_sub );
307 switch( p_sys->i_mode )
310 LOG_STRING( HTML_FOOTER, p_sys->p_file );
319 LOG_STRING( TEXT_FOOTER, p_sys->p_file );
323 /* Close the log file */
324 if( p_sys->i_mode != MODE_SYSLOG )
325 fclose( p_sys->p_file );
327 /* Destroy structure */
331 static void Run( intf_thread_t *p_intf )
335 int canc = vlc_savecancel();
336 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
337 p_intf->p_sys->i_mode,
338 var_CreateGetInteger( p_intf, "verbose" ) );
340 vlc_restorecancel( canc );
341 /* FIXME: this is WRONG. */
342 msleep( INTF_IDLE_SLEEP );
346 /*****************************************************************************
347 * FlushQueue: flush the message queue into the log
348 *****************************************************************************/
349 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 )
365 switch( p_sub->p_msg[i_start].i_type )
368 if( i_verbose < 0 ) continue;
371 if( i_verbose < 0 ) continue;
374 if( i_verbose < 1 ) continue;
377 if( i_verbose < 2 ) continue;
384 HtmlPrint( &p_sub->p_msg[i_start], p_file );
388 SyslogPrint( &p_sub->p_msg[i_start] );
393 TextPrint( &p_sub->p_msg[i_start], p_file );
398 vlc_mutex_lock( p_sub->p_lock );
399 p_sub->i_start = i_start;
400 vlc_mutex_unlock( p_sub->p_lock );
404 static const char *ppsz_type[4] = { ": ", " error: ",
405 " warning: ", " debug: " };
407 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
409 LOG_STRING( p_msg->psz_module, p_file );
410 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
411 LOG_STRING( p_msg->psz_msg, p_file );
412 LOG_STRING( "\n", p_file );
416 static void SyslogPrint( const msg_item_t *p_msg )
418 static const int i_prio[4] = { LOG_INFO, LOG_ERR, LOG_WARNING, LOG_DEBUG };
419 int i_priority = i_prio[p_msg->i_type];
421 if( p_msg->psz_header )
422 syslog( i_priority, "%s%s %s: %s", p_msg->psz_header,
423 ppsz_type[p_msg->i_type],
424 p_msg->psz_module, p_msg->psz_msg );
426 syslog( i_priority, "%s%s: %s", p_msg->psz_module,
427 ppsz_type[p_msg->i_type], p_msg->psz_msg );
432 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
434 static const char *ppsz_color[4] = { "<span style=\"color: #ffffff\">",
435 "<span style=\"color: #ff6666\">",
436 "<span style=\"color: #ffff66\">",
437 "<span style=\"color: #aaaaaa\">" };
439 LOG_STRING( p_msg->psz_module, p_file );
440 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
441 LOG_STRING( ppsz_color[p_msg->i_type], p_file );
442 LOG_STRING( p_msg->psz_msg, p_file );
443 LOG_STRING( "</span>\n", p_file );
446 static void *DoRRD (void *data)
448 intf_thread_t *p_intf = data;
449 FILE *file = p_intf->p_sys->rrd.stream;
453 /* FIXME: I wonder how memory synchronization occurs here...
455 if( p_intf->p_libvlc->p_stats )
457 lldiv_t in = lldiv( p_intf->p_libvlc->p_stats->f_input_bitrate * 1000000,
459 lldiv_t dm = lldiv( p_intf->p_libvlc->p_stats->f_demux_bitrate * 1000000,
461 lldiv_t out = lldiv( p_intf->p_libvlc->p_stats->f_output_bitrate * 1000000,
464 "%"PRIi64":%lld.%03llu:%lld.%03llu:%lld.%03llu\n",
465 (int64_t)time(NULL), in.quot, in.rem, dm.quot, dm.rem, out.quot, out.rem );
467 #undef msleep /* yeah, we really want to wake up every second here */