]> git.sesse.net Git - vlc/blob - plugins/text/logger.c
* ALL: the first libvlc commit.
[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.10 2002/06/01 12:32:00 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 <vlc/vlc.h>
34 #include <vlc/intf.h>
35
36 #define MODE_TEXT 0
37 #define MODE_HTML 1
38
39 #define LOG_FILE "vlc-log.txt"
40 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
41
42 #define TEXT_HEADER "-- logger module started --\n"
43 #define TEXT_FOOTER "-- logger module stopped --\n"
44
45 #define HTML_HEADER \
46     "<html>\n" \
47     "  <head>\n" \
48     "    <title>vlc log</title>\n" \
49     "  </head>\n" \
50     "  <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
51     "    <pre>\n" \
52     "      <b>-- logger module started --</b>\n"
53 #define HTML_FOOTER \
54     "      <b>-- logger module stopped --</b>\n" \
55     "    </pre>\n" \
56     "  </body>\n" \
57     "</html>\n"
58
59 /*****************************************************************************
60  * intf_sys_t: description and status of log interface
61  *****************************************************************************/
62 struct intf_sys_s
63 {
64     int i_mode;
65
66     FILE *    p_file; /* The log file */
67     msg_subscription_t *p_sub;
68 };
69
70 /*****************************************************************************
71  * Local prototypes.
72  *****************************************************************************/
73 static void intf_getfunctions ( function_list_t * p_function_list );
74 static int  intf_Open         ( intf_thread_t *p_intf );
75 static void intf_Close        ( intf_thread_t *p_intf );
76 static void intf_Run          ( intf_thread_t *p_intf );
77
78 static void FlushQueue        ( msg_subscription_t *, FILE *, int );
79 static void TextPrint         ( const msg_item_t *, FILE * );
80 static void HtmlPrint         ( const msg_item_t *, FILE * );
81
82 /*****************************************************************************
83  * Build configuration tree.
84  *****************************************************************************/
85 MODULE_CONFIG_START
86     ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
87     ADD_STRING( "logfile", NULL, NULL, N_("log filename"), N_("Specify the log filename.") )
88     ADD_STRING( "logmode", NULL, NULL, N_("log format"), N_("Specify the log format. Available choices are \"text\" (default) and \"html\"") )
89 MODULE_CONFIG_STOP
90
91 MODULE_INIT_START
92     SET_DESCRIPTION( _("file logging interface module") )
93     ADD_CAPABILITY( INTF, 1 )
94 MODULE_INIT_STOP
95
96 MODULE_ACTIVATE_START
97     intf_getfunctions( &p_module->p_functions->intf );
98 MODULE_ACTIVATE_STOP
99
100 MODULE_DEACTIVATE_START
101 MODULE_DEACTIVATE_STOP
102
103 /*****************************************************************************
104  * Functions exported as capabilities. They are declared as static so that
105  * we don't pollute the namespace too much.
106  *****************************************************************************/
107 static void intf_getfunctions( function_list_t * p_function_list )
108 {
109     p_function_list->functions.intf.pf_open  = intf_Open;
110     p_function_list->functions.intf.pf_close = intf_Close;
111     p_function_list->functions.intf.pf_run   = intf_Run;
112 }
113
114 /*****************************************************************************
115  * intf_Open: initialize and create stuff
116  *****************************************************************************/
117 static int intf_Open( intf_thread_t *p_intf )
118 {
119     char *psz_mode, *psz_file;
120
121 #ifdef WIN32
122     AllocConsole();
123     freopen( "CONOUT$", "w", stdout );
124     freopen( "CONOUT$", "w", stderr );
125     freopen( "CONIN$", "r", stdin );
126     msg_Info( p_intf, VERSION_MESSAGE );
127     msg_Info( p_intf, _("\nUsing the logger interface plugin...") );
128 #endif
129
130     /* Allocate instance and initialize some members */
131     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
132     if( p_intf->p_sys == NULL )
133     {
134         msg_Err( p_intf, "out of memory" );
135         return -1;
136     }
137
138     psz_mode = config_GetPsz( p_intf, "logmode" );
139     if( psz_mode )
140     {
141         if( !strcmp( psz_mode, "text" ) )
142         {
143             p_intf->p_sys->i_mode = MODE_TEXT;
144         }
145         else if( !strcmp( psz_mode, "html" ) )
146         {
147             p_intf->p_sys->i_mode = MODE_HTML;
148         }
149         else
150         {
151             msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
152             p_intf->p_sys->i_mode = MODE_TEXT;
153         }
154
155         free( psz_mode );
156     }
157     else
158     {
159         msg_Warn( p_intf, "no log mode specified, using `text'" );
160         p_intf->p_sys->i_mode = MODE_TEXT;
161     }
162
163     psz_file = config_GetPsz( p_intf, "logfile" );
164     if( !psz_file )
165     {
166         switch( p_intf->p_sys->i_mode )
167         {
168         case MODE_HTML:
169             psz_file = strdup( "vlc-log.html" );
170             break;
171         case MODE_TEXT:
172         default:
173             psz_file = strdup( "vlc-log.txt" );
174             break;
175         }
176
177         msg_Warn( p_intf, "no log filename provided, using `%s'", psz_file );
178     }
179
180     /* Open the log file and remove any buffering for the stream */
181     msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
182     p_intf->p_sys->p_file = fopen( psz_file, "wt" );
183     setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
184
185     p_intf->p_sys->p_sub = msg_Subscribe( p_intf->p_this );
186
187     if( p_intf->p_sys->p_file == NULL )
188     {
189         msg_Err( p_intf, "error opening logfile `%s'", psz_file );
190         free( p_intf->p_sys );
191         msg_Unsubscribe( p_intf->p_this, p_intf->p_sys->p_sub );
192         free( psz_file );
193         return -1;
194     }
195
196     free( psz_file );
197
198     switch( p_intf->p_sys->i_mode )
199     {
200     case MODE_HTML:
201         LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
202         break;
203     case MODE_TEXT:
204     default:
205         LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
206         break;
207     }
208
209     return 0;
210 }
211
212 /*****************************************************************************
213  * intf_Close: destroy interface stuff
214  *****************************************************************************/
215 static void intf_Close( intf_thread_t *p_intf )
216 {
217     /* Flush the queue and unsubscribe from the message queue */
218     FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
219                 p_intf->p_sys->i_mode );
220     msg_Unsubscribe( p_intf->p_this, p_intf->p_sys->p_sub );
221
222     switch( p_intf->p_sys->i_mode )
223     {
224     case MODE_HTML:
225         LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
226         break;
227     case MODE_TEXT:
228     default:
229         LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
230         break;
231     }
232
233     /* Close the log file */
234     fclose( p_intf->p_sys->p_file );
235
236     /* Destroy structure */
237     free( p_intf->p_sys );
238 }
239
240 /*****************************************************************************
241  * intf_Run: rc thread
242  *****************************************************************************
243  * This part of the interface is in a separate thread so that we can call
244  * exec() from within it without annoying the rest of the program.
245  *****************************************************************************/
246 static void intf_Run( intf_thread_t *p_intf )
247 {
248     while( !p_intf->p_vlc->b_die )
249     {
250         FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
251                     p_intf->p_sys->i_mode );
252
253         msleep( INTF_IDLE_SLEEP );
254     }
255 }
256
257 /*****************************************************************************
258  * FlushQueue: flush the message queue into the log file
259  *****************************************************************************/
260 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
261 {
262     int i_start, i_stop;
263
264     vlc_mutex_lock( p_sub->p_lock );
265     i_stop = *p_sub->pi_stop;
266     vlc_mutex_unlock( p_sub->p_lock );
267
268     if( p_sub->i_start != i_stop )
269     {
270         /* Append all messages to log file */
271         for( i_start = p_sub->i_start;
272              i_start != i_stop;
273              i_start = (i_start+1) % VLC_MSG_QSIZE )
274         {
275             switch( i_mode )
276             {
277             case MODE_HTML:
278                 HtmlPrint( &p_sub->p_msg[i_start], p_file );
279                 break;
280             case MODE_TEXT:
281             default:
282                 TextPrint( &p_sub->p_msg[i_start], p_file );
283                 break;
284             }
285         }
286
287         vlc_mutex_lock( p_sub->p_lock );
288         p_sub->i_start = i_start;
289         vlc_mutex_unlock( p_sub->p_lock );
290     }
291 }
292
293 static const char *ppsz_type[4] = { ": ", " error: ",
294                                     " warning: ", " debug: " };
295
296 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
297 {
298     LOG_STRING( p_msg->psz_module, p_file );
299     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
300     LOG_STRING( p_msg->psz_msg, p_file );
301     LOG_STRING( "\n", p_file );
302 }
303
304 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
305 {
306     static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
307                                          "<font color=\"#ff6666\">",
308                                          "<font color=\"#ffff66\">",
309                                          "<font color=\"#aaaaaa\">" };
310
311     LOG_STRING( p_msg->psz_module, p_file );
312     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
313     LOG_STRING( ppsz_color[p_msg->i_type], p_file );
314     LOG_STRING( p_msg->psz_msg, p_file );
315     LOG_STRING( "</font>\n", p_file );
316 }
317