]> git.sesse.net Git - vlc/blob - modules/misc/logger.c
Remove _GNU_SOURCE and string.h too
[vlc] / modules / misc / logger.c
1 /*****************************************************************************
2  * logger.c : file logging plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 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 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdio.h>
30
31 #ifdef UNDER_CE
32 #   define _IONBF 0x0004
33 #endif
34
35 #include <vlc/vlc.h>
36 #include <vlc_interface.h>
37 #include <vlc_playlist.h>
38 #include <vlc_charset.h>
39
40 #define MODE_TEXT 0
41 #define MODE_HTML 1
42 #define MODE_SYSLOG 2
43
44 #ifdef __APPLE__
45 #define LOG_DIR "Library/Logs/"
46 #endif
47
48 #define LOG_FILE_TEXT "vlc-log.txt"
49 #define LOG_FILE_HTML "vlc-log.html"
50
51 #define LOG_STRING( msg, file ) fwrite( msg, strlen( msg ), 1, file );
52
53 #define TEXT_HEADER "-- logger module started --\n"
54 #define TEXT_FOOTER "-- logger module stopped --\n"
55
56 #define HTML_HEADER \
57     "<html>\n" \
58     "  <head>\n" \
59     "    <title>vlc log</title>\n" \
60     "  </head>\n" \
61     "  <body bgcolor=\"#000000\" text=\"#aaaaaa\">\n" \
62     "    <pre>\n" \
63     "      <b>-- logger module started --</b>\n"
64 #define HTML_FOOTER \
65     "      <b>-- logger module stopped --</b>\n" \
66     "    </pre>\n" \
67     "  </body>\n" \
68     "</html>\n"
69
70 #if HAVE_SYSLOG_H
71 #include <syslog.h>
72 #endif
73
74 /*****************************************************************************
75  * intf_sys_t: description and status of log interface
76  *****************************************************************************/
77 struct intf_sys_t
78 {
79     int i_mode;
80     FILE *p_rrd;
81     mtime_t last_update;
82
83     FILE *    p_file; /* The log file */
84     msg_subscription_t *p_sub;
85 };
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90 static int  Open    ( vlc_object_t * );
91 static void Close   ( vlc_object_t * );
92 static void Run     ( intf_thread_t * );
93
94 static void FlushQueue        ( msg_subscription_t *, FILE *, int );
95 static void TextPrint         ( const msg_item_t *, FILE * );
96 static void HtmlPrint         ( const msg_item_t *, FILE * );
97 #ifdef HAVE_SYSLOG_H
98 static void SyslogPrint       ( const msg_item_t *);
99 #endif
100
101 static void DoRRD( intf_thread_t *p_intf );
102
103 /*****************************************************************************
104  * Module descriptor
105  *****************************************************************************/
106 static const char *mode_list[] = { "text", "html"
107 #ifdef HAVE_SYSLOG_H
108 ,"syslog"
109 #endif
110 };
111 static const char *mode_list_text[] = { N_("Text"), "HTML"
112 #ifdef HAVE_SYSLOG_H
113 , "syslog"
114 #endif
115 };
116
117 #define LOGMODE_TEXT N_("Log format")
118 #ifdef HAVE_SYSLOG_H
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.")
122 #else
123 #define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
124   "\"text\" (default) and \"html\".")
125 #endif
126
127 vlc_module_begin();
128     set_shortname( _( "Logging" ) );
129     set_description( _("File logging") );
130
131     set_category( CAT_ADVANCED );
132     set_subcategory( SUBCAT_ADVANCED_MISC );
133
134     add_file( "logfile", NULL, NULL,
135              N_("Log filename"), N_("Specify the log filename."), VLC_FALSE );
136     add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
137                 VLC_FALSE );
138         change_string_list( mode_list, mode_list_text, 0 );
139
140     add_file( "rrd-file", NULL, NULL, N_("RRD output file") ,
141                     N_("Output data for RRDTool in this file." ), VLC_TRUE );
142
143     set_capability( "interface", 0 );
144     set_callbacks( Open, Close );
145 vlc_module_end();
146
147 /*****************************************************************************
148  * Open: initialize and create stuff
149  *****************************************************************************/
150 static int Open( vlc_object_t *p_this )
151 {
152     intf_thread_t *p_intf = (intf_thread_t *)p_this;
153     char *psz_mode, *psz_file, *psz_rrd_file;
154
155     CONSOLE_INTRO_MSG;
156     msg_Info( p_intf, "using logger..." );
157
158     /* Allocate instance and initialize some members */
159     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
160     if( p_intf->p_sys == NULL )
161     {
162         msg_Err( p_intf, "out of memory" );
163         return -1;
164     }
165
166     psz_mode = var_CreateGetString( p_intf, "logmode" );
167     if( psz_mode )
168     {
169         if( !strcmp( psz_mode, "text" ) )
170         {
171             p_intf->p_sys->i_mode = MODE_TEXT;
172         }
173         else if( !strcmp( psz_mode, "html" ) )
174         {
175             p_intf->p_sys->i_mode = MODE_HTML;
176         }
177 #ifdef HAVE_SYSLOG_H
178         else if( !strcmp( psz_mode, "syslog" ) )
179         {
180             p_intf->p_sys->i_mode = MODE_SYSLOG;
181         }
182 #endif
183         else
184         {
185             msg_Warn( p_intf, "invalid log mode `%s', using `text'", psz_mode );
186             p_intf->p_sys->i_mode = MODE_TEXT;
187         }
188
189         free( psz_mode );
190     }
191     else
192     {
193         msg_Warn( p_intf, "no log mode specified, using `text'" );
194         p_intf->p_sys->i_mode = MODE_TEXT;
195     }
196
197     if( p_intf->p_sys->i_mode != MODE_SYSLOG )
198     {
199         psz_file = config_GetPsz( p_intf, "logfile" );
200         if( !psz_file )
201         {
202 #ifdef __APPLE__
203             char *psz_homedir = p_this->p_libvlc->psz_homedir;
204
205             if( !psz_homedir )
206             {
207                 msg_Err( p_this, "unable to find home directory" );
208                 return -1;
209             }
210             psz_file = (char *)malloc( sizeof("/" LOG_DIR "/" LOG_FILE_HTML) +
211                                            strlen(psz_homedir) );
212             if( psz_file )
213             {
214                 switch( p_intf->p_sys->i_mode )
215                 {
216                 case MODE_HTML:
217                     sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_HTML,
218                          psz_homedir );
219                     break;
220                 case MODE_TEXT:
221                 default:
222                     sprintf( psz_file, "%s/" LOG_DIR "/" LOG_FILE_TEXT,
223                          psz_homedir );
224                     break;
225                 }
226             }
227 #else
228             switch( p_intf->p_sys->i_mode )
229             {
230             case MODE_HTML:
231                 psz_file = strdup( LOG_FILE_HTML );
232                 break;
233             case MODE_TEXT:
234             default:
235                 psz_file = strdup( LOG_FILE_TEXT );
236                 break;
237             }
238 #endif
239             msg_Warn( p_intf, "no log filename provided, using `%s'",
240                                psz_file );
241         }
242
243         /* Open the log file and remove any buffering for the stream */
244         msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
245         p_intf->p_sys->p_file = utf8_fopen( psz_file, "at" );
246         if( p_intf->p_sys->p_file == NULL )
247         {
248             msg_Err( p_intf, "error opening logfile `%s'", psz_file );
249             free( p_intf->p_sys );
250             free( psz_file );
251             return -1;
252         }
253         setvbuf( p_intf->p_sys->p_file, NULL, _IONBF, 0 );
254
255         free( psz_file );
256
257         switch( p_intf->p_sys->i_mode )
258         {
259         case MODE_HTML:
260             LOG_STRING( HTML_HEADER, p_intf->p_sys->p_file );
261             break;
262         case MODE_TEXT:
263         default:
264             LOG_STRING( TEXT_HEADER, p_intf->p_sys->p_file );
265             break;
266         }
267
268     }
269     else
270     {
271         p_intf->p_sys->p_file = NULL;
272 #ifdef HAVE_SYSLOG_H
273         openlog( "VLC", 0, LOG_DAEMON );
274 #endif
275     }
276
277     p_intf->p_sys->last_update = 0;
278     p_intf->p_sys->p_rrd = NULL;
279
280     psz_rrd_file = config_GetPsz( p_intf, "rrd-file" );
281     if( psz_rrd_file && *psz_rrd_file )
282     {
283         p_intf->p_sys->p_rrd = utf8_fopen( psz_rrd_file, "w" );
284     }
285
286     p_intf->p_sys->p_sub = msg_Subscribe( p_intf , MSG_QUEUE_NORMAL );
287     p_intf->pf_run = Run;
288
289     return 0;
290 }
291
292 /*****************************************************************************
293  * Close: destroy interface stuff
294  *****************************************************************************/
295 static void Close( vlc_object_t *p_this )
296 {
297     intf_thread_t *p_intf = (intf_thread_t *)p_this;
298
299     /* Flush the queue and unsubscribe from the message queue */
300     FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
301                 p_intf->p_sys->i_mode );
302     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
303
304     switch( p_intf->p_sys->i_mode )
305     {
306     case MODE_HTML:
307         LOG_STRING( HTML_FOOTER, p_intf->p_sys->p_file );
308         break;
309     case MODE_TEXT:
310 #ifdef HAVE_SYSLOG_H
311     case MODE_SYSLOG:
312         closelog();
313         break;
314 #endif
315     default:
316         LOG_STRING( TEXT_FOOTER, p_intf->p_sys->p_file );
317         break;
318     }
319
320     /* Close the log file */
321     if( p_intf->p_sys->i_mode != MODE_SYSLOG )
322         fclose( p_intf->p_sys->p_file );
323
324     /* Destroy structure */
325     free( p_intf->p_sys );
326 }
327
328 /*****************************************************************************
329  * Run: rc thread
330  *****************************************************************************
331  * This part of the interface is in a separate thread so that we can call
332  * exec() from within it without annoying the rest of the program.
333  *****************************************************************************/
334 static void Run( intf_thread_t *p_intf )
335 {
336     while( !p_intf->b_die )
337     {
338         FlushQueue( p_intf->p_sys->p_sub, p_intf->p_sys->p_file,
339                     p_intf->p_sys->i_mode );
340
341         if( p_intf->p_sys->p_rrd )
342             DoRRD( p_intf );
343
344         msleep( INTF_IDLE_SLEEP );
345     }
346 }
347
348 /*****************************************************************************
349  * FlushQueue: flush the message queue into the log
350  *****************************************************************************/
351 static void FlushQueue( msg_subscription_t *p_sub, FILE *p_file, int i_mode )
352 {
353     int i_start, i_stop;
354
355     vlc_mutex_lock( p_sub->p_lock );
356     i_stop = *p_sub->pi_stop;
357     vlc_mutex_unlock( p_sub->p_lock );
358
359     if( p_sub->i_start != i_stop )
360     {
361         /* Append all messages to log file */
362         for( i_start = p_sub->i_start;
363              i_start != i_stop;
364              i_start = (i_start+1) % VLC_MSG_QSIZE )
365         {
366             switch( i_mode )
367             {
368             case MODE_HTML:
369                 HtmlPrint( &p_sub->p_msg[i_start], p_file );
370                 break;
371 #ifdef HAVE_SYSLOG_H
372             case MODE_SYSLOG:
373                 SyslogPrint( &p_sub->p_msg[i_start] );
374                 break;
375 #endif
376             case MODE_TEXT:
377             default:
378                 TextPrint( &p_sub->p_msg[i_start], p_file );
379                 break;
380             }
381         }
382
383         vlc_mutex_lock( p_sub->p_lock );
384         p_sub->i_start = i_start;
385         vlc_mutex_unlock( p_sub->p_lock );
386     }
387 }
388
389 static const char *ppsz_type[4] = { ": ", " error: ",
390                                     " warning: ", " debug: " };
391
392 static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
393 {
394     LOG_STRING( p_msg->psz_module, p_file );
395     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
396     LOG_STRING( p_msg->psz_msg, p_file );
397     LOG_STRING( "\n", p_file );
398 }
399
400 #ifdef HAVE_SYSLOG_H
401 static void SyslogPrint( const msg_item_t *p_msg )
402 {
403     int i_priority = LOG_INFO;
404
405     if( p_msg->i_type  == 0 ) i_priority = LOG_INFO;
406     if( p_msg->i_type  == 1 ) i_priority = LOG_ERR;
407     if( p_msg->i_type  == 2 ) i_priority = LOG_WARNING;
408     if( p_msg->i_type  == 3 ) i_priority = LOG_DEBUG;
409
410     if( p_msg->psz_header )
411         syslog( i_priority, "%s %s: %s", p_msg->psz_header,
412                 p_msg->psz_module, p_msg->psz_msg );
413     else
414         syslog( i_priority, "%s: %s", p_msg->psz_module, p_msg->psz_msg );
415         
416 }
417 #endif
418
419 static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
420 {
421     static const char *ppsz_color[4] = { "<font color=\"#ffffff\">",
422                                          "<font color=\"#ff6666\">",
423                                          "<font color=\"#ffff66\">",
424                                          "<font color=\"#aaaaaa\">" };
425
426     LOG_STRING( p_msg->psz_module, p_file );
427     LOG_STRING( ppsz_type[p_msg->i_type], p_file );
428     LOG_STRING( ppsz_color[p_msg->i_type], p_file );
429     LOG_STRING( p_msg->psz_msg, p_file );
430     LOG_STRING( "</font>\n", p_file );
431 }
432
433 static void DoRRD( intf_thread_t *p_intf )
434 {
435     playlist_t *p_playlist;
436     if( mdate() - p_intf->p_sys->last_update < 1000000 )
437         return;
438     p_intf->p_sys->last_update = mdate();
439
440     p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
441                                                 FIND_ANYWHERE );
442     if( p_playlist && p_playlist->p_stats )
443     {
444         lldiv_t din = lldiv( p_playlist->p_stats->f_input_bitrate * 1000000,
445                              1000 );
446         lldiv_t ddm = lldiv( p_playlist->p_stats->f_demux_bitrate * 1000000,
447                              1000 );
448         lldiv_t dout = lldiv( p_playlist->p_stats->f_output_bitrate * 1000000,
449                              1000 );
450         fprintf( p_intf->p_sys->p_rrd,
451                    I64Fi":%lld.%03u:%lld.%03u:%lld.%03u\n",
452                    p_intf->p_sys->last_update/1000000,
453                    din.quot, (unsigned int)din.rem,
454                    ddm.quot, (unsigned int)ddm.rem,
455                    dout.quot, (unsigned int)dout.rem );
456         fflush( p_intf->p_sys->p_rrd );
457         vlc_object_release( p_playlist );
458     }
459 }