1 /*****************************************************************************
2 * logger.c : file logging plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: logger.c,v 1.2 2002/08/24 17:04:36 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 *****************************************************************************/
85 add_category_hint( N_("Miscellaneous"), NULL );
86 add_string( "logfile", NULL, NULL, N_("log filename"), N_("Specify the log filename.") );
87 add_string( "logmode", NULL, NULL, N_("log format"), N_("Specify the log format. Available choices are \"text\" (default) and \"html\"") );
88 set_description( _("file logging interface module") );
89 set_capability( "interface", 0 );
90 set_callbacks( Open, Close );
93 /*****************************************************************************
94 * Open: initialize and create stuff
95 *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
98 intf_thread_t *p_intf = (intf_thread_t *)p_this;
99 char *psz_mode, *psz_file;
103 freopen( "CONOUT$", "w", stdout );
104 freopen( "CONOUT$", "w", stderr );
105 freopen( "CONIN$", "r", stdin );
106 msg_Info( p_intf, VERSION_MESSAGE );
107 msg_Info( p_intf, _("\nUsing the logger interface plugin...") );
110 /* Allocate instance and initialize some members */
111 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
112 if( p_intf->p_sys == NULL )
114 msg_Err( p_intf, "out of memory" );
118 psz_mode = config_GetPsz( p_intf, "logmode" );
121 if( !strcmp( psz_mode, "text" ) )
123 p_intf->p_sys->i_mode = MODE_TEXT;
125 else if( !strcmp( psz_mode, "html" ) )
127 p_intf->p_sys->i_mode = MODE_HTML;
131 msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
132 p_intf->p_sys->i_mode = MODE_TEXT;
139 msg_Warn( p_intf, "no log mode specified, using `text'" );
140 p_intf->p_sys->i_mode = MODE_TEXT;
143 psz_file = config_GetPsz( p_intf, "logfile" );
146 switch( p_intf->p_sys->i_mode )
149 psz_file = strdup( "vlc-log.html" );
153 psz_file = strdup( "vlc-log.txt" );
157 msg_Warn( p_intf, "no log filename provided, using `%s'", psz_file );
160 /* Open the log file and remove any buffering for the stream */
161 msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
162 p_intf->p_sys->p_file = fopen( psz_file, "wt" );
163 if( p_intf->p_sys->p_file == NULL )
165 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
166 free( p_intf->p_sys );
170 setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
171 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
175 switch( p_intf->p_sys->i_mode )
178 LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
182 LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
186 p_intf->pf_run = Run;
191 /*****************************************************************************
192 * Close: destroy interface stuff
193 *****************************************************************************/
194 static void Close( vlc_object_t *p_this )
196 intf_thread_t *p_intf = (intf_thread_t *)p_this;
198 /* Flush the queue and unsubscribe from the message queue */
199 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
200 p_intf->p_sys->i_mode );
201 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
203 switch( p_intf->p_sys->i_mode )
206 LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
210 LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
214 /* Close the log file */
215 fclose( p_intf->p_sys->p_file );
217 /* Destroy structure */
218 free( p_intf->p_sys );
221 /*****************************************************************************
223 *****************************************************************************
224 * This part of the interface is in a separate thread so that we can call
225 * exec() from within it without annoying the rest of the program.
226 *****************************************************************************/
227 static void Run( intf_thread_t *p_intf )
229 while( !p_intf->b_die )
231 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
232 p_intf->p_sys->i_mode );
234 msleep( INTF_IDLE_SLEEP );
238 /*****************************************************************************
239 * FlushQueue: flush the message queue into the log file
240 *****************************************************************************/
241 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
245 vlc_mutex_lock( p_sub->p_lock );
246 i_stop = *p_sub->pi_stop;
247 vlc_mutex_unlock( p_sub->p_lock );
249 if( p_sub->i_start != i_stop )
251 /* Append all messages to log file */
252 for( i_start = p_sub->i_start;
254 i_start = (i_start+1) % VLC_MSG_QSIZE )
259 HtmlPrint( &p_sub->p_msg[i_start], p_file );
263 TextPrint( &p_sub->p_msg[i_start], p_file );
268 vlc_mutex_lock( p_sub->p_lock );
269 p_sub->i_start = i_start;
270 vlc_mutex_unlock( p_sub->p_lock );
274 static const char *ppsz_type[4] = { ": ", " error: ",
275 " warning: ", " debug: " };
277 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
279 LOG_STRING( p_msg->psz_module, p_file );
280 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
281 LOG_STRING( p_msg->psz_msg, p_file );
282 LOG_STRING( "\n", p_file );
285 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
287 static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
288 "<font color=\"#ff6666\">",
289 "<font color=\"#ffff66\">",
290 "<font color=\"#aaaaaa\">" };
292 LOG_STRING( p_msg->psz_module, p_file );
293 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
294 LOG_STRING( ppsz_color[p_msg->i_type], p_file );
295 LOG_STRING( p_msg->psz_msg, p_file );
296 LOG_STRING( "</font>\n", p_file );