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