1 /*****************************************************************************
2 * logger.c : file logging plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2008 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
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>
41 # define _IONBF 0x0004
49 #define LOG_DIR "Library/Logs/"
52 #define LOG_FILE_TEXT "vlc-log.txt"
53 #define LOG_FILE_HTML "vlc-log.html"
55 #define TEXT_HEADER "-- logger module started --\n"
56 #define TEXT_FOOTER "-- logger module stopped --\n"
59 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
60 " \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
63 " <title>vlc log</title>\n" \
64 " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" \
66 " <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
68 " <b>-- logger module started --</b>\n"
70 " <b>-- logger module stopped --</b>\n" \
81 intf_thread_t *p_intf;
86 /*****************************************************************************
87 * intf_sys_t: description and status of log interface
88 *****************************************************************************/
91 msg_subscription_t *p_sub;
95 /*****************************************************************************
97 *****************************************************************************/
98 static int Open ( vlc_object_t * );
99 static void Close ( vlc_object_t * );
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 * );
105 static void SyslogPrint ( const msg_item_t *);
108 /*****************************************************************************
110 *****************************************************************************/
111 static const char *const mode_list[] = { "text", "html"
116 static const char *const mode_list_text[] = { N_("Text"), "HTML"
122 #define LOGMODE_TEXT N_("Log format")
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.")
128 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
129 "\"text\" (default) and \"html\".")
133 set_shortname( N_( "Logging" ) )
134 set_description( N_("File logging") )
136 set_category( CAT_ADVANCED )
137 set_subcategory( SUBCAT_ADVANCED_MISC )
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,
143 change_string_list( mode_list, mode_list_text, 0 )
145 add_obsolete_string( "rrd-file" )
147 set_capability( "interface", 0 )
148 set_callbacks( Open, Close )
151 /*****************************************************************************
152 * Open: initialize and create stuff
153 *****************************************************************************/
154 static int Open( vlc_object_t *p_this )
156 intf_thread_t *p_intf = (intf_thread_t *)p_this;
161 msg_Info( p_intf, "using logger..." );
163 /* Allocate instance and initialize some members */
164 p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
168 p_sys->msg.p_intf = p_intf;
169 p_sys->msg.i_mode = MODE_TEXT;
170 psz_mode = var_CreateGetString( p_intf, "logmode" );
173 if( !strcmp( psz_mode, "text" ) )
175 else if( !strcmp( psz_mode, "html" ) )
177 p_sys->msg.i_mode = MODE_HTML;
180 else if( !strcmp( psz_mode, "syslog" ) )
182 p_sys->msg.i_mode = MODE_SYSLOG;
187 msg_Warn( p_intf, "invalid log mode `%s', using `text'", psz_mode );
188 p_sys->msg.i_mode = MODE_TEXT;
194 msg_Warn( p_intf, "no log mode specified, using `text'" );
197 if( p_sys->msg.i_mode != MODE_SYSLOG )
199 char *psz_file = config_GetPsz( p_intf, "logfile" );
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 )
208 switch( p_sys->msg.i_mode )
211 psz_file = strdup( LOG_FILE_HTML );
215 psz_file = strdup( LOG_FILE_TEXT );
219 msg_Warn( p_intf, "no log filename provided, using `%s'",
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 )
228 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
233 setvbuf( p_sys->msg.p_file, NULL, _IONBF, 0 );
237 switch( p_sys->msg.i_mode )
240 fputs( HTML_HEADER, p_sys->msg.p_file );
244 fputs( TEXT_HEADER, p_sys->msg.p_file );
251 p_sys->msg.p_file = NULL;
253 openlog( "vlc", LOG_PID|LOG_NDELAY, LOG_DAEMON );
257 p_sys->p_sub = msg_Subscribe( p_intf->p_libvlc, Overflow, &p_sys->msg );
262 /*****************************************************************************
263 * Close: destroy interface stuff
264 *****************************************************************************/
265 static void Close( vlc_object_t *p_this )
267 intf_thread_t *p_intf = (intf_thread_t *)p_this;
268 intf_sys_t *p_sys = p_intf->p_sys;
270 /* Flush the queue and unsubscribe from the message queue */
272 msg_Unsubscribe( p_sys->p_sub );
274 switch( p_sys->msg.i_mode )
277 fputs( HTML_FOOTER, p_sys->msg.p_file );
286 fputs( TEXT_FOOTER, p_sys->msg.p_file );
290 /* Close the log file */
291 if( p_sys->msg.p_file )
292 fclose( p_sys->msg.p_file );
294 /* Destroy structure */
301 static void Overflow (msg_cb_data_t *p_sys, msg_item_t *p_item, unsigned overruns)
303 int verbosity = var_CreateGetInteger( p_sys->p_intf, "verbose" );
306 switch( p_item->i_type )
308 case VLC_MSG_WARN: priority = 1; break;
309 case VLC_MSG_DBG: priority = 2; break;
311 if (verbosity < priority)
314 switch( p_sys->i_mode )
317 HtmlPrint( p_item, p_sys->p_file );
321 SyslogPrint( p_item );
326 TextPrint( p_item, p_sys->p_file );
331 static const char ppsz_type[4][11] = {
338 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
340 fprintf( p_file, "%s%s%s\n", p_msg->psz_module, ppsz_type[p_msg->i_type],
345 static void SyslogPrint( const msg_item_t *p_msg )
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];
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 );
355 syslog( i_priority, "%s%s: %s", p_msg->psz_module,
356 ppsz_type[p_msg->i_type], p_msg->psz_msg );
361 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
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\">",
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],