1 /*****************************************************************************
2 * logger.c : file logging plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
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 */
34 # define _IONBF 0x0004
44 #define LOG_DIR "Library/Logs/"
47 #define LOG_FILE_TEXT "vlc-log.txt"
48 #define LOG_FILE_HTML "vlc-log.html"
50 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
52 #define TEXT_HEADER "-- logger module started --\n"
53 #define TEXT_FOOTER "-- logger module stopped --\n"
58 " <title>vlc log</title>\n" \
60 " <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
62 " <b>-- logger module started --</b>\n"
64 " <b>-- logger module stopped --</b>\n" \
69 /*****************************************************************************
70 * intf_sys_t: description and status of log interface
71 *****************************************************************************/
76 FILE * p_file; /* The log file */
77 msg_subscription_t *p_sub;
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Open ( vlc_object_t * );
84 static void Close ( vlc_object_t * );
85 static void Run ( intf_thread_t * );
87 static void FlushQueue ( msg_subscription_t *, FILE *, int );
88 static void TextPrint ( const msg_item_t *, FILE * );
89 static void HtmlPrint ( const msg_item_t *, FILE * );
91 /*****************************************************************************
93 *****************************************************************************/
94 static char *mode_list[] = { "text", "html" };
95 static char *mode_list_text[] = { N_("Text"), "HTML" };
97 #define LOGMODE_TEXT N_("Log format")
98 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default) and \"html\".")
101 set_category( CAT_INTERFACE );
102 set_subcategory( SUBCAT_INTERFACE_CONTROL );
103 set_shortname( N_( "Logging" ) );
104 set_description( _("File logging") );
106 add_file( "logfile", NULL, NULL, N_("Log filename"), N_("Specify the log filename."), VLC_FALSE );
107 add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
109 change_string_list( mode_list, mode_list_text, 0 );
111 set_capability( "interface", 0 );
112 set_callbacks( Open, Close );
115 /*****************************************************************************
116 * Open: initialize and create stuff
117 *****************************************************************************/
118 static int Open( vlc_object_t *p_this )
120 intf_thread_t *p_intf = (intf_thread_t *)p_this;
121 char *psz_mode, *psz_file;
124 msg_Info( p_intf, "Using the logger interface module..." );
126 /* Allocate instance and initialize some members */
127 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
128 if( p_intf->p_sys == NULL )
130 msg_Err( p_intf, "out of memory" );
134 psz_mode = config_GetPsz( p_intf, "logmode" );
137 if( !strcmp( psz_mode, "text" ) )
139 p_intf->p_sys->i_mode = MODE_TEXT;
141 else if( !strcmp( psz_mode, "html" ) )
143 p_intf->p_sys->i_mode = MODE_HTML;
147 msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
148 p_intf->p_sys->i_mode = MODE_TEXT;
155 msg_Warn( p_intf, "no log mode specified, using `text'" );
156 p_intf->p_sys->i_mode = MODE_TEXT;
159 psz_file = config_GetPsz( p_intf, "logfile" );
163 char *psz_homedir = p_this->p_vlc->psz_homedir;
167 msg_Err( p_this, "psz_homedir is null" );
170 psz_file = (char *)malloc( sizeof("/" LOG_DIR "/" LOG_FILE_HTML) +
171 strlen(psz_homedir) );
174 switch( p_intf->p_sys->i_mode )
177 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_HTML,
182 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_TEXT,
188 switch( p_intf->p_sys->i_mode )
191 psz_file = strdup( LOG_FILE_HTML );
195 psz_file = strdup( LOG_FILE_TEXT );
199 msg_Warn( p_intf, "no log filename provided, using `%s'", psz_file );
202 /* Open the log file and remove any buffering for the stream */
203 msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
204 p_intf->p_sys->p_file = fopen( psz_file, "wt" );
205 if( p_intf->p_sys->p_file == NULL )
207 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
208 free( p_intf->p_sys );
212 setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
213 p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
217 switch( p_intf->p_sys->i_mode )
220 LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
224 LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
228 p_intf->pf_run = Run;
233 /*****************************************************************************
234 * Close: destroy interface stuff
235 *****************************************************************************/
236 static void Close( vlc_object_t *p_this )
238 intf_thread_t *p_intf = (intf_thread_t *)p_this;
240 /* Flush the queue and unsubscribe from the message queue */
241 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
242 p_intf->p_sys->i_mode );
243 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
245 switch( p_intf->p_sys->i_mode )
248 LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
252 LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
256 /* Close the log file */
257 fclose( p_intf->p_sys->p_file );
259 /* Destroy structure */
260 free( p_intf->p_sys );
263 /*****************************************************************************
265 *****************************************************************************
266 * This part of the interface is in a separate thread so that we can call
267 * exec() from within it without annoying the rest of the program.
268 *****************************************************************************/
269 static void Run( intf_thread_t *p_intf )
271 while( !p_intf->b_die )
273 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
274 p_intf->p_sys->i_mode );
276 msleep( INTF_IDLE_SLEEP );
280 /*****************************************************************************
281 * FlushQueue: flush the message queue into the log file
282 *****************************************************************************/
283 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
287 vlc_mutex_lock( p_sub->p_lock );
288 i_stop = *p_sub->pi_stop;
289 vlc_mutex_unlock( p_sub->p_lock );
291 if( p_sub->i_start != i_stop )
293 /* Append all messages to log file */
294 for( i_start = p_sub->i_start;
296 i_start = (i_start+1) % VLC_MSG_QSIZE )
301 HtmlPrint( &p_sub->p_msg[i_start], p_file );
305 TextPrint( &p_sub->p_msg[i_start], p_file );
310 vlc_mutex_lock( p_sub->p_lock );
311 p_sub->i_start = i_start;
312 vlc_mutex_unlock( p_sub->p_lock );
316 static const char *ppsz_type[4] = { ": ", " error: ",
317 " warning: ", " debug: " };
319 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
321 LOG_STRING( p_msg->psz_module, p_file );
322 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
323 LOG_STRING( p_msg->psz_msg, p_file );
324 LOG_STRING( "\n", p_file );
327 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
329 static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
330 "<font color=\"#ff6666\">",
331 "<font color=\"#ffff66\">",
332 "<font color=\"#aaaaaa\">" };
334 LOG_STRING( p_msg->psz_module, p_file );
335 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
336 LOG_STRING( ppsz_color[p_msg->i_type], p_file );
337 LOG_STRING( p_msg->psz_msg, p_file );
338 LOG_STRING( "</font>\n", p_file );