]> git.sesse.net Git - vlc/blob - modules/misc/logger.c
Improvements to preferences
[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 #ifdef UNDER_CE
34 #   define _IONBF 0x0004
35 #endif
36
37 #include <vlc/vlc.h>
38 #include <vlc/intf.h>
39
40 #define MODE_TEXT 0
41 #define MODE_HTML 1
42
43 #ifdef SYS_DARWIN
44 #define LOG_DIR "Library/Logs/"
45 #endif
46
47 #define LOG_FILE_TEXT "vlc-log.txt"
48 #define LOG_FILE_HTML "vlc-log.html"
49
50 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
51
52 #define TEXT_HEADER "-- logger module started --\n"
53 #define TEXT_FOOTER "-- logger module stopped --\n"
54
55 #define HTML_HEADER \
56     "<html>\n" \
57     "  <head>\n" \
58     "    <title>vlc log</title>\n" \
59     "  </head>\n" \
60     "  <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
61     "    <pre>\n" \
62     "      <b>-- logger module started --</b>\n"
63 #define HTML_FOOTER \
64     "      <b>-- logger module stopped --</b>\n" \
65     "    </pre>\n" \
66     "  </body>\n" \
67     "</html>\n"
68
69 /*****************************************************************************
70  * intf_sys_t: description and status of log interface
71  *****************************************************************************/
72 struct intf_sys_t
73 {
74     int i_mode;
75
76     FILE *    p_file; /* The log file */
77     msg_subscription_t *p_sub;
78 };
79
80 /*****************************************************************************
81  * Local prototypes
82  *****************************************************************************/
83 static int  Open    ( vlc_object_t * );                  
84 static void Close   ( vlc_object_t * );
85 static void Run     ( intf_thread_t * );
86
87 static void FlushQueue        ( msg_subscription_t *, FILE *, int );
88 static void TextPrint         ( const msg_item_t *, FILE * );
89 static void HtmlPrint         ( const msg_item_t *, FILE * );
90
91 /*****************************************************************************
92  * Module descriptor
93  *****************************************************************************/
94 static char *mode_list[] = { "text", "html" };
95 static char *mode_list_text[] = { N_("Text"), "HTML" };
96
97 #define LOGMODE_TEXT N_("Log format")
98 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are \"text\" (default) and \"html\".")
99
100 vlc_module_begin();
101     set_category( CAT_ADVANCED );
102     set_subcategory( SUBCAT_ADVANCED_MISC );
103     set_description( _("File logging interface") );
104
105     add_file( "logfile", NULL, NULL, N_("Log filename"), N_("Specify the log filename."), VLC_FALSE );
106     add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
107                 VLC_FALSE );
108         change_string_list( mode_list, mode_list_text, 0 );
109
110     set_capability( "interface", 0 );
111     set_callbacks( Open, Close );
112 vlc_module_end();
113
114 /*****************************************************************************
115  * Open: initialize and create stuff
116  *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
118 {   
119     intf_thread_t *p_intf = (intf_thread_t *)p_this;
120     char *psz_mode, *psz_file;
121
122     CONSOLE_INTRO_MSG;
123     msg_Info( p_intf, "Using the logger interface module..." );
124
125     /* Allocate instance and initialize some members */
126     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
127     if( p_intf->p_sys == NULL )
128     {
129         msg_Err( p_intf, "out of memory" );
130         return -1;
131     }
132
133     psz_mode = config_GetPsz( p_intf, "logmode" );
134     if( psz_mode )
135     {
136         if( !strcmp( psz_mode, "text" ) )
137         {
138             p_intf->p_sys->i_mode = MODE_TEXT;
139         }
140         else if( !strcmp( psz_mode, "html" ) )
141         {
142             p_intf->p_sys->i_mode = MODE_HTML;
143         }
144         else
145         {
146             msg_Err( p_intf, "invalid log mode `%s', using `text'", psz_mode );
147             p_intf->p_sys->i_mode = MODE_TEXT;
148         }
149
150         free( psz_mode );
151     }
152     else
153     {
154         msg_Warn( p_intf, "no log mode specified, using `text'" );
155         p_intf->p_sys->i_mode = MODE_TEXT;
156     }
157
158     psz_file = config_GetPsz( p_intf, "logfile" );
159     if( !psz_file )
160     {
161 #ifdef SYS_DARWIN
162         char *psz_homedir = p_this->p_vlc->psz_homedir; 
163
164         if( !psz_homedir )
165         {
166             msg_Err( p_this, "psz_homedir is null" );
167             return -1;
168         }
169         psz_file = (char *)malloc( sizeof("/" LOG_DIR "/" LOG_FILE_HTML) +
170                                        strlen(psz_homedir) );
171         if( psz_file )
172         {
173             switch( p_intf->p_sys->i_mode )
174             {
175             case MODE_HTML:
176                 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_HTML,
177                      psz_homedir );
178                 break;
179             case MODE_TEXT:
180             default:
181                 sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_TEXT,
182                      psz_homedir );
183                 break;
184             }
185         }
186 #else
187         switch( p_intf->p_sys->i_mode )
188         {
189         case MODE_HTML:
190             psz_file = strdup( LOG_FILE_HTML );
191             break;
192         case MODE_TEXT:
193         default:
194             psz_file = strdup( LOG_FILE_TEXT );
195             break;
196         }
197 #endif
198         msg_Warn( p_intf, "no log filename provided, using `%s'", psz_file );
199     }
200
201     /* Open the log file and remove any buffering for the stream */
202     msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
203     p_intf->p_sys->p_file = fopen( psz_file, "wt" );
204     if( p_intf->p_sys->p_file == NULL )
205     {
206         msg_Err( p_intf, "error opening logfile `%s'", psz_file );
207         free( p_intf->p_sys );
208         free( psz_file );
209         return -1;
210     }
211     setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
212     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
213
214     free( psz_file );
215
216     switch( p_intf->p_sys->i_mode )
217     {
218     case MODE_HTML:
219         LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
220         break;
221     case MODE_TEXT:
222     default:
223         LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
224         break;
225     }
226
227     p_intf->pf_run = Run;
228
229     return 0;
230 }
231
232 /*****************************************************************************
233  * Close: destroy interface stuff
234  *****************************************************************************/
235 static void Close( vlc_object_t *p_this )
236 {       
237     intf_thread_t *p_intf = (intf_thread_t *)p_this;
238         
239     /* Flush the queue and unsubscribe from the message queue */
240     FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
241                 p_intf->p_sys->i_mode );
242     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
243
244     switch( p_intf->p_sys->i_mode )
245     {
246     case MODE_HTML:
247         LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
248         break;
249     case MODE_TEXT:
250     default:
251         LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
252         break;
253     }
254
255     /* Close the log file */
256     fclose( p_intf->p_sys->p_file );
257
258     /* Destroy structure */
259     free( p_intf->p_sys );
260 }
261
262 /*****************************************************************************
263  * Run: rc thread
264  *****************************************************************************
265  * This part of the interface is in a separate thread so that we can call
266  * exec() from within it without annoying the rest of the program.
267  *****************************************************************************/
268 static void Run( intf_thread_t *p_intf )
269 {
270     while( !p_intf->b_die )
271     {
272         FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
273                     p_intf->p_sys->i_mode );
274
275         msleep( INTF_IDLE_SLEEP );
276     }
277 }
278
279 /*****************************************************************************
280  * FlushQueue: flush the message queue into the log file
281  *****************************************************************************/
282 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
283 {
284     int i_start, i_stop;
285
286     vlc_mutex_lock( p_sub->p_lock );
287     i_stop = *p_sub->pi_stop;
288     vlc_mutex_unlock( p_sub->p_lock );
289
290     if( p_sub->i_start != i_stop )
291     {
292         /* Append all messages to log file */
293         for( i_start = p_sub->i_start;
294              i_start != i_stop;
295              i_start = (i_start+1) % VLC_MSG_QSIZE )
296         {
297             switch( i_mode )
298             {
299             case MODE_HTML:
300                 HtmlPrint( &p_sub->p_msg[i_start], p_file );
301                 break;
302             case MODE_TEXT:
303             default:
304                 TextPrint( &p_sub->p_msg[i_start], p_file );
305                 break;
306             }
307         }
308
309         vlc_mutex_lock( p_sub->p_lock );
310         p_sub->i_start = i_start;
311         vlc_mutex_unlock( p_sub->p_lock );
312     }
313 }
314
315 static const char *ppsz_type[4] = { ": ", " error: ",
316                                     " warning: ", " debug: " };
317
318 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
319 {
320     LOG_STRING( p_msg->psz_module, p_file );
321     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
322     LOG_STRING( p_msg->psz_msg, p_file );
323     LOG_STRING( "\n", p_file );
324 }
325
326 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
327 {
328     static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
329                                          "<font color=\"#ff6666\">",
330                                          "<font color=\"#ffff66\">",
331                                          "<font color=\"#aaaaaa\">" };
332
333     LOG_STRING( p_msg->psz_module, p_file );
334     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
335     LOG_STRING( ppsz_color[p_msg->i_type], p_file );
336     LOG_STRING( p_msg->psz_msg, p_file );
337     LOG_STRING( "</font>\n", p_file );
338 }
339