]> git.sesse.net Git - vlc/blob - plugins/text/logger.c
ab04952f285b91b86af863e80a9130e17c4be072
[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.3 2002/02/20 05:56:18 sam 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;
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     psz_filename = main_GetPszVariable( INTF_METHOD_VAR, NULL );
106
107     while( *psz_filename && *psz_filename != ':' )
108     {
109         psz_filename++;
110     }
111
112     if( *psz_filename == ':' )
113     {
114         psz_filename++;
115     }
116     else
117     {
118         intf_ErrMsg( "intf error: no log filename provided, using `%s'",
119                      LOG_FILE );
120         psz_filename = LOG_FILE;
121     }
122
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" );
126
127     p_intf->p_sys->p_sub = intf_MsgSub();
128
129     if( p_intf->p_sys->p_file == NULL )
130     {
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 );
134         return -1;
135     }
136
137     LOG_STRING( "-- log plugin started --\n", p_intf->p_sys->p_file );
138
139     return 0;
140 }
141
142 /*****************************************************************************
143  * intf_Close: destroy interface stuff
144  *****************************************************************************/
145 static void intf_Close( intf_thread_t *p_intf )
146 {
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 );
150
151     LOG_STRING( "-- log plugin stopped --\n", p_intf->p_sys->p_file );
152
153     /* Close the log file */
154     fclose( p_intf->p_sys->p_file );
155
156     /* Destroy structure */
157     free( p_intf->p_sys );
158 }
159
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 )
167 {
168     while( !p_intf->b_die )
169     {
170         p_intf->pf_manage( p_intf );
171
172         FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file );
173
174         msleep( INTF_IDLE_SLEEP );
175     }
176 }
177
178 /*****************************************************************************
179  * FlushQueue: flush the message queue into the log file
180  *****************************************************************************/
181 static void FlushQueue( intf_subscription_t *p_sub, FILE *p_file )
182 {
183     int i_start, i_stop;
184     char *psz_msg;
185
186     vlc_mutex_lock( p_sub->p_lock );
187     i_stop = *p_sub->pi_stop;
188     vlc_mutex_unlock( p_sub->p_lock );
189
190     if( p_sub->i_start != i_stop )
191     {
192         /* Append all messages to log file */
193         for( i_start = p_sub->i_start;
194              i_start != i_stop;
195              i_start = (i_start+1) % INTF_MSG_QSIZE )
196         {
197             psz_msg = p_sub->p_msg[i_start].psz_msg;
198             LOG_STRING( psz_msg, p_file );
199             LOG_STRING( "\n", p_file );
200         }
201
202         vlc_mutex_lock( p_sub->p_lock );
203         p_sub->i_start = i_start;
204         vlc_mutex_unlock( p_sub->p_lock );
205     }
206 }
207