]> git.sesse.net Git - vlc/blob - modules/misc/logger.c
Remove non-working and hardly documented RRD support
[vlc] / modules / misc / logger.c
1 /*****************************************************************************
2  * logger.c : file logging plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2008 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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_charset.h>
37
38 #include <assert.h>
39
40 #ifdef UNDER_CE
41 #   define _IONBF 0x0004
42 #endif
43
44 #define MODE_TEXT 0
45 #define MODE_HTML 1
46 #define MODE_SYSLOG 2
47
48 #ifdef __APPLE__
49 #define LOG_DIR "Library/Logs/"
50 #endif
51
52 #define LOG_FILE_TEXT "vlc-log.txt"
53 #define LOG_FILE_HTML "vlc-log.html"
54
55 #define TEXT_HEADER "-- logger module started --\n"
56 #define TEXT_FOOTER "-- logger module stopped --\n"
57
58 #define HTML_HEADER \
59     "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
60     "  \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
61     "<html>\n" \
62     "  <head>\n" \
63     "    <title>vlc log</title>\n" \
64     "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" \
65     "  </head>\n" \
66     "  <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
67     "    <pre>\n" \
68     "      <b>-- logger module started --</b>\n"
69 #define HTML_FOOTER \
70     "      <b>-- logger module stopped --</b>\n" \
71     "    </pre>\n" \
72     "  </body>\n" \
73     "</html>\n"
74
75 #if HAVE_SYSLOG_H
76 #include <syslog.h>
77 #endif
78
79 struct msg_cb_data_t
80 {
81     intf_thread_t *p_intf;
82     FILE *p_file;
83     int   i_mode;
84 };
85
86 /*****************************************************************************
87  * intf_sys_t: description and status of log interface
88  *****************************************************************************/
89 struct intf_sys_t
90 {
91     msg_subscription_t *p_sub;
92     msg_cb_data_t msg;
93 };
94
95 /*****************************************************************************
96  * Local prototypes
97  *****************************************************************************/
98 static int  Open    ( vlc_object_t * );
99 static void Close   ( vlc_object_t * );
100
101 static void Overflow (msg_cb_data_t *p_sys, msg_item_t *p_item, unsigned overruns);
102 static void TextPrint         ( const msg_item_t *, FILE * );
103 static void HtmlPrint         ( const msg_item_t *, FILE * );
104 #ifdef HAVE_SYSLOG_H
105 static void SyslogPrint       ( const msg_item_t *);
106 #endif
107
108 /*****************************************************************************
109  * Module descriptor
110  *****************************************************************************/
111 static const char *const mode_list[] = { "text", "html"
112 #ifdef HAVE_SYSLOG_H
113 ,"syslog"
114 #endif
115 };
116 static const char *const mode_list_text[] = { N_("Text"), "HTML"
117 #ifdef HAVE_SYSLOG_H
118 , "syslog"
119 #endif
120 };
121
122 #define LOGMODE_TEXT N_("Log format")
123 #ifdef HAVE_SYSLOG_H
124 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
125   "\"text\" (default), \"html\", and \"syslog\" (special mode to send to " \
126   "syslog instead of file.")
127 #else
128 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
129   "\"text\" (default) and \"html\".")
130 #endif
131
132 vlc_module_begin ()
133     set_shortname( N_( "Logging" ) )
134     set_description( N_("File logging") )
135
136     set_category( CAT_ADVANCED )
137     set_subcategory( SUBCAT_ADVANCED_MISC )
138
139     add_file( "logfile", NULL, NULL,
140              N_("Log filename"), N_("Specify the log filename."), false )
141     add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
142                 false )
143         change_string_list( mode_list, mode_list_text, 0 )
144
145     add_obsolete_string( "rrd-file" )
146
147     set_capability( "interface", 0 )
148     set_callbacks( Open, Close )
149 vlc_module_end ()
150
151 /*****************************************************************************
152  * Open: initialize and create stuff
153  *****************************************************************************/
154 static int Open( vlc_object_t *p_this )
155 {
156     intf_thread_t *p_intf = (intf_thread_t *)p_this;
157     intf_sys_t *p_sys;
158     char *psz_mode;
159
160     CONSOLE_INTRO_MSG;
161     msg_Info( p_intf, "using logger..." );
162
163     /* Allocate instance and initialize some members */
164     p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
165     if( p_sys == NULL )
166         return VLC_ENOMEM;
167
168     p_sys->msg.p_intf = p_intf;
169     p_sys->msg.i_mode = MODE_TEXT;
170     psz_mode = var_CreateGetString( p_intf, "logmode" );
171     if( psz_mode )
172     {
173         if( !strcmp( psz_mode, "text" ) )
174             ;
175         else if( !strcmp( psz_mode, "html" ) )
176         {
177             p_sys->msg.i_mode = MODE_HTML;
178         }
179 #ifdef HAVE_SYSLOG_H
180         else if( !strcmp( psz_mode, "syslog" ) )
181         {
182             p_sys->msg.i_mode = MODE_SYSLOG;
183         }
184 #endif
185         else
186         {
187             msg_Warn( p_intf, "invalid log mode `%s', using `text'", psz_mode );
188             p_sys->msg.i_mode = MODE_TEXT;
189         }
190         free( psz_mode );
191     }
192     else
193     {
194         msg_Warn( p_intf, "no log mode specified, using `text'" );
195     }
196
197     if( p_sys->msg.i_mode != MODE_SYSLOG )
198     {
199         char *psz_file = config_GetPsz( p_intf, "logfile" );
200         if( !psz_file )
201         {
202 #ifdef __APPLE__
203             if( asprintf( &psz_file, "%s/"LOG_DIR"/%s", config_GetHomeDir(),
204                 (p_sys->msg.i_mode == MODE_HTML) ? LOG_FILE_HTML
205                                              : LOG_FILE_TEXT ) == -1 )
206                 psz_file = NULL;
207 #else
208             switch( p_sys->msg.i_mode )
209             {
210             case MODE_HTML:
211                 psz_file = strdup( LOG_FILE_HTML );
212                 break;
213             case MODE_TEXT:
214             default:
215                 psz_file = strdup( LOG_FILE_TEXT );
216                 break;
217             }
218 #endif
219             msg_Warn( p_intf, "no log filename provided, using `%s'",
220                                psz_file );
221         }
222
223         /* Open the log file and remove any buffering for the stream */
224         msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
225         p_sys->msg.p_file = utf8_fopen( psz_file, "at" );
226         if( p_sys->msg.p_file == NULL )
227         {
228             msg_Err( p_intf, "error opening logfile `%s'", psz_file );
229             free( p_sys );
230             free( psz_file );
231             return -1;
232         }
233         setvbuf( p_sys->msg.p_file, NULL, _IONBF, 0 );
234
235         free( psz_file );
236
237         switch( p_sys->msg.i_mode )
238         {
239         case MODE_HTML:
240             fputs( HTML_HEADER, p_sys->msg.p_file );
241             break;
242         case MODE_TEXT:
243         default:
244             fputs( TEXT_HEADER, p_sys->msg.p_file );
245             break;
246         }
247
248     }
249     else
250     {
251         p_sys->msg.p_file = NULL;
252 #ifdef HAVE_SYSLOG_H
253         openlog( "vlc", LOG_PID|LOG_NDELAY, LOG_DAEMON );
254 #endif
255     }
256
257     p_sys->p_sub = msg_Subscribe( p_intf->p_libvlc, Overflow, &p_sys->msg );
258
259     return 0;
260 }
261
262 /*****************************************************************************
263  * Close: destroy interface stuff
264  *****************************************************************************/
265 static void Close( vlc_object_t *p_this )
266 {
267     intf_thread_t *p_intf = (intf_thread_t *)p_this;
268     intf_sys_t *p_sys = p_intf->p_sys;
269
270     /* Flush the queue and unsubscribe from the message queue */
271     /* FIXME: flush */
272     msg_Unsubscribe( p_sys->p_sub );
273
274     switch( p_sys->msg.i_mode )
275     {
276     case MODE_HTML:
277         fputs( HTML_FOOTER, p_sys->msg.p_file );
278         break;
279 #ifdef HAVE_SYSLOG_H
280     case MODE_SYSLOG:
281         closelog();
282         break;
283 #endif
284     case MODE_TEXT:
285     default:
286         fputs( TEXT_FOOTER, p_sys->msg.p_file );
287         break;
288     }
289
290     /* Close the log file */
291     if( p_sys->msg.p_file )
292         fclose( p_sys->msg.p_file );
293
294     /* Destroy structure */
295     free( p_sys );
296 }
297
298 /**
299  * Log a message
300  */
301 static void Overflow (msg_cb_data_t *p_sys, msg_item_t *p_item, unsigned overruns)
302 {
303     int verbosity = var_CreateGetInteger( p_sys->p_intf, "verbose" );
304     int priority = 0;
305
306     switch( p_item->i_type )
307     {
308         case VLC_MSG_WARN: priority = 1; break;
309         case VLC_MSG_DBG:  priority = 2; break;
310     }
311     if (verbosity < priority)
312         return;
313
314     switch( p_sys->i_mode )
315     {
316         case MODE_HTML:
317             HtmlPrint( p_item, p_sys->p_file );
318             break;
319 #ifdef HAVE_SYSLOG_H
320         case MODE_SYSLOG:
321             SyslogPrint( p_item );
322             break;
323 #endif
324         case MODE_TEXT:
325         default:
326             TextPrint( p_item, p_sys->p_file );
327             break;
328     }
329 }
330
331 static const char ppsz_type[4][11] = {
332     ": ",
333     " error: ",
334     " warning: ",
335     " debug: ",
336 };
337
338 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
339 {
340     fprintf( p_file, "%s%s%s\n", p_msg->psz_module, ppsz_type[p_msg->i_type],
341              p_msg->psz_msg );
342 }
343
344 #ifdef HAVE_SYSLOG_H
345 static void SyslogPrint( const msg_item_t *p_msg )
346 {
347     static const int i_prio[4] = { LOG_INFO, LOG_ERR, LOG_WARNING, LOG_DEBUG };
348     int i_priority = i_prio[p_msg->i_type];
349
350     if( p_msg->psz_header )
351         syslog( i_priority, "%s%s %s: %s", p_msg->psz_header,
352                 ppsz_type[p_msg->i_type],
353                 p_msg->psz_module, p_msg->psz_msg );
354     else
355         syslog( i_priority, "%s%s: %s", p_msg->psz_module, 
356                 ppsz_type[p_msg->i_type], p_msg->psz_msg );
357  
358 }
359 #endif
360
361 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
362 {
363     static const char ppsz_color[4][30] = {
364         "<span style=\"color: #ffffff\">",
365         "<span style=\"color: #ff6666\">",
366         "<span style=\"color: #ffff66\">",
367         "<span style=\"color: #aaaaaa\">",
368     };
369
370     fprintf( p_file, "%s%s%s%s</span>\n", p_msg->psz_module,
371              ppsz_type[p_msg->i_type], ppsz_color[p_msg->i_type],
372              p_msg->psz_msg );
373 }