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