]> git.sesse.net Git - vlc/blob - src/misc/messages.c
Don't print debug messages and don't do interaction on preparsing
[vlc] / src / misc / messages.c
1 /*****************************************************************************
2  * messages.c: messages interface
3  * This library provides an interface to the message queue to be used by other
4  * modules, especially intf modules. See config.h for output configuration.
5  *****************************************************************************
6  * Copyright (C) 1998-2005 the VideoLAN team
7  * $Id$
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdio.h>                                               /* required */
31 #include <stdarg.h>                                       /* va_list for BSD */
32 #include <stdlib.h>                                              /* malloc() */
33 #include <string.h>                                            /* strerror() */
34
35 #include <vlc/vlc.h>
36
37 #ifdef HAVE_FCNTL_H
38 #   include <fcntl.h>                  /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
39 #endif
40
41 #include <errno.h>                                                  /* errno */
42
43 #ifdef HAVE_UNISTD_H
44 #   include <unistd.h>                                   /* close(), write() */
45 #endif
46
47 #include "vlc_interface.h"
48
49 /*****************************************************************************
50  * Local macros
51  *****************************************************************************/
52 #if defined(HAVE_VA_COPY)
53 #   define vlc_va_copy(dest,src) va_copy(dest,src)
54 #elif defined(HAVE___VA_COPY)
55 #   define vlc_va_copy(dest,src) __va_copy(dest,src)
56 #else
57 #   define vlc_va_copy(dest,src) (dest)=(src)
58 #endif
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static void QueueMsg ( vlc_object_t *, int, int , const char *,
64                        const char *, va_list );
65 static void FlushMsg ( msg_queue_t * );
66 static void PrintMsg ( vlc_object_t *, msg_item_t * );
67 static void CreateMsgQueue( vlc_object_t *p_this, int i_queue );
68
69 /**
70  * Initialize messages queues
71  * This function initializes all message queues
72  */
73 void __msg_Create( vlc_object_t *p_this )
74 {
75     vlc_mutex_init( p_this, &(p_this->p_libvlc->msg_bank.lock) );
76
77     CreateMsgQueue( p_this, MSG_QUEUE_NORMAL );
78     CreateMsgQueue( p_this, MSG_QUEUE_HTTPD_ACCESS );
79
80 #ifdef UNDER_CE
81     p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile =
82         CreateFile( L"vlc-log.txt", GENERIC_WRITE,
83                     FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
84                     CREATE_ALWAYS, 0, NULL );
85     SetFilePointer( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->
86                                      logfile, 0, NULL, FILE_END );
87 #endif
88
89 }
90
91 static void CreateMsgQueue( vlc_object_t *p_this, int i_queue )
92 {
93     msg_queue_t *p_queue = (msg_queue_t *)malloc( sizeof( msg_queue_t ) );
94
95     vlc_mutex_init( p_this, &p_queue->lock );
96
97     p_queue->b_overflow = VLC_FALSE;
98     p_queue->i_id = i_queue;
99     p_queue->i_start = 0;
100     p_queue->i_stop = 0;
101
102     p_queue->i_sub = 0;
103     p_queue->pp_sub = NULL;
104
105     INSERT_ELEM( p_this->p_libvlc->msg_bank.pp_queues,
106                  p_this->p_libvlc->msg_bank.i_queues,
107                  i_queue,
108                  p_queue );
109 }
110
111 /**
112  * Flush all message queues
113  */
114 void __msg_Flush( vlc_object_t *p_this )
115 {
116     int i;
117
118     for( i = 0 ; i < p_this->p_libvlc->msg_bank.i_queues; i++ )
119     {
120         vlc_mutex_lock( &p_this->p_libvlc->msg_bank.pp_queues[i]->lock );
121         FlushMsg( p_this->p_libvlc->msg_bank.pp_queues[i] );
122         vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.pp_queues[i]->lock );
123     }
124 }
125
126 /**
127  * Free resources allocated by msg_Create
128  *
129  * This functions prints all messages remaining in the normal queue,
130  * then frees all the allocated ressources
131  * No other messages interface functions should be called after this one.
132  */
133 void __msg_Destroy( vlc_object_t *p_this )
134 {
135     int i;
136     for( i =  0 ; i < p_this->p_libvlc->msg_bank.i_queues; i++ )
137     {
138         msg_queue_t *p_queue = p_this->p_libvlc->msg_bank.pp_queues[i];
139         if( p_queue->i_sub )
140         {
141             msg_Err( p_this, "stale interface subscribers" );
142         }
143         FlushMsg( p_queue );
144
145 #ifdef UNDER_CE
146         if( i == MSG_QUEUE_NORMAL )
147             CloseHandle( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile );
148 #endif
149         /* Destroy lock */
150         vlc_mutex_destroy( &p_queue->lock );
151     }
152     vlc_mutex_destroy( &(p_this->p_libvlc->msg_bank.lock) );
153 }
154
155 /**
156  * Subscribe to the message queue.
157  */
158 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this, int i_queue )
159 {
160     msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
161     msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
162     msg_queue_t *p_queue = NULL;
163     int i;
164
165     vlc_mutex_lock( &p_bank->lock );
166
167     for( i = 0 ; i <p_bank->i_queues ;i++ )
168     {
169         if( p_bank->pp_queues[i]->i_id == i_queue )
170         {
171             p_queue = p_bank->pp_queues[i];
172         }
173     }
174
175     if( p_queue == NULL )
176     {
177         vlc_mutex_unlock( &p_bank->lock );
178         return NULL;
179     }
180
181     vlc_mutex_lock( &p_queue->lock );
182
183     /* Add subscription to the list */
184     INSERT_ELEM( p_bank->pp_queues[i_queue]->pp_sub,
185                  p_bank->pp_queues[i_queue]->i_sub,
186                  p_bank->pp_queues[i_queue]->i_sub,
187                  p_sub );
188
189     p_sub->i_start = p_queue->i_start;
190     p_sub->pi_stop = &p_queue->i_stop;
191
192     p_sub->p_msg   = p_queue->msg;
193     p_sub->p_lock  = &p_queue->lock;
194
195     vlc_mutex_unlock( &p_queue->lock );
196     vlc_mutex_unlock( &p_bank->lock );
197
198     return p_sub;
199 }
200
201 /**
202  * Unsubscribe from the message queue.
203  */
204 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
205 {
206     msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
207     int i,j;
208
209     vlc_mutex_lock( &p_bank->lock );
210
211     for( i = 0 ; i< p_bank->i_queues ; i++ )
212     {
213         vlc_mutex_lock( & p_bank->pp_queues[i]->lock );
214         for( j = 0 ; j< p_bank->pp_queues[i]->i_sub ; j++ )
215         {
216             if( p_bank->pp_queues[i]->pp_sub[j] == p_sub )
217             {
218                 REMOVE_ELEM( p_bank->pp_queues[i]->pp_sub,
219                              p_bank->pp_queues[i]->i_sub,
220                              j );
221                 if( p_sub ) free( p_sub );
222             }
223         }
224         vlc_mutex_unlock( & p_bank->pp_queues[i]->lock );
225     }
226
227     vlc_mutex_unlock( &p_bank->lock );
228 }
229
230 /*****************************************************************************
231  * __msg_*: print a message
232  *****************************************************************************
233  * These functions queue a message for later printing.
234  *****************************************************************************/
235 void __msg_Generic( vlc_object_t *p_this, int i_queue_id, int i_type,
236                     const char *psz_module,
237                     const char *psz_format, ... )
238 {
239     va_list args;
240
241     va_start( args, psz_format );
242     QueueMsg( p_this, i_queue_id, i_type, psz_module, psz_format, args );
243     va_end( args );
244 }
245
246 void __msg_GenericVa( vlc_object_t *p_this, int i_queue_id,
247                       int i_type, const char *psz_module,
248                       const char *psz_format, va_list args )
249 {
250     QueueMsg( p_this, i_queue_id, i_type, psz_module, psz_format, args );
251 }
252
253 /* Generic functions used when variadic macros are not available. */
254 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
255     void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
256     { \
257         va_list args; \
258         va_start( args, psz_format ); \
259         QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
260                   psz_format, args ); \
261         va_end( args ); \
262     } \
263     struct _
264 /**
265  * Output an informational message.
266  * \note Do not use this for debug messages
267  * \see input_AddInfo
268  */
269 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
270 /**
271  * Output an error message.
272  */
273 DECLARE_MSG_FN( __msg_Err,  VLC_MSG_ERR );
274 /**
275  * Output a waring message
276  */
277 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
278 /**
279  * Output a debug message
280  */
281 DECLARE_MSG_FN( __msg_Dbg,  VLC_MSG_DBG );
282
283 /**
284  * Add a message to a queue
285  *
286  * This function provides basic functionnalities to other msg_* functions.
287  * It adds a message to a queue (after having printed all stored messages if it
288  * is full). If the message can't be converted to string in memory, it issues
289  * a warning.
290  */
291 static void QueueMsg( vlc_object_t *p_this, int i_queue_id, int i_type,
292                       const char *psz_module,
293                       const char *psz_format, va_list _args )
294 {
295     int         i_header_size;               /* Size of the additionnal header */
296     vlc_object_t *p_obj;
297     msg_bank_t * p_bank = &p_this->p_libvlc->msg_bank;       /* message bank */
298     msg_queue_t *p_queue = NULL;
299     char *       psz_str = NULL;                 /* formatted message string */
300     char *       psz_header = NULL;
301     va_list      args;
302     msg_item_t * p_item = NULL;                        /* pointer to message */
303     msg_item_t   item;                    /* message in case of a full queue */
304
305 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
306     int          i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
307 #endif
308     int i;
309
310     if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
311         (p_this->i_flags & OBJECT_FLAGS_NODBG &&
312          i_type == VLC_MSG_DBG ) )
313     {
314         return;
315     }
316
317     /*
318      * Convert message to string
319      */
320 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined( SYS_BEOS )
321     vlc_va_copy( args, _args );
322     vasprintf( &psz_str, psz_format, args );
323     va_end( args );
324 #else
325     psz_str = (char*) malloc( i_size * sizeof(char) );
326 #endif
327
328     if( psz_str == NULL )
329     {
330         fprintf( stderr, "main warning: can't store message (%s): ",
331                  strerror(errno) );
332         vlc_va_copy( args, _args );
333         vfprintf( stderr, psz_format, args );
334         va_end( args );
335         fprintf( stderr, "\n" );
336         return;
337     }
338
339     i_header_size = 0;
340     p_obj = p_this;
341     while( p_obj != NULL )
342     {
343         char *psz_old = NULL;
344         if( p_obj == NULL ) break;
345         if( p_obj->psz_header )
346         {
347             i_header_size += strlen( p_obj->psz_header ) + 4;
348             if( psz_header )
349             {
350                 psz_old = strdup( psz_header );
351                 psz_header = (char*)realloc( psz_header, i_header_size );
352                 snprintf( psz_header, i_header_size , "[%s] %s",
353                           p_obj->psz_header, psz_old );
354             }
355             else
356             {
357                 psz_header = (char *)malloc( i_header_size );
358                 snprintf( psz_header, i_header_size, "[%s]",
359                           p_obj->psz_header );
360             }
361         }
362         if( psz_old ) free( psz_old );
363         p_obj = p_obj->p_parent;
364     }
365
366 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
367     vlc_va_copy( args, _args );
368     vsnprintf( psz_str, i_size, psz_format, args );
369     va_end( args );
370     psz_str[ i_size - 1 ] = 0; /* Just in case */
371 #endif
372
373     /* Put message in queue */
374     vlc_mutex_lock( &p_bank->lock );
375     for( i = 0 ; i <p_bank->i_queues ;i++ )
376     {
377         if( p_bank->pp_queues[i]->i_id == i_queue_id )
378         {
379             p_queue = p_bank->pp_queues[i];
380         }
381     }
382
383     if( p_queue == NULL )
384     {
385         vlc_mutex_unlock( &p_bank->lock );
386         return;
387     }
388
389     vlc_mutex_lock( &p_queue->lock );
390
391     /* Check there is room in the queue for our message */
392     if( p_queue->b_overflow )
393     {
394         FlushMsg( p_queue );
395
396         if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
397         {
398             /* Still in overflow mode, print from a dummy item */
399             p_item = &item;
400         }
401         else
402         {
403             /* Pheeew, at last, there is room in the queue! */
404             p_queue->b_overflow = VLC_FALSE;
405         }
406     }
407     else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
408     {
409         FlushMsg( p_queue );
410
411         if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
412         {
413             p_queue->b_overflow = VLC_TRUE;
414
415             if( p_queue->i_id == MSG_QUEUE_NORMAL )
416             {
417                /* Put the overflow message in the queue */
418                 p_item = p_queue->msg + p_queue->i_stop;
419                 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
420
421                 p_item->i_type =        VLC_MSG_WARN;
422                 p_item->i_object_id =   p_this->i_object_id;
423                 p_item->i_object_type = p_this->i_object_type;
424                 p_item->psz_module =    strdup( "message" );
425                 p_item->psz_msg =       strdup( "message queue overflowed" );
426                 p_item->psz_header =    NULL;
427
428                PrintMsg( p_this, p_item );
429                /* We print from a dummy item */
430                p_item = &item;
431             }
432         }
433     }
434
435     if( !p_queue->b_overflow )
436     {
437         /* Put the message in the queue */
438         p_item = p_queue->msg + p_queue->i_stop;
439         p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
440     }
441
442     /* Fill message information fields */
443     p_item->i_type =        i_type;
444     p_item->i_object_id =   p_this->i_object_id;
445     p_item->i_object_type = p_this->i_object_type;
446     p_item->psz_module =    strdup( psz_module );
447     p_item->psz_msg =       psz_str;
448     p_item->psz_header =    psz_header;
449
450     if( p_queue->i_id == MSG_QUEUE_NORMAL )
451         PrintMsg( p_this, p_item );
452
453     if( p_queue->b_overflow )
454     {
455         if( p_item->psz_module )
456             free( p_item->psz_module );
457         if( p_item->psz_msg )
458             free( p_item->psz_msg );
459     }
460
461     vlc_mutex_unlock ( &p_queue->lock );
462     vlc_mutex_unlock( &p_bank->lock );
463 }
464
465 /* following functions are local */
466
467 /*****************************************************************************
468  * FlushMsg
469  *****************************************************************************
470  * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
471  * this function does not check the lock.
472  *****************************************************************************/
473 static void FlushMsg ( msg_queue_t *p_queue )
474 {
475     int i_index, i_start, i_stop;
476
477     /* Get the maximum message index that can be freed */
478     i_stop = p_queue->i_stop;
479
480     /* Check until which value we can free messages */
481     for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
482     {
483         i_start = p_queue->pp_sub[ i_index ]->i_start;
484
485         /* If this subscriber is late, we don't free messages before
486          * his i_start value, otherwise he'll miss messages */
487         if(   ( i_start < i_stop
488                && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
489            || ( i_stop < i_start
490                && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
491         {
492             i_stop = i_start;
493         }
494     }
495
496     /* Free message data */
497     for( i_index = p_queue->i_start;
498          i_index != i_stop;
499          i_index = (i_index+1) % VLC_MSG_QSIZE )
500     {
501         if( p_queue->msg[i_index].psz_msg )
502             free( p_queue->msg[i_index].psz_msg );
503         if( p_queue->msg[i_index].psz_module )
504             free( p_queue->msg[i_index].psz_module );
505         if( p_queue->msg[i_index].psz_header )
506             free( p_queue->msg[i_index].psz_header );
507     }
508
509     /* Update the new start value */
510     p_queue->i_start = i_index;
511 }
512
513 /*****************************************************************************
514  * PrintMsg: output a standard message item to stderr
515  *****************************************************************************
516  * Print a message to stderr, with colour formatting if needed.
517  *****************************************************************************/
518 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
519 {
520 #   define COL(x)  "\033[" #x ";1m"
521 #   define RED     COL(31)
522 #   define GREEN   COL(32)
523 #   define YELLOW  COL(33)
524 #   define WHITE   COL(37)
525 #   define GRAY    "\033[0m"
526
527 #ifdef UNDER_CE
528     int i_dummy;
529 #endif
530     static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
531     static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
532     char *psz_object = "private";
533     int i_type = p_item->i_type;
534
535     switch( i_type )
536     {
537         case VLC_MSG_ERR:
538             if( p_this->p_libvlc->i_verbose < 0 ) return;
539             break;
540         case VLC_MSG_INFO:
541             if( p_this->p_libvlc->i_verbose < 0 ) return;
542             break;
543         case VLC_MSG_WARN:
544             if( p_this->p_libvlc->i_verbose < 1 ) return;
545             break;
546         case VLC_MSG_DBG:
547             if( p_this->p_libvlc->i_verbose < 2 ) return;
548             break;
549     }
550
551     switch( p_item->i_object_type )
552     {
553         case VLC_OBJECT_ROOT: psz_object = "root"; break;
554         case VLC_OBJECT_VLC: psz_object = "vlc"; break;
555         case VLC_OBJECT_MODULE: psz_object = "module"; break;
556         case VLC_OBJECT_INTF: psz_object = "interface"; break;
557         case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
558         case VLC_OBJECT_ITEM: psz_object = "item"; break;
559         case VLC_OBJECT_INPUT: psz_object = "input"; break;
560         case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
561         case VLC_OBJECT_PACKETIZER: psz_object = "packetizer"; break;
562         case VLC_OBJECT_ENCODER: psz_object = "encoder"; break;
563         case VLC_OBJECT_VOUT: psz_object = "video output"; break;
564         case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
565         case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
566         case VLC_OBJECT_HTTPD: psz_object = "http daemon"; break;
567         case VLC_OBJECT_DIALOGS: psz_object = "dialogs provider"; break;
568         case VLC_OBJECT_VLM: psz_object = "vlm"; break;
569         case VLC_OBJECT_ANNOUNCE: psz_object = "announce handler"; break;
570         case VLC_OBJECT_DEMUX: psz_object = "demuxer"; break;
571         case VLC_OBJECT_ACCESS: psz_object = "access"; break;
572     }
573
574 #ifdef UNDER_CE
575 #   define CE_WRITE(str) WriteFile( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile, \
576                                     str, strlen(str), &i_dummy, NULL );
577     CE_WRITE( p_item->psz_module );
578     CE_WRITE( " " );
579     CE_WRITE( psz_object );
580     CE_WRITE( ppsz_type[i_type] );
581     CE_WRITE( ": " );
582     CE_WRITE( p_item->psz_msg );
583     CE_WRITE( "\r\n" );
584     FlushFileBuffers( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile );
585
586 #else
587     /* Send the message to stderr */
588     if( p_this->p_libvlc->b_color )
589     {
590         if( p_item->psz_header )
591         {
592             fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
593                               "\n",
594                          p_item->i_object_id, p_item->psz_header,
595                          p_item->psz_module, psz_object,
596                          ppsz_type[i_type], ppsz_color[i_type],
597                          p_item->psz_msg );
598         }
599         else
600         {
601              fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
602                          p_item->i_object_id, p_item->psz_module, psz_object,
603                          ppsz_type[i_type], ppsz_color[i_type],
604                          p_item->psz_msg );
605         }
606     }
607     else
608     {
609         if( p_item->psz_header )
610         {
611             fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
612                          p_item->psz_header, p_item->psz_module,
613                          psz_object, ppsz_type[i_type], p_item->psz_msg );
614         }
615         else
616         {
617             fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
618                          p_item->psz_module, psz_object, ppsz_type[i_type],
619                          p_item->psz_msg );
620         }
621     }
622
623 #   if defined(WIN32)
624     fflush( stderr );
625 #   endif
626 #endif
627 }