]> git.sesse.net Git - vlc/blob - plugins/text/logger.c
04bf16e5d2676742742c1560305e55e9e96622fd
[vlc] / plugins / text / logger.c
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 $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdio.h>
32
33 #include <videolan/vlc.h>
34
35 #include "interface.h"
36
37 #define LOG_FILE "vlc.log"
38 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
39
40 /*****************************************************************************
41  * intf_sys_t: description and status of log interface
42  *****************************************************************************/
43 typedef struct intf_sys_s
44 {
45     FILE *    p_file; /* The log file */
46     intf_subscription_t *p_sub;
47
48 } intf_sys_t;
49
50 /*****************************************************************************
51  * Local prototypes.
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 );
57
58 static void FlushQueue        ( intf_subscription_t *, FILE * );
59
60 /*****************************************************************************
61  * Build configuration tree.
62  *****************************************************************************/
63 MODULE_CONFIG_START
64 MODULE_CONFIG_STOP
65
66 MODULE_INIT_START
67     SET_DESCRIPTION( "file logging interface module" )
68     ADD_CAPABILITY( INTF, 1 )
69     ADD_SHORTCUT( "logger" )
70 MODULE_INIT_STOP
71
72 MODULE_ACTIVATE_START
73     intf_getfunctions( &p_module->p_functions->intf );
74 MODULE_ACTIVATE_STOP
75
76 MODULE_DEACTIVATE_START
77 MODULE_DEACTIVATE_STOP
78
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 )
84 {
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;
88 }
89
90 /*****************************************************************************
91  * intf_Open: initialize and create stuff
92  *****************************************************************************/
93 static int intf_Open( intf_thread_t *p_intf )
94 {
95     char *psz_filename_tmp, *psz_filename;
96
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 )
100     {
101         intf_ErrMsg( "intf error: %s", strerror(ENOMEM) );
102         return -1;
103     }
104
105     if( !(psz_filename = psz_filename_tmp
106           = config_GetPszVariable( INTF_METHOD_VAR )) )
107     {
108         intf_ErrMsg( "intf error: configuration variable %s empty",
109                      INTF_METHOD_VAR );
110         return -1;
111     }
112
113     while( *psz_filename && *psz_filename != ':' )
114     {
115         psz_filename++;
116     }
117
118     if( *psz_filename == ':' )
119     {
120         psz_filename++;
121     }
122     else
123     {
124         intf_ErrMsg( "intf error: no log filename provided, using `%s'",
125                      LOG_FILE );
126         psz_filename = LOG_FILE;
127     }
128
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" );
132
133     p_intf->p_sys->p_sub = intf_MsgSub();
134
135     if( p_intf->p_sys->p_file == NULL )
136     {
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 );
141         return -1;
142     }
143
144     free( psz_filename_tmp );
145
146     LOG_STRING( "-- log plugin started --\n", p_intf->p_sys->p_file );
147
148     return 0;
149 }
150
151 /*****************************************************************************
152  * intf_Close: destroy interface stuff
153  *****************************************************************************/
154 static void intf_Close( intf_thread_t *p_intf )
155 {
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 );
159
160     LOG_STRING( "-- log plugin stopped --\n", p_intf->p_sys->p_file );
161
162     /* Close the log file */
163     fclose( p_intf->p_sys->p_file );
164
165     /* Destroy structure */
166     free( p_intf->p_sys );
167 }
168
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 )
176 {
177     while( !p_intf->b_die )
178     {
179         p_intf->pf_manage( p_intf );
180
181         FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file );
182
183         msleep( INTF_IDLE_SLEEP );
184     }
185 }
186
187 /*****************************************************************************
188  * FlushQueue: flush the message queue into the log file
189  *****************************************************************************/
190 static void FlushQueue( intf_subscription_t *p_sub, FILE *p_file )
191 {
192     int i_start, i_stop;
193     char *psz_msg;
194
195     vlc_mutex_lock( p_sub->p_lock );
196     i_stop = *p_sub->pi_stop;
197     vlc_mutex_unlock( p_sub->p_lock );
198
199     if( p_sub->i_start != i_stop )
200     {
201         /* Append all messages to log file */
202         for( i_start = p_sub->i_start;
203              i_start != i_stop;
204              i_start = (i_start+1) % INTF_MSG_QSIZE )
205         {
206             psz_msg = p_sub->p_msg[i_start].psz_msg;
207             LOG_STRING( psz_msg, p_file );
208             LOG_STRING( "\n", p_file );
209         }
210
211         vlc_mutex_lock( p_sub->p_lock );
212         p_sub->i_start = i_start;
213         vlc_mutex_unlock( p_sub->p_lock );
214     }
215 }
216