1 /*****************************************************************************
2 * logger.c : file logging plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: logger.c,v 1.4 2002/02/24 20:51:10 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 */
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 )
95 char *psz_filename_tmp, *psz_filename;
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 if( !(psz_filename = psz_filename_tmp
106 = config_GetPszVariable( INTF_METHOD_VAR )) )
108 intf_ErrMsg( "intf error: configuration variable %s empty",
113 while( *psz_filename && *psz_filename != ':' )
118 if( *psz_filename == ':' )
124 intf_ErrMsg( "intf error: no log filename provided, using `%s'",
126 psz_filename = LOG_FILE;
129 /* Open the log file */
130 intf_WarnMsg( 1, "intf: opening logfile `%s'", psz_filename );
131 p_intf->p_sys->p_file = fopen( psz_filename, "w" );
133 p_intf->p_sys->p_sub = intf_MsgSub();
135 if( p_intf->p_sys->p_file == NULL )
137 intf_ErrMsg( "intf error: error opening logfile `%s'", psz_filename );
138 free( p_intf->p_sys );
139 intf_MsgUnsub( p_intf->p_sys->p_sub );
140 free( psz_filename_tmp );
144 free( psz_filename_tmp );
146 LOG_STRING( "-- log plugin started --\n", p_intf->p_sys->p_file );
151 /*****************************************************************************
152 * intf_Close: destroy interface stuff
153 *****************************************************************************/
154 static void intf_Close( intf_thread_t *p_intf )
156 /* Flush the queue and unsubscribe from the message queue */
157 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file );
158 intf_MsgUnsub( p_intf->p_sys->p_sub );
160 LOG_STRING( "-- log plugin stopped --\n", p_intf->p_sys->p_file );
162 /* Close the log file */
163 fclose( p_intf->p_sys->p_file );
165 /* Destroy structure */
166 free( p_intf->p_sys );
169 /*****************************************************************************
170 * intf_Run: rc thread
171 *****************************************************************************
172 * This part of the interface is in a separate thread so that we can call
173 * exec() from within it without annoying the rest of the program.
174 *****************************************************************************/
175 static void intf_Run( intf_thread_t *p_intf )
177 while( !p_intf->b_die )
179 p_intf->pf_manage( p_intf );
181 FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file );
183 msleep( INTF_IDLE_SLEEP );
187 /*****************************************************************************
188 * FlushQueue: flush the message queue into the log file
189 *****************************************************************************/
190 static void FlushQueue( intf_subscription_t *p_sub, FILE *p_file )
195 vlc_mutex_lock( p_sub->p_lock );
196 i_stop = *p_sub->pi_stop;
197 vlc_mutex_unlock( p_sub->p_lock );
199 if( p_sub->i_start != i_stop )
201 /* Append all messages to log file */
202 for( i_start = p_sub->i_start;
204 i_start = (i_start+1) % INTF_MSG_QSIZE )
206 psz_msg = p_sub->p_msg[i_start].psz_msg;
207 LOG_STRING( psz_msg, p_file );
208 LOG_STRING( "\n", p_file );
211 vlc_mutex_lock( p_sub->p_lock );
212 p_sub->i_start = i_start;
213 vlc_mutex_unlock( p_sub->p_lock );