1 /*****************************************************************************
2 * logger.c : file logging plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: logger.c,v 1.3 2002/02/20 05:56:18 sam 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 */
33 #include <videolan/vlc.h>
35 #include "interface.h"
37 #define LOG_FILE "vlc.log"
38 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
40 /*****************************************************************************
41 * intf_sys_t: description and status of log interface
42 *****************************************************************************/
43 typedef struct intf_sys_s
45 FILE * p_file; /* The log file */
46 intf_subscription_t *p_sub;
50 /*****************************************************************************
52 *****************************************************************************/
53 static void intf_getfunctions ( function_list_t * p_function_list );
54 static int intf_Open ( intf_thread_t *p_intf );
55 static void intf_Close ( intf_thread_t *p_intf );
56 static void intf_Run ( intf_thread_t *p_intf );
58 static void FlushQueue ( intf_subscription_t *, FILE * );
60 /*****************************************************************************
61 * Build configuration tree.
62 *****************************************************************************/
67 SET_DESCRIPTION( "file logging interface module" )
68 ADD_CAPABILITY( INTF, 1 )
69 ADD_SHORTCUT( "logger" )
73 intf_getfunctions( &p_module->p_functions->intf );
76 MODULE_DEACTIVATE_START
77 MODULE_DEACTIVATE_STOP
79 /*****************************************************************************
80 * Functions exported as capabilities. They are declared as static so that
81 * we don't pollute the namespace too much.
82 *****************************************************************************/
83 static void intf_getfunctions( function_list_t * p_function_list )
85 p_function_list->functions.intf.pf_open = intf_Open;
86 p_function_list->functions.intf.pf_close = intf_Close;
87 p_function_list->functions.intf.pf_run = intf_Run;
90 /*****************************************************************************
91 * intf_Open: initialize and create stuff
92 *****************************************************************************/
93 static int intf_Open( intf_thread_t *p_intf )
97 /* Allocate instance and initialize some members */
98 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
99 if( p_intf->p_sys == NULL )
101 intf_ErrMsg( "intf error: %s", strerror(ENOMEM) );
105 psz_filename = main_GetPszVariable( INTF_METHOD_VAR, NULL );
107 while( *psz_filename && *psz_filename != ':' )
112 if( *psz_filename == ':' )
118 intf_ErrMsg( "intf error: no log filename provided, using `%s'",
120 psz_filename = LOG_FILE;
123 /* Open the log file */
124 intf_WarnMsg( 1, "intf: opening logfile `%s'", psz_filename );
125 p_intf->p_sys->p_file = fopen( psz_filename, "w" );
127 p_intf->p_sys->p_sub = intf_MsgSub();
129 if( p_intf->p_sys->p_file == NULL )
131 intf_ErrMsg( "intf error: error opening logfile `%s'", psz_filename );
132 free( p_intf->p_sys );
133 intf_MsgUnsub( p_intf->p_sys->p_sub );
137 LOG_STRING( "-- log plugin started --\n", p_intf->p_sys->p_file );
142 /*****************************************************************************
143 * intf_Close: destroy interface stuff
144 *****************************************************************************/
145 static void intf_Close( intf_thread_t *p_intf )
147 /* Flush the queue and unsubscribe from the message queue */
148 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file );
149 intf_MsgUnsub( p_intf->p_sys->p_sub );
151 LOG_STRING( "-- log plugin stopped --\n", p_intf->p_sys->p_file );
153 /* Close the log file */
154 fclose( p_intf->p_sys->p_file );
156 /* Destroy structure */
157 free( p_intf->p_sys );
160 /*****************************************************************************
161 * intf_Run: rc thread
162 *****************************************************************************
163 * This part of the interface is in a separate thread so that we can call
164 * exec() from within it without annoying the rest of the program.
165 *****************************************************************************/
166 static void intf_Run( intf_thread_t *p_intf )
168 while( !p_intf->b_die )
170 p_intf->pf_manage( p_intf );
172 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file );
174 msleep( INTF_IDLE_SLEEP );
178 /*****************************************************************************
179 * FlushQueue: flush the message queue into the log file
180 *****************************************************************************/
181 static void FlushQueue( intf_subscription_t *p_sub, FILE *p_file )
186 vlc_mutex_lock( p_sub->p_lock );
187 i_stop = *p_sub->pi_stop;
188 vlc_mutex_unlock( p_sub->p_lock );
190 if( p_sub->i_start != i_stop )
192 /* Append all messages to log file */
193 for( i_start = p_sub->i_start;
195 i_start = (i_start+1) % INTF_MSG_QSIZE )
197 psz_msg = p_sub->p_msg[i_start].psz_msg;
198 LOG_STRING( psz_msg, p_file );
199 LOG_STRING( "\n", p_file );
202 vlc_mutex_lock( p_sub->p_lock );
203 p_sub->i_start = i_start;
204 vlc_mutex_unlock( p_sub->p_lock );