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