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