]> git.sesse.net Git - vlc/blob - modules/misc/logger.c
* Remove directories with only one source file inside
[vlc] / modules / misc / logger.c
1 /*****************************************************************************
2  * logger.c : file logging plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id$
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_t
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 int  Open    ( vlc_object_t * );                  
74 static void Close   ( vlc_object_t * );
75 static void Run     ( intf_thread_t * );
76
77 static void FlushQueue        ( msg_subscription_t *, FILE *, int );
78 static void TextPrint         ( const msg_item_t *, FILE * );
79 static void HtmlPrint         ( const msg_item_t *, FILE * );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 static char *mode_list[] = { "text", "html" };
85 static char *mode_list_text[] = { N_("Text"), N_("Html") };
86
87 #define LOGMODE_TEXT N_("Log format")
88 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default) and \"html\".")
89
90 vlc_module_begin();
91     set_description( _("File logging interface") );
92
93     add_file( "logfile", NULL, NULL, N_("Log filename"), N_("Specify the log filename."), VLC_FALSE );
94     add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
95                 VLC_FALSE );
96         change_string_list( mode_list, mode_list_text, 0 );
97
98     set_capability( "interface", 0 );
99     set_callbacks( Open, Close );
100 vlc_module_end();
101
102 /*****************************************************************************
103  * Open: initialize and create stuff
104  *****************************************************************************/
105 static int Open( vlc_object_t *p_this )
106 {   
107     intf_thread_t *p_intf = (intf_thread_t *)p_this;
108     char *psz_mode, *psz_file;
109
110     CONSOLE_INTRO_MSG;
111     msg_Info( p_intf, "Using the logger interface module..." );
112
113     /* Allocate instance and initialize some members */
114     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
115     if( p_intf->p_sys == NULL )
116     {
117         msg_Err( p_intf, "out of memory" );
118         return -1;
119     }
120
121     psz_mode = config_GetPsz( p_intf, "logmode" );
122     if( psz_mode )
123     {
124         if( !strcmp( psz_mode, "text" ) )
125         {
126             p_intf->p_sys->i_mode = MODE_TEXT;
127         }
128         else if( !strcmp( psz_mode, "html" ) )
129         {
130             p_intf->p_sys->i_mode = MODE_HTML;
131         }
132         else
133         {
134             msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
135             p_intf->p_sys->i_mode = MODE_TEXT;
136         }
137
138         free( psz_mode );
139     }
140     else
141     {
142         msg_Warn( p_intf, "no log mode specified, using `text'" );
143         p_intf->p_sys->i_mode = MODE_TEXT;
144     }
145
146     psz_file = config_GetPsz( p_intf, "logfile" );
147     if( !psz_file )
148     {
149         switch( p_intf->p_sys->i_mode )
150         {
151         case MODE_HTML:
152             psz_file = strdup( "vlc-log.html" );
153             break;
154         case MODE_TEXT:
155         default:
156             psz_file = strdup( "vlc-log.txt" );
157             break;
158         }
159
160         msg_Warn( p_intf, "no log filename provided, using `%s'", psz_file );
161     }
162
163     /* Open the log file and remove any buffering for the stream */
164     msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
165     p_intf->p_sys->p_file = fopen( psz_file, "wt" );
166     if( p_intf->p_sys->p_file == NULL )
167     {
168         msg_Err( p_intf, "error opening logfile `%s'", psz_file );
169         free( p_intf->p_sys );
170         free( psz_file );
171         return -1;
172     }
173     setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
174     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
175
176     free( psz_file );
177
178     switch( p_intf->p_sys->i_mode )
179     {
180     case MODE_HTML:
181         LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
182         break;
183     case MODE_TEXT:
184     default:
185         LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
186         break;
187     }
188
189     p_intf->pf_run = Run;
190
191     return 0;
192 }
193
194 /*****************************************************************************
195  * Close: destroy interface stuff
196  *****************************************************************************/
197 static void Close( vlc_object_t *p_this )
198 {       
199     intf_thread_t *p_intf = (intf_thread_t *)p_this;
200         
201     /* Flush the queue and unsubscribe from the message queue */
202     FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
203                 p_intf->p_sys->i_mode );
204     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
205
206     switch( p_intf->p_sys->i_mode )
207     {
208     case MODE_HTML:
209         LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
210         break;
211     case MODE_TEXT:
212     default:
213         LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
214         break;
215     }
216
217     /* Close the log file */
218     fclose( p_intf->p_sys->p_file );
219
220     /* Destroy structure */
221     free( p_intf->p_sys );
222 }
223
224 /*****************************************************************************
225  * Run: rc thread
226  *****************************************************************************
227  * This part of the interface is in a separate thread so that we can call
228  * exec() from within it without annoying the rest of the program.
229  *****************************************************************************/
230 static void Run( intf_thread_t *p_intf )
231 {
232     while( !p_intf->b_die )
233     {
234         FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
235                     p_intf->p_sys->i_mode );
236
237         msleep( INTF_IDLE_SLEEP );
238     }
239 }
240
241 /*****************************************************************************
242  * FlushQueue: flush the message queue into the log file
243  *****************************************************************************/
244 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
245 {
246     int i_start, i_stop;
247
248     vlc_mutex_lock( p_sub->p_lock );
249     i_stop = *p_sub->pi_stop;
250     vlc_mutex_unlock( p_sub->p_lock );
251
252     if( p_sub->i_start != i_stop )
253     {
254         /* Append all messages to log file */
255         for( i_start = p_sub->i_start;
256              i_start != i_stop;
257              i_start = (i_start+1) % VLC_MSG_QSIZE )
258         {
259             switch( i_mode )
260             {
261             case MODE_HTML:
262                 HtmlPrint( &p_sub->p_msg[i_start], p_file );
263                 break;
264             case MODE_TEXT:
265             default:
266                 TextPrint( &p_sub->p_msg[i_start], p_file );
267                 break;
268             }
269         }
270
271         vlc_mutex_lock( p_sub->p_lock );
272         p_sub->i_start = i_start;
273         vlc_mutex_unlock( p_sub->p_lock );
274     }
275 }
276
277 static const char *ppsz_type[4] = { ": ", " error: ",
278                                     " warning: ", " debug: " };
279
280 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
281 {
282     LOG_STRING( p_msg->psz_module, p_file );
283     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
284     LOG_STRING( p_msg->psz_msg, p_file );
285     LOG_STRING( "\n", p_file );
286 }
287
288 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
289 {
290     static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
291                                          "<font color=\"#ff6666\">",
292                                          "<font color=\"#ffff66\">",
293                                          "<font color=\"#aaaaaa\">" };
294
295     LOG_STRING( p_msg->psz_module, p_file );
296     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
297     LOG_STRING( ppsz_color[p_msg->i_type], p_file );
298     LOG_STRING( p_msg->psz_msg, p_file );
299     LOG_STRING( "</font>\n", p_file );
300 }
301