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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <errno.h> /* ENOMEM */
34 # define _IONBF 0x0004
45 #define LOG_DIR "Library/Logs/"
48 #define LOG_FILE_TEXT "vlc-log.txt"
49 #define LOG_FILE_HTML "vlc-log.html"
51 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
53 #define TEXT_HEADER "-- logger module started --\n"
54 #define TEXT_FOOTER "-- logger module stopped --\n"
59 " <title>vlc log</title>\n" \
61 " <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
63 " <b>-- logger module started --</b>\n"
65 " <b>-- logger module stopped --</b>\n" \
74 /*****************************************************************************
75 * intf_sys_t: description and status of log interface
76 *****************************************************************************/
81 FILE * p_file; /* The log file */
82 msg_subscription_t *p_sub;
85 /*****************************************************************************
87 *****************************************************************************/
88 static int Open ( vlc_object_t * );
89 static void Close ( vlc_object_t * );
90 static void Run ( intf_thread_t * );
92 static void FlushQueue ( msg_subscription_t *, FILE *, int );
93 static void TextPrint ( const msg_item_t *, FILE * );
94 static void HtmlPrint ( const msg_item_t *, FILE * );
96 static void SyslogPrint ( const msg_item_t *);
99 /*****************************************************************************
101 *****************************************************************************/
102 static char *mode_list[] = { "text", "html"
107 static char *mode_list_text[] = { N_("Text"), "HTML"
113 #define LOGMODE_TEXT N_("Log format")
115 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default), \"html\", and \"syslog\".")
117 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default) and \"html\".")
121 set_shortname( N_( "Logging" ) );
122 set_description( _("File logging") );
124 add_file( "logfile", NULL, NULL,
125 N_("Log filename"), N_("Specify the log filename."), VLC_FALSE );
126 add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
128 change_string_list( mode_list, mode_list_text, 0 );
130 set_capability( "interface", 0 );
131 set_callbacks( Open, Close );
134 /*****************************************************************************
135 * Open: initialize and create stuff
136 *****************************************************************************/
137 static int Open( vlc_object_t *p_this )
139 intf_thread_t *p_intf = (intf_thread_t *)p_this;
140 char *psz_mode, *psz_file;
143 msg_Info( p_intf, "Using logger..." );
145 /* Allocate instance and initialize some members */
146 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
147 if( p_intf->p_sys == NULL )
149 msg_Err( p_intf, "out of memory" );
153 psz_mode = var_CreateGetString( p_intf, "logmode" );
156 if( !strcmp( psz_mode, "text" ) )
158 p_intf->p_sys->i_mode = MODE_TEXT;
160 else if( !strcmp( psz_mode, "html" ) )
162 p_intf->p_sys->i_mode = MODE_HTML;
165 else if( !strcmp( psz_mode, "syslog" ) )
167 p_intf->p_sys->i_mode = MODE_SYSLOG;
172 msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
173 p_intf->p_sys->i_mode = MODE_TEXT;
180 msg_Warn( p_intf, "no log mode specified, using `text'" );
181 p_intf->p_sys->i_mode = MODE_TEXT;
184 if( p_intf->p_sys->i_mode != MODE_SYSLOG )
186 psz_file = config_GetPsz( p_intf, "logfile" );
190 char *psz_homedir = p_this->p_vlc->psz_homedir;
194 msg_Err( p_this, "psz_homedir is null" );
197 psz_file = (char *)malloc( sizeof("/" LOG_DIR "/" LOG_FILE_HTML) +
198 strlen(psz_homedir) );
201 switch( p_intf->p_sys->i_mode )
204 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_HTML,
209 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_TEXT,
215 switch( p_intf->p_sys->i_mode )
218 psz_file = strdup( LOG_FILE_HTML );
222 psz_file = strdup( LOG_FILE_TEXT );
226 msg_Warn( p_intf, "no log filename provided, using `%s'",
230 /* Open the log file and remove any buffering for the stream */
231 msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
232 p_intf->p_sys->p_file = fopen( psz_file, "wt" );
233 if( p_intf->p_sys->p_file == NULL )
235 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
236 free( p_intf->p_sys );
240 setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
244 switch( p_intf->p_sys->i_mode )
247 LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
251 LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
258 p_intf->p_sys->p_file = NULL;
260 openlog( "VLC", 0, LOG_DAEMON );
264 p_intf->p_sys->p_sub = msg_Subscribe( p_intf , MSG_QUEUE_NORMAL );
265 p_intf->pf_run = Run;
270 /*****************************************************************************
271 * Close: destroy interface stuff
272 *****************************************************************************/
273 static void Close( vlc_object_t *p_this )
275 intf_thread_t *p_intf = (intf_thread_t *)p_this;
277 /* Flush the queue and unsubscribe from the message queue */
278 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
279 p_intf->p_sys->i_mode );
280 msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
282 switch( p_intf->p_sys->i_mode )
285 LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
294 LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
298 /* Close the log file */
299 if( p_intf->p_sys->i_mode != MODE_SYSLOG )
300 fclose( p_intf->p_sys->p_file );
302 /* Destroy structure */
303 free( p_intf->p_sys );
306 /*****************************************************************************
308 *****************************************************************************
309 * This part of the interface is in a separate thread so that we can call
310 * exec() from within it without annoying the rest of the program.
311 *****************************************************************************/
312 static void Run( intf_thread_t *p_intf )
314 while( !p_intf->b_die )
316 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
317 p_intf->p_sys->i_mode );
319 msleep( INTF_IDLE_SLEEP );
323 /*****************************************************************************
324 * FlushQueue: flush the message queue into the log
325 *****************************************************************************/
326 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
330 vlc_mutex_lock( p_sub->p_lock );
331 i_stop = *p_sub->pi_stop;
332 vlc_mutex_unlock( p_sub->p_lock );
334 if( p_sub->i_start != i_stop )
336 /* Append all messages to log file */
337 for( i_start = p_sub->i_start;
339 i_start = (i_start+1) % VLC_MSG_QSIZE )
344 HtmlPrint( &p_sub->p_msg[i_start], p_file );
348 SyslogPrint( &p_sub->p_msg[i_start] );
353 TextPrint( &p_sub->p_msg[i_start], p_file );
358 vlc_mutex_lock( p_sub->p_lock );
359 p_sub->i_start = i_start;
360 vlc_mutex_unlock( p_sub->p_lock );
364 static const char *ppsz_type[4] = { ": ", " error: ",
365 " warning: ", " debug: " };
367 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
369 LOG_STRING( p_msg->psz_module, p_file );
370 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
371 LOG_STRING( p_msg->psz_msg, p_file );
372 LOG_STRING( "\n", p_file );
376 static void SyslogPrint( const msg_item_t *p_msg )
378 int i_priority = LOG_INFO;
380 if( p_msg->i_type == 0 ) i_priority = LOG_INFO;
381 if( p_msg->i_type == 1 ) i_priority = LOG_ERR;
382 if( p_msg->i_type == 2 ) i_priority = LOG_WARNING;
383 if( p_msg->i_type == 3 ) i_priority = LOG_DEBUG;
385 syslog( i_priority, "%s %s", p_msg->psz_module, p_msg->psz_msg );
389 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
391 static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
392 "<font color=\"#ff6666\">",
393 "<font color=\"#ffff66\">",
394 "<font color=\"#aaaaaa\">" };
396 LOG_STRING( p_msg->psz_module, p_file );
397 LOG_STRING( ppsz_type[p_msg->i_type], p_file );
398 LOG_STRING( ppsz_color[p_msg->i_type], p_file );
399 LOG_STRING( p_msg->psz_msg, p_file );
400 LOG_STRING( "</font>\n", p_file );