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