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