1 /*****************************************************************************
2 * logger.c : file logging plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: logger.c,v 1.9 2003/11/05 00:39:16 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <errno.h> /* ENOMEM */
39 #define LOG_FILE "vlc-log.txt"
40 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
42 #define TEXT_HEADER "-- logger module started --\n"
43 #define TEXT_FOOTER "-- logger module stopped --\n"
48 " <title>vlc log</title>\n" \
50 " <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
52 " <b>-- logger module started --</b>\n"
54 " <b>-- logger module stopped --</b>\n" \
59 /*****************************************************************************
60 * intf_sys_t: description and status of log interface
61 *****************************************************************************/
66 FILE * p_file; /* The log file */
67 msg_subscription_t *p_sub;
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close ( vlc_object_t * );
75 static void Run ( intf_thread_t * );
77 static void FlushQueue ( msg_subscription_t *, FILE *, int );
78 static void TextPrint ( const msg_item_t *, FILE * );
79 static void HtmlPrint ( const msg_item_t *, FILE * );
81 /*****************************************************************************
83 *****************************************************************************/
84 static char *mode_list[] = { "text", "html" };
85 static char *mode_list_text[] = { N_("Text"), N_("Html") };
87 #define LOGMODE_TEXT N_("Log format")
88 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default) and \"html\"")
91 add_category_hint( N_("Miscellaneous"), NULL, VLC_FALSE );
92 add_file( "logfile", NULL, NULL, N_("log filename"), N_("Specify the log filename."), VLC_FALSE );
93 add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
95 change_string_list( mode_list, mode_list_text, 0 );
96 set_description( _("file logging interface") );
97 set_capability( "interface", 0 );
98 set_callbacks( Open, Close );
101 /*****************************************************************************
102 * Open: initialize and create stuff
103 *****************************************************************************/
104 static int Open( vlc_object_t *p_this )
106 intf_thread_t *p_intf = (intf_thread_t *)p_this;
107 char *psz_mode, *psz_file;
110 msg_Info( p_intf, _("Using the logger interface plugin...") );
112 /* Allocate instance and initialize some members */
113 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
114 if( p_intf->p_sys == NULL )
116 msg_Err( p_intf, "out of memory" );
120 psz_mode = config_GetPsz( p_intf, "logmode" );
123 if( !strcmp( psz_mode, "text" ) )
125 p_intf->p_sys->i_mode = MODE_TEXT;
127 else if( !strcmp( psz_mode, "html" ) )
129 p_intf->p_sys->i_mode = MODE_HTML;
133 msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
134 p_intf->p_sys->i_mode = MODE_TEXT;
141 msg_Warn( p_intf, "no log mode specified, using `text'" );
142 p_intf->p_sys->i_mode = MODE_TEXT;
145 psz_file = config_GetPsz( p_intf, "logfile" );
148 switch( p_intf->p_sys->i_mode )
151 psz_file = strdup( "vlc-log.html" );
155 psz_file = strdup( "vlc-log.txt" );
159 msg_Warn( p_intf, "no log filename provided, using `%s'", psz_file );
162 /* Open the log file and remove any buffering for the stream */
163 msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
164 p_intf->p_sys->p_file = fopen( psz_file, "wt" );
165 if( p_intf->p_sys->p_file == NULL )
167 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
168 free( p_intf->p_sys );
172 setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
173 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
177 switch( p_intf->p_sys->i_mode )
180 LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
184 LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
188 p_intf->pf_run = Run;
193 /*****************************************************************************
194 * Close: destroy interface stuff
195 *****************************************************************************/
196 static void Close( vlc_object_t *p_this )
198 intf_thread_t *p_intf = (intf_thread_t *)p_this;
200 /* Flush the queue and unsubscribe from the message queue */
201 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
202 p_intf->p_sys->i_mode );
203 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
205 switch( p_intf->p_sys->i_mode )
208 LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
212 LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
216 /* Close the log file */
217 fclose( p_intf->p_sys->p_file );
219 /* Destroy structure */
220 free( p_intf->p_sys );
223 /*****************************************************************************
225 *****************************************************************************
226 * This part of the interface is in a separate thread so that we can call
227 * exec() from within it without annoying the rest of the program.
228 *****************************************************************************/
229 static void Run( intf_thread_t *p_intf )
231 while( !p_intf->b_die )
233 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
234 p_intf->p_sys->i_mode );
236 msleep( INTF_IDLE_SLEEP );
240 /*****************************************************************************
241 * FlushQueue: flush the message queue into the log file
242 *****************************************************************************/
243 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
247 vlc_mutex_lock( p_sub->p_lock );
248 i_stop = *p_sub->pi_stop;
249 vlc_mutex_unlock( p_sub->p_lock );
251 if( p_sub->i_start != i_stop )
253 /* Append all messages to log file */
254 for( i_start = p_sub->i_start;
256 i_start = (i_start+1) % VLC_MSG_QSIZE )
261 HtmlPrint( &p_sub->p_msg[i_start], p_file );
265 TextPrint( &p_sub->p_msg[i_start], p_file );
270 vlc_mutex_lock( p_sub->p_lock );
271 p_sub->i_start = i_start;
272 vlc_mutex_unlock( p_sub->p_lock );
276 static const char *ppsz_type[4] = { ": ", " error: ",
277 " warning: ", " debug: " };
279 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
281 LOG_STRING( p_msg->psz_module, p_file );
282 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
283 LOG_STRING( p_msg->psz_msg, p_file );
284 LOG_STRING( "\n", p_file );
287 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
289 static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
290 "<font color=\"#ff6666\">",
291 "<font color=\"#ffff66\">",
292 "<font color=\"#aaaaaa\">" };
294 LOG_STRING( p_msg->psz_module, p_file );
295 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
296 LOG_STRING( ppsz_color[p_msg->i_type], p_file );
297 LOG_STRING( p_msg->psz_msg, p_file );
298 LOG_STRING( "</font>\n", p_file );