]> git.sesse.net Git - vlc/blob - modules/misc/logger.c
opengl: Handle pixel pitch properly when repacking textures for upload
[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 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  *****************************************************************************/
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_fs.h>
36 #include <vlc_charset.h>
37
38 #include <stdarg.h>
39 #include <assert.h>
40
41 #ifdef __ANDROID__
42 # include <android/log.h>
43 #endif
44
45 #define LOG_FILE_TEXT "vlc-log.txt"
46 #define LOG_FILE_HTML "vlc-log.html"
47
48 #define TEXT_HEADER "\xEF\xBB\xBF-- logger module started --\n"
49 #define TEXT_FOOTER "-- logger module stopped --\n"
50
51 #define HTML_HEADER \
52     "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
53     "  \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
54     "<html>\n" \
55     "  <head>\n" \
56     "    <title>vlc log</title>\n" \
57     "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" \
58     "  </head>\n" \
59     "  <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
60     "    <pre>\n" \
61     "      <strong>-- logger module started --</strong>\n"
62 #define HTML_FOOTER \
63     "      <strong>-- logger module stopped --</strong>\n" \
64     "    </pre>\n" \
65     "  </body>\n" \
66     "</html>\n"
67
68 #ifdef HAVE_SYSLOG_H
69 #include <syslog.h>
70 #endif
71
72 /*****************************************************************************
73  * intf_sys_t: description and status of log interface
74  *****************************************************************************/
75 struct intf_sys_t
76 {
77     FILE *p_file;
78     const char *footer;
79 };
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 static int  Open    ( vlc_object_t * );
85 static void Close   ( vlc_object_t * );
86
87 static void TextPrint(void *, int, const vlc_log_t *, const char *, va_list);
88 static void HtmlPrint(void *, int, const vlc_log_t *, const char *, va_list);
89 #ifdef HAVE_SYSLOG_H
90 static void SyslogPrint(void *, int, const vlc_log_t *, const char *, va_list);
91 #endif
92 #ifdef __ANDROID__
93 static void AndroidPrint(void *, int, const vlc_log_t *, const char *, va_list);
94 #endif
95
96 /*****************************************************************************
97  * Module descriptor
98  *****************************************************************************/
99 static const char *const mode_list[] = { "text", "html"
100 #ifdef HAVE_SYSLOG_H
101 ,"syslog"
102 #endif
103 #ifdef __ANDROID__
104 ,"android"
105 #endif
106 };
107 static const char *const mode_list_text[] = { N_("Text"), "HTML"
108 #ifdef HAVE_SYSLOG_H
109 , "syslog"
110 #endif
111 #ifdef __ANDROID__
112 ,"android"
113 #endif
114 };
115
116 #define LOGMODE_TEXT N_("Log format")
117 #define LOGMODE_LONGTEXT N_("Specify the logging format.")
118
119 #ifdef HAVE_SYSLOG_H
120 #define SYSLOG_FACILITY_TEXT N_("Syslog facility")
121 #define SYSLOG_FACILITY_LONGTEXT N_("Select the syslog facility where logs " \
122   "will be forwarded.")
123
124 /* First in list is the default facility used. */
125 #define DEFINE_SYSLOG_FACILITY \
126   DEF( "user",   LOG_USER ), \
127   DEF( "daemon", LOG_DAEMON ), \
128   DEF( "local0", LOG_LOCAL0 ), \
129   DEF( "local1", LOG_LOCAL1 ), \
130   DEF( "local2", LOG_LOCAL2 ), \
131   DEF( "local3", LOG_LOCAL3 ), \
132   DEF( "local4", LOG_LOCAL4 ), \
133   DEF( "local5", LOG_LOCAL5 ), \
134   DEF( "local6", LOG_LOCAL6 ), \
135   DEF( "local7", LOG_LOCAL7 )
136
137 #define DEF( a, b ) a
138 static const char *const fac_name[]   = { DEFINE_SYSLOG_FACILITY };
139 #undef  DEF
140 #define DEF( a, b ) b
141 static const int         fac_number[] = { DEFINE_SYSLOG_FACILITY };
142 #undef  DEF
143 enum                   { fac_entries = sizeof(fac_name)/sizeof(fac_name[0]) };
144 #undef  DEFINE_SYSLOG_FACILITY
145
146 #endif
147
148 #define LOGVERBOSE_TEXT N_("Verbosity")
149 #define LOGVERBOSE_LONGTEXT N_("Select the verbosity to use for log or -1 to " \
150 "use the same verbosity given by --verbose.")
151
152 vlc_module_begin ()
153     set_shortname( N_( "Logging" ) )
154     set_description( N_("File logging") )
155
156     set_category( CAT_ADVANCED )
157     set_subcategory( SUBCAT_ADVANCED_MISC )
158
159     add_savefile( "logfile", NULL,
160              N_("Log filename"), N_("Specify the log filename."), false )
161     add_string( "logmode", "text", LOGMODE_TEXT, LOGMODE_LONGTEXT,
162                 false )
163         change_string_list( mode_list, mode_list_text )
164 #ifdef HAVE_SYSLOG_H
165     add_string( "syslog-facility", fac_name[0], SYSLOG_FACILITY_TEXT,
166                 SYSLOG_FACILITY_LONGTEXT, true )
167         change_string_list( fac_name, fac_name )
168 #endif
169     add_integer( "log-verbose", -1, LOGVERBOSE_TEXT, LOGVERBOSE_LONGTEXT,
170            false )
171     
172     add_obsolete_string( "rrd-file" )
173
174     set_capability( "interface", 0 )
175     set_callbacks( Open, Close )
176 vlc_module_end ()
177
178 /*****************************************************************************
179  * Open: initialize and create stuff
180  *****************************************************************************/
181 static int Open( vlc_object_t *p_this )
182 {
183     intf_thread_t *p_intf = (intf_thread_t *)p_this;
184     intf_sys_t *p_sys;
185
186     CONSOLE_INTRO_MSG;
187     msg_Info( p_intf, "using logger." );
188
189     /* Allocate instance and initialize some members */
190     p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
191     if( p_sys == NULL )
192         return VLC_ENOMEM;
193
194     p_sys->p_file = NULL;
195     vlc_log_cb cb = TextPrint;
196     const char *filename = LOG_FILE_TEXT, *header = TEXT_HEADER;
197     p_sys->footer = TEXT_FOOTER;
198
199     char *mode = var_InheritString( p_intf, "logmode" );
200     if( mode != NULL )
201     {
202         if( !strcmp( mode, "html" ) )
203         {
204             p_sys->footer = HTML_FOOTER;
205             header = HTML_HEADER;
206             cb = HtmlPrint;
207         }
208 #ifdef HAVE_SYSLOG_H
209         else if( !strcmp( mode, "syslog" ) )
210             cb = SyslogPrint;
211 #endif
212 #ifdef __ANDROID__
213         else if( !strcmp( mode, "android" ) )
214             cb = AndroidPrint;
215 #endif
216         else if( strcmp( mode, "text" ) )
217             msg_Warn( p_intf, "invalid log mode `%s', using `text'", mode );
218         free( mode );
219     }
220
221 #ifdef HAVE_SYSLOG_H
222     if( cb == SyslogPrint )
223     {
224         int i_facility;
225         char *psz_facility = var_InheritString( p_intf, "syslog-facility" );
226         if( psz_facility )
227         {
228             bool b_valid = 0;
229             for( size_t i = 0; i < fac_entries; ++i )
230             {
231                 if( !strcmp( psz_facility, fac_name[i] ) )
232                 {
233                     i_facility = fac_number[i];
234                     b_valid = 1;
235                     break;
236                 }
237             }
238             if( !b_valid )
239             {
240                 msg_Warn( p_intf, "invalid syslog facility `%s', using `%s'",
241                           psz_facility, fac_name[0] );
242                 i_facility = fac_number[0];
243             }
244             free( psz_facility );
245         }
246         else
247         {
248             msg_Warn( p_intf, "no syslog facility specified, using `%s'",
249                       fac_name[0] );
250             i_facility = fac_number[0];
251         }
252
253         openlog( "vlc", LOG_PID|LOG_NDELAY, i_facility );
254         p_sys->p_file = NULL;
255     }
256     else
257 #endif
258 #ifdef __ANDROID__
259     if( cb == AndroidPrint )
260     {
261         /* nothing to do */
262     }
263     else
264 #endif
265     {
266         char *psz_file = var_InheritString( p_intf, "logfile" );
267         if( !psz_file )
268         {
269 #ifdef __APPLE__
270 # define LOG_DIR "Library/Logs"
271             char *home = config_GetUserDir(VLC_HOME_DIR);
272             if( home == NULL
273              || asprintf( &psz_file, "%s/"LOG_DIR"/%s", home,
274                           filename ) == -1 )
275                 psz_file = NULL;
276             free(home);
277             filename = psz_file;
278 #endif
279             msg_Warn( p_intf, "no log filename provided, using `%s'",
280                       filename );
281         }
282         else
283             filename = psz_file;
284
285         /* Open the log file and remove any buffering for the stream */
286         msg_Dbg( p_intf, "opening logfile `%s'", filename );
287         p_sys->p_file = vlc_fopen( filename, "at" );
288         free( psz_file );
289         if( p_sys->p_file == NULL )
290         {
291             msg_Err( p_intf, "error opening logfile `%s': %m", filename );
292             free( p_sys );
293             return VLC_EGENERIC;
294         }
295         setvbuf( p_sys->p_file, NULL, _IONBF, 0 );
296         fputs( header, p_sys->p_file );
297     }
298
299     vlc_LogSet( p_intf->p_libvlc, cb, p_intf );
300     return VLC_SUCCESS;
301 }
302
303 /*****************************************************************************
304  * Close: destroy interface stuff
305  *****************************************************************************/
306 static void Close( vlc_object_t *p_this )
307 {
308     intf_thread_t *p_intf = (intf_thread_t *)p_this;
309     intf_sys_t *p_sys = p_intf->p_sys;
310
311     /* Flush the queue and unsubscribe from the message queue */
312     vlc_LogSet( p_intf->p_libvlc, NULL, NULL );
313
314     /* Close the log file */
315 #ifdef HAVE_SYSLOG_H
316     if( p_sys->p_file == NULL )
317         closelog();
318     else
319 #endif
320     if( p_sys->p_file )
321     {
322         fputs( p_sys->footer, p_sys->p_file );
323         fclose( p_sys->p_file );
324     }
325
326     /* Destroy structure */
327     free( p_sys );
328 }
329
330 static bool IgnoreMessage( intf_thread_t *p_intf, int type )
331 {
332     /* TODO: cache value... */
333     int verbosity = var_InheritInteger( p_intf, "log-verbose" );
334     if (verbosity == -1)
335         verbosity = var_InheritInteger( p_intf, "verbose" );
336
337     return verbosity < 0 || verbosity < (type - VLC_MSG_ERR);
338 }
339
340 /*
341  * Logging callbacks
342  */
343
344 static const char ppsz_type[4][9] = {
345     "",
346     " error",
347     " warning",
348     " debug",
349 };
350
351 #ifdef __ANDROID__
352 static const android_LogPriority prioritytype[4] = {
353     ANDROID_LOG_INFO,
354     ANDROID_LOG_ERROR,
355     ANDROID_LOG_WARN,
356     ANDROID_LOG_DEBUG
357 };
358
359 static void AndroidPrint( void *opaque, int type, const vlc_log_t *item,
360                        const char *fmt, va_list ap )
361 {
362     (void)item;
363     intf_thread_t *p_intf = opaque;
364
365     if( IgnoreMessage( p_intf, type ) )
366         return;
367
368     int canc = vlc_savecancel();
369     __android_log_vprint(prioritytype[type], "VLC", fmt, ap);
370     vlc_restorecancel( canc );
371 }
372 #endif
373
374 static void TextPrint( void *opaque, int type, const vlc_log_t *item,
375                        const char *fmt, va_list ap )
376 {
377     intf_thread_t *p_intf = opaque;
378     FILE *stream = p_intf->p_sys->p_file;
379
380     if( IgnoreMessage( p_intf, type ) )
381         return;
382
383     int canc = vlc_savecancel();
384     flockfile( stream );
385     fprintf( stream, "%s%s: ", item->psz_module, ppsz_type[type] );
386     vfprintf( stream, fmt, ap );
387     putc_unlocked( '\n', stream );
388     funlockfile( stream );
389     vlc_restorecancel( canc );
390 }
391
392 #ifdef HAVE_SYSLOG_H
393 static void SyslogPrint( void *opaque, int type, const vlc_log_t *item,
394                          const char *fmt, va_list ap )
395 {
396     static const int i_prio[4] = { LOG_INFO, LOG_ERR, LOG_WARNING, LOG_DEBUG };
397
398     intf_thread_t *p_intf = opaque;
399     char *str;
400     int i_priority = i_prio[type];
401
402     if( IgnoreMessage( p_intf, type )
403      || unlikely(vasprintf( &str, fmt, ap ) == -1) )
404         return;
405
406     int canc = vlc_savecancel();
407     if( item->psz_header != NULL )
408         syslog( i_priority, "[%s] %s%s: %s", item->psz_header,
409                 item->psz_module, ppsz_type[type], str );
410     else
411         syslog( i_priority, "%s%s: %s",
412                 item->psz_module, ppsz_type[type], str );
413     vlc_restorecancel( canc );
414     free( str );
415 }
416 #endif
417
418 static void HtmlPrint( void *opaque, int type, const vlc_log_t *item,
419                        const char *fmt, va_list ap )
420 {
421     static const unsigned color[4] = {
422         0xffffff, 0xff6666, 0xffff66, 0xaaaaaa,
423     };
424
425     intf_thread_t *p_intf = opaque;
426     FILE *stream = p_intf->p_sys->p_file;
427
428     if( IgnoreMessage( p_intf, type ) )
429         return;
430
431     int canc = vlc_savecancel();
432     flockfile( stream );
433     fprintf( stream, "%s%s: <span style=\"color: #%06x\">",
434              item->psz_module, ppsz_type[type], color[type] );
435     /* FIXME: encode special ASCII characters */
436     vfprintf( stream, fmt, ap );
437     fputs( "</span>\n", stream );
438     funlockfile( stream );
439     vlc_restorecancel( canc );
440 }