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