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 static const char *const facility_list[] = { "daemon", "user", "local0",
134 "local1", "local2", "local3", "local4", "local5", "local6", "local7" };
136 #define SYSLOG_FACILITY_TEXT N_("Syslog facility")
137 #define SYSLOG_FACILITY_LONGTEXT N_("Select the syslog facility where logs " \
138 "will be forwarded. Available choices are \"daemon\" (default), \"user\", " \
139 "and \"local0\" through \"local7\".")
143 set_shortname( N_( "Logging" ) )
144 set_description( N_("File logging") )
146 set_category( CAT_ADVANCED )
147 set_subcategory( SUBCAT_ADVANCED_MISC )
149 add_file( "logfile", NULL, NULL,
150 N_("Log filename"), N_("Specify the log filename."), false )
151 add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
153 change_string_list( mode_list, mode_list_text, 0 )
155 add_string( "syslog-facility", "daemon", NULL, SYSLOG_FACILITY_TEXT,
156 SYSLOG_FACILITY_LONGTEXT, true )
157 change_string_list( facility_list, facility_list, 0 )
160 add_obsolete_string( "rrd-file" )
162 set_capability( "interface", 0 )
163 set_callbacks( Open, Close )
166 /*****************************************************************************
167 * Open: initialize and create stuff
168 *****************************************************************************/
169 static int Open( vlc_object_t *p_this )
171 intf_thread_t *p_intf = (intf_thread_t *)p_this;
176 msg_Info( p_intf, "using logger..." );
178 /* Allocate instance and initialize some members */
179 p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
183 p_sys->msg.p_intf = p_intf;
184 p_sys->msg.i_mode = MODE_TEXT;
185 psz_mode = var_CreateGetString( p_intf, "logmode" );
188 if( !strcmp( psz_mode, "text" ) )
190 else if( !strcmp( psz_mode, "html" ) )
192 p_sys->msg.i_mode = MODE_HTML;
195 else if( !strcmp( psz_mode, "syslog" ) )
197 p_sys->msg.i_mode = MODE_SYSLOG;
202 msg_Warn( p_intf, "invalid log mode `%s', using `text'", psz_mode );
203 p_sys->msg.i_mode = MODE_TEXT;
209 msg_Warn( p_intf, "no log mode specified, using `text'" );
212 if( p_sys->msg.i_mode != MODE_SYSLOG )
214 char *psz_file = config_GetPsz( p_intf, "logfile" );
218 if( asprintf( &psz_file, "%s/"LOG_DIR"/%s", config_GetHomeDir(),
219 (p_sys->msg.i_mode == MODE_HTML) ? LOG_FILE_HTML
220 : LOG_FILE_TEXT ) == -1 )
223 switch( p_sys->msg.i_mode )
226 psz_file = strdup( LOG_FILE_HTML );
230 psz_file = strdup( LOG_FILE_TEXT );
234 msg_Warn( p_intf, "no log filename provided, using `%s'",
238 /* Open the log file and remove any buffering for the stream */
239 msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
240 p_sys->msg.p_file = utf8_fopen( psz_file, "at" );
241 if( p_sys->msg.p_file == NULL )
243 msg_Err( p_intf, "error opening logfile `%s'", psz_file );
248 setvbuf( p_sys->msg.p_file, NULL, _IONBF, 0 );
252 switch( p_sys->msg.i_mode )
255 fputs( HTML_HEADER, p_sys->msg.p_file );
259 fputs( TEXT_HEADER, p_sys->msg.p_file );
266 p_sys->msg.p_file = NULL;
268 int i_facility = LOG_DAEMON;
269 char *psz_facility = var_CreateGetString( p_intf, "syslog-facility" );
273 static const struct { const char psz_name[7]; int i_value; }
275 { "daemon", LOG_DAEMON }, { "user", LOG_USER },
276 { "local0", LOG_LOCAL0 }, { "local1", LOG_LOCAL1 },
277 { "local2", LOG_LOCAL2 }, { "local3", LOG_LOCAL3 },
278 { "local4", LOG_LOCAL4 }, { "local5", LOG_LOCAL5 },
279 { "local6", LOG_LOCAL6 }, { "local7", LOG_LOCAL7 }
282 i < sizeof( p_facility ) / sizeof( p_facility[0] );
285 if( !strcmp( psz_facility, p_facility[i].psz_name ) )
287 i_facility = p_facility[i].i_value;
294 msg_Warn( p_intf, "invalid syslog facility `%s', using `daemon'", psz_facility );
296 free( psz_facility );
300 msg_Warn( p_intf, "no syslog facility specified, using `daemon'" );
303 openlog( "vlc", LOG_PID|LOG_NDELAY, i_facility );
307 p_sys->p_sub = msg_Subscribe( p_intf->p_libvlc, Overflow, &p_sys->msg );
312 /*****************************************************************************
313 * Close: destroy interface stuff
314 *****************************************************************************/
315 static void Close( vlc_object_t *p_this )
317 intf_thread_t *p_intf = (intf_thread_t *)p_this;
318 intf_sys_t *p_sys = p_intf->p_sys;
320 /* Flush the queue and unsubscribe from the message queue */
322 msg_Unsubscribe( p_sys->p_sub );
324 switch( p_sys->msg.i_mode )
327 fputs( HTML_FOOTER, p_sys->msg.p_file );
336 fputs( TEXT_FOOTER, p_sys->msg.p_file );
340 /* Close the log file */
341 if( p_sys->msg.p_file )
342 fclose( p_sys->msg.p_file );
344 /* Destroy structure */
351 static void Overflow (msg_cb_data_t *p_sys, msg_item_t *p_item, unsigned overruns)
353 int verbosity = var_CreateGetInteger( p_sys->p_intf, "verbose" );
356 switch( p_item->i_type )
358 case VLC_MSG_WARN: priority = 1; break;
359 case VLC_MSG_DBG: priority = 2; break;
361 if (verbosity < priority)
364 switch( p_sys->i_mode )
367 HtmlPrint( p_item, p_sys->p_file );
371 SyslogPrint( p_item );
376 TextPrint( p_item, p_sys->p_file );
381 static const char ppsz_type[4][11] = {
388 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
390 fprintf( p_file, "%s%s%s\n", p_msg->psz_module, ppsz_type[p_msg->i_type],
395 static void SyslogPrint( const msg_item_t *p_msg )
397 static const int i_prio[4] = { LOG_INFO, LOG_ERR, LOG_WARNING, LOG_DEBUG };
398 int i_priority = i_prio[p_msg->i_type];
400 if( p_msg->psz_header )
401 syslog( i_priority, "%s %s%s%s", p_msg->psz_header, p_msg->psz_module,
402 ppsz_type[p_msg->i_type], p_msg->psz_msg );
404 syslog( i_priority, "%s%s%s", p_msg->psz_module,
405 ppsz_type[p_msg->i_type], p_msg->psz_msg );
410 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
412 static const char ppsz_color[4][30] = {
413 "<span style=\"color: #ffffff\">",
414 "<span style=\"color: #ff6666\">",
415 "<span style=\"color: #ffff66\">",
416 "<span style=\"color: #aaaaaa\">",
419 fprintf( p_file, "%s%s%s%s</span>\n", p_msg->psz_module,
420 ppsz_type[p_msg->i_type], ppsz_color[p_msg->i_type],