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 along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 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>
36 #include <vlc_charset.h>
46 #define LOG_DIR "Library/Logs/"
49 #define LOG_FILE_TEXT "vlc-log.txt"
50 #define LOG_FILE_HTML "vlc-log.html"
52 #define TEXT_HEADER "\xEF\xBB\xBF-- logger module started --\n"
53 #define TEXT_FOOTER "-- logger module stopped --\n"
56 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
57 " \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
60 " <title>vlc log</title>\n" \
61 " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" \
63 " <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
65 " <strong>-- logger module started --</strong>\n"
67 " <strong>-- logger module stopped --</strong>\n" \
76 /*****************************************************************************
77 * intf_sys_t: description and status of log interface
78 *****************************************************************************/
81 msg_subscription_t *p_sub;
86 /*****************************************************************************
88 *****************************************************************************/
89 static int Open ( vlc_object_t * );
90 static void Close ( vlc_object_t * );
92 static void TextPrint(void *, int, const msg_item_t *, const char *, va_list);
93 static void HtmlPrint(void *, int, const msg_item_t *, const char *, va_list);
95 static void SyslogPrint(void *, int, const msg_item_t *, const char *,
99 /*****************************************************************************
101 *****************************************************************************/
102 static const char *const mode_list[] = { "text", "html"
107 static const char *const mode_list_text[] = { N_("Text"), "HTML"
113 #define LOGMODE_TEXT N_("Log format")
114 #ifndef HAVE_SYSLOG_H
115 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
116 "\"text\" (default) and \"html\".")
119 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
120 "\"text\" (default), \"html\", and \"syslog\" (special mode to send to " \
121 "syslog instead of file.")
123 #define SYSLOG_FACILITY_TEXT N_("Syslog facility")
124 #define SYSLOG_FACILITY_LONGTEXT N_("Select the syslog facility where logs " \
125 "will be forwarded. Available choices are \"user\" (default), \"daemon\", " \
126 "and \"local0\" through \"local7\".")
128 /* First in list is the default facility used. */
129 #define DEFINE_SYSLOG_FACILITY \
130 DEF( "user", LOG_USER ), \
131 DEF( "daemon", LOG_DAEMON ), \
132 DEF( "local0", LOG_LOCAL0 ), \
133 DEF( "local1", LOG_LOCAL1 ), \
134 DEF( "local2", LOG_LOCAL2 ), \
135 DEF( "local3", LOG_LOCAL3 ), \
136 DEF( "local4", LOG_LOCAL4 ), \
137 DEF( "local5", LOG_LOCAL5 ), \
138 DEF( "local6", LOG_LOCAL6 ), \
139 DEF( "local7", LOG_LOCAL7 )
141 #define DEF( a, b ) a
142 static const char *const fac_name[] = { DEFINE_SYSLOG_FACILITY };
144 #define DEF( a, b ) b
145 static const int fac_number[] = { DEFINE_SYSLOG_FACILITY };
147 enum { fac_entries = sizeof(fac_name)/sizeof(fac_name[0]) };
148 #undef DEFINE_SYSLOG_FACILITY
152 #define LOGVERBOSE_TEXT N_("Verbosity")
153 #define LOGVERBOSE_LONGTEXT N_("Select the verbosity to use for log or -1 to " \
154 "use the same verbosity given by --verbose.")
157 set_shortname( N_( "Logging" ) )
158 set_description( N_("File logging") )
160 set_category( CAT_ADVANCED )
161 set_subcategory( SUBCAT_ADVANCED_MISC )
163 add_savefile( "logfile", NULL,
164 N_("Log filename"), N_("Specify the log filename."), false )
165 add_string( "logmode", "text", LOGMODE_TEXT, LOGMODE_LONGTEXT,
167 change_string_list( mode_list, mode_list_text, 0 )
169 add_string( "syslog-facility", fac_name[0], SYSLOG_FACILITY_TEXT,
170 SYSLOG_FACILITY_LONGTEXT, true )
171 change_string_list( fac_name, fac_name, 0 )
173 add_integer( "log-verbose", -1, LOGVERBOSE_TEXT, LOGVERBOSE_LONGTEXT,
176 add_obsolete_string( "rrd-file" )
178 set_capability( "interface", 0 )
179 set_callbacks( Open, Close )
182 /*****************************************************************************
183 * Open: initialize and create stuff
184 *****************************************************************************/
185 static int Open( vlc_object_t *p_this )
187 intf_thread_t *p_intf = (intf_thread_t *)p_this;
191 msg_Info( p_intf, "using logger." );
193 /* Allocate instance and initialize some members */
194 p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
198 msg_callback_t cb = TextPrint;
199 const char *filename = LOG_FILE_TEXT, *header = TEXT_HEADER;
200 p_sys->footer = TEXT_FOOTER;
202 char *mode = var_InheritString( p_intf, "logmode" );
205 if( !strcmp( mode, "html" ) )
207 p_sys->footer = HTML_FOOTER;
208 header = HTML_HEADER;
212 else if( !strcmp( mode, "syslog" ) )
215 else if( strcmp( mode, "text" ) )
216 msg_Warn( p_intf, "invalid log mode `%s', using `text'", mode );
221 if( cb == SyslogPrint )
224 char *psz_facility = var_InheritString( p_intf, "syslog-facility" );
228 for( size_t i = 0; i < fac_entries; ++i )
230 if( !strcmp( psz_facility, fac_name[i] ) )
232 i_facility = fac_number[i];
239 msg_Warn( p_intf, "invalid syslog facility `%s', using `%s'",
240 psz_facility, fac_name[0] );
241 i_facility = fac_number[0];
243 free( psz_facility );
247 msg_Warn( p_intf, "no syslog facility specified, using `%s'",
249 i_facility = fac_number[0];
252 openlog( "vlc", LOG_PID|LOG_NDELAY, i_facility );
253 p_sys->p_file = NULL;
258 char *psz_file = var_InheritString( p_intf, "logfile" );
262 char *home = config_GetUserDir(VLC_DOCUMENTS_DIR);
264 || asprintf( &psz_file, "%s/"LOG_DIR"/%s", home,
270 msg_Warn( p_intf, "no log filename provided, using `%s'",
276 /* Open the log file and remove any buffering for the stream */
277 msg_Dbg( p_intf, "opening logfile `%s'", filename );
278 p_sys->p_file = vlc_fopen( filename, "at" );
280 if( p_sys->p_file == NULL )
282 msg_Err( p_intf, "error opening logfile `%s': %m", filename );
286 setvbuf( p_sys->p_file, NULL, _IONBF, 0 );
287 fputs( header, p_sys->p_file );
290 p_sys->p_sub = vlc_Subscribe( cb, p_intf );
294 /*****************************************************************************
295 * Close: destroy interface stuff
296 *****************************************************************************/
297 static void Close( vlc_object_t *p_this )
299 intf_thread_t *p_intf = (intf_thread_t *)p_this;
300 intf_sys_t *p_sys = p_intf->p_sys;
302 /* Flush the queue and unsubscribe from the message queue */
303 vlc_Unsubscribe( p_sys->p_sub );
305 /* Close the log file */
307 if( p_sys->p_file == NULL )
312 fputs( p_sys->footer, p_sys->p_file );
313 fclose( p_sys->p_file );
316 /* Destroy structure */
320 static bool IgnoreMessage( intf_thread_t *p_intf, int type )
322 /* TODO: cache value... */
323 int verbosity = var_InheritInteger( p_intf, "log-verbose" );
325 verbosity = var_InheritInteger( p_intf, "verbose" );
327 return verbosity < 0 || verbosity < (type - VLC_MSG_ERR);
334 static const char ppsz_type[4][9] = {
341 static void TextPrint( void *opaque, int type, const msg_item_t *item,
342 const char *fmt, va_list ap )
344 intf_thread_t *p_intf = opaque;
345 FILE *stream = p_intf->p_sys->p_file;
347 if( IgnoreMessage( p_intf, type ) )
350 int canc = vlc_savecancel();
352 fprintf( stream, "%s%s: ", item->psz_module, ppsz_type[type] );
353 vfprintf( stream, fmt, ap );
354 putc_unlocked( '\n', stream );
355 funlockfile( stream );
356 vlc_restorecancel( canc );
360 static void SyslogPrint( void *opaque, int type, const msg_item_t *item,
361 const char *fmt, va_list ap )
363 static const int i_prio[4] = { LOG_INFO, LOG_ERR, LOG_WARNING, LOG_DEBUG };
365 intf_thread_t *p_intf = opaque;
367 int i_priority = i_prio[type];
369 if( IgnoreMessage( p_intf, type )
370 || unlikely(vasprintf( &str, fmt, ap ) == -1) )
373 int canc = vlc_savecancel();
374 if( item->psz_header != NULL )
375 syslog( i_priority, "[%s] %s%s: %s", item->psz_header,
376 item->psz_module, ppsz_type[type], str );
378 syslog( i_priority, "%s%s: %s",
379 item->psz_module, ppsz_type[type], str );
380 vlc_restorecancel( canc );
385 static void HtmlPrint( void *opaque, int type, const msg_item_t *item,
386 const char *fmt, va_list ap )
388 static const unsigned color[4] = {
389 0xffffff, 0xff6666, 0xffff66, 0xaaaaaa,
392 intf_thread_t *p_intf = opaque;
393 FILE *stream = p_intf->p_sys->p_file;
395 if( IgnoreMessage( p_intf, type ) )
398 int canc = vlc_savecancel();
400 fprintf( stream, "%s%s: <span style=\"color: #%06x\">",
401 item->psz_module, ppsz_type[type], color[type] );
402 /* FIXME: encode special ASCII characters */
403 fprintf( stream, fmt, ap );
404 fputs( "</span>\n", stream );
405 funlockfile( stream );
406 vlc_restorecancel( canc );