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