]> git.sesse.net Git - vlc/blob - src/misc/messages.c
Clean up a bit queues handling. Do not use dynamic arrays
[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 /*****************************************************************************
198  * __msg_*: print a message
199  *****************************************************************************
200  * These functions queue a message for later printing.
201  *****************************************************************************/
202 void __msg_Generic( vlc_object_t *p_this, int i_queue, int i_type,
203                     const char *psz_module,
204                     const char *psz_format, ... )
205 {
206     va_list args;
207
208     va_start( args, psz_format );
209     QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
210     va_end( args );
211 }
212
213 void __msg_GenericVa( vlc_object_t *p_this, int i_queue,
214                       int i_type, const char *psz_module,
215                       const char *psz_format, va_list args )
216 {
217     QueueMsg( p_this, i_queue, i_type, psz_module, psz_format, args );
218 }
219
220 /* Generic functions used when variadic macros are not available. */
221 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
222     void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
223     { \
224         va_list args; \
225         va_start( args, psz_format ); \
226         QueueMsg( (vlc_object_t *)p_this,MSG_QUEUE_NORMAL, FN_TYPE, "unknown", \
227                   psz_format, args ); \
228         va_end( args ); \
229     } \
230     struct _
231 /**
232  * Output an informational message.
233  * \note Do not use this for debug messages
234  * \see input_AddInfo
235  */
236 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
237 /**
238  * Output an error message.
239  */
240 DECLARE_MSG_FN( __msg_Err,  VLC_MSG_ERR );
241 /**
242  * Output a waring message
243  */
244 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
245 /**
246  * Output a debug message
247  */
248 DECLARE_MSG_FN( __msg_Dbg,  VLC_MSG_DBG );
249
250 /**
251  * Add a message to a queue
252  *
253  * This function provides basic functionnalities to other msg_* functions.
254  * It adds a message to a queue (after having printed all stored messages if it
255  * is full). If the message can't be converted to string in memory, it issues
256  * a warning.
257  */
258 static void QueueMsg( vlc_object_t *p_this, int i_queue, int i_type,
259                       const char *psz_module,
260                       const char *psz_format, va_list _args )
261 {
262     int         i_header_size;             /* Size of the additionnal header */
263     vlc_object_t *p_obj;
264     char *       psz_str = NULL;                 /* formatted message string */
265     char *       psz_header = NULL;
266     va_list      args;
267     msg_item_t * p_item = NULL;                        /* pointer to message */
268     msg_item_t   item;                    /* message in case of a full queue */
269     msg_queue_t *p_queue;
270
271 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
272     int          i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
273 #endif
274
275     if( p_this == NULL || p_this->i_flags & OBJECT_FLAGS_QUIET ||
276         (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
277     {
278         return;
279     }
280
281     /* Convert message to string  */
282 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
283     vlc_va_copy( args, _args );
284     vasprintf( &psz_str, psz_format, args );
285     va_end( args );
286 #else
287     psz_str = (char*) malloc( i_size * sizeof(char) );
288 #endif
289
290     if( psz_str == NULL )
291     {
292         fprintf( stderr, "main warning: can't store message (%s): ",
293                  strerror(errno) );
294         vlc_va_copy( args, _args );
295         /* We should use utf8_vfprintf - but it calls malloc()... */
296         vfprintf( stderr, psz_format, args );
297         va_end( args );
298         fputs( "\n", stderr );
299         return;
300     }
301
302     i_header_size = 0;
303     p_obj = p_this;
304     while( p_obj != NULL )
305     {
306         char *psz_old = NULL;
307         if( p_obj == NULL ) break;
308         if( p_obj->psz_header )
309         {
310             i_header_size += strlen( p_obj->psz_header ) + 4;
311             if( psz_header )
312             {
313                 psz_old = strdup( psz_header );
314                 psz_header = (char*)realloc( psz_header, i_header_size );
315                 snprintf( psz_header, i_header_size , "[%s] %s",
316                           p_obj->psz_header, psz_old );
317             }
318             else
319             {
320                 psz_header = (char *)malloc( i_header_size );
321                 snprintf( psz_header, i_header_size, "[%s]",
322                           p_obj->psz_header );
323             }
324         }
325         if( psz_old ) free( psz_old );
326         p_obj = p_obj->p_parent;
327     }
328
329 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
330     vlc_va_copy( args, _args );
331     vsnprintf( psz_str, i_size, psz_format, args );
332     va_end( args );
333     psz_str[ i_size - 1 ] = 0; /* Just in case */
334 #endif
335
336     assert( i_queue < NB_QUEUES );
337     LOCK_BANK;
338     p_queue = &QUEUE(i_queue) ;
339     vlc_mutex_lock( &p_queue->lock );
340
341     /* Check there is room in the queue for our message */
342     if( p_queue->b_overflow )
343     {
344         FlushMsg( p_queue );
345
346         if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
347         {
348             /* Still in overflow mode, print from a dummy item */
349             p_item = &item;
350         }
351         else
352         {
353             /* Pheeew, at last, there is room in the queue! */
354             p_queue->b_overflow = VLC_FALSE;
355         }
356     }
357     else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
358     {
359         FlushMsg( p_queue );
360
361         if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
362         {
363             p_queue->b_overflow = VLC_TRUE;
364
365             if( p_queue->i_id == MSG_QUEUE_NORMAL )
366             {
367                /* Put the overflow message in the queue */
368                 p_item = p_queue->msg + p_queue->i_stop;
369                 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
370
371                 p_item->i_type =        VLC_MSG_WARN;
372                 p_item->i_object_id =   p_this->i_object_id;
373                 p_item->i_object_type = p_this->i_object_type;
374                 p_item->psz_module =    strdup( "message" );
375                 p_item->psz_msg =       strdup( "message queue overflowed" );
376                 p_item->psz_header =    NULL;
377
378                PrintMsg( p_this, p_item );
379                /* We print from a dummy item */
380                p_item = &item;
381             }
382         }
383     }
384
385     if( !p_queue->b_overflow )
386     {
387         /* Put the message in the queue */
388         p_item = p_queue->msg + p_queue->i_stop;
389         p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
390     }
391
392     /* Fill message information fields */
393     p_item->i_type =        i_type;
394     p_item->i_object_id =   p_this->i_object_id;
395     p_item->i_object_type = p_this->i_object_type;
396     p_item->psz_module =    strdup( psz_module );
397     p_item->psz_msg =       psz_str;
398     p_item->psz_header =    psz_header;
399
400     if( p_queue->i_id == MSG_QUEUE_NORMAL )
401         PrintMsg( p_this, p_item );
402
403     if( p_queue->b_overflow )
404     {
405         if( p_item->psz_module )
406             free( p_item->psz_module );
407         if( p_item->psz_msg )
408             free( p_item->psz_msg );
409         if( p_item->psz_header )
410             free( p_item->psz_header );
411     }
412
413     vlc_mutex_unlock ( &p_queue->lock );
414     UNLOCK_BANK;
415 }
416
417 /* following functions are local */
418
419 /*****************************************************************************
420  * FlushMsg
421  *****************************************************************************
422  * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
423  * this function does not check the lock.
424  *****************************************************************************/
425 static void FlushMsg ( msg_queue_t *p_queue )
426 {
427     int i_index, i_start, i_stop;
428
429     /* Get the maximum message index that can be freed */
430     i_stop = p_queue->i_stop;
431
432     /* Check until which value we can free messages */
433     for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
434     {
435         i_start = p_queue->pp_sub[ i_index ]->i_start;
436
437         /* If this subscriber is late, we don't free messages before
438          * his i_start value, otherwise he'll miss messages */
439         if(   ( i_start < i_stop
440                && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
441            || ( i_stop < i_start
442                && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
443         {
444             i_stop = i_start;
445         }
446     }
447
448     /* Free message data */
449     for( i_index = p_queue->i_start;
450          i_index != i_stop;
451          i_index = (i_index+1) % VLC_MSG_QSIZE )
452     {
453         if( p_queue->msg[i_index].psz_msg )
454             free( p_queue->msg[i_index].psz_msg );
455         if( p_queue->msg[i_index].psz_module )
456             free( p_queue->msg[i_index].psz_module );
457         if( p_queue->msg[i_index].psz_header )
458             free( p_queue->msg[i_index].psz_header );
459     }
460
461     /* Update the new start value */
462     p_queue->i_start = i_index;
463 }
464
465 /*****************************************************************************
466  * PrintMsg: output a standard message item to stderr
467  *****************************************************************************
468  * Print a message to stderr, with colour formatting if needed.
469  *****************************************************************************/
470 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
471 {
472 #   define COL(x)  "\033[" #x ";1m"
473 #   define RED     COL(31)
474 #   define GREEN   COL(32)
475 #   define YELLOW  COL(33)
476 #   define WHITE   COL(37)
477 #   define GRAY    "\033[0m"
478
479 #ifdef UNDER_CE
480     int i_dummy;
481 #endif
482     static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
483     static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
484     char *psz_object = "private";
485     int i_type = p_item->i_type;
486
487     switch( i_type )
488     {
489         case VLC_MSG_ERR:
490             if( p_this->p_libvlc->i_verbose < 0 ) return;
491             break;
492         case VLC_MSG_INFO:
493             if( p_this->p_libvlc->i_verbose < 0 ) return;
494             break;
495         case VLC_MSG_WARN:
496             if( p_this->p_libvlc->i_verbose < 1 ) return;
497             break;
498         case VLC_MSG_DBG:
499             if( p_this->p_libvlc->i_verbose < 2 ) return;
500             break;
501     }
502
503     switch( p_item->i_object_type )
504     {
505         case VLC_OBJECT_ROOT: psz_object = "root"; break;
506         case VLC_OBJECT_LIBVLC: psz_object = "libvlc"; break;
507         case VLC_OBJECT_MODULE: psz_object = "module"; break;
508         case VLC_OBJECT_INTF: psz_object = "interface"; break;
509         case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
510         case VLC_OBJECT_ITEM: psz_object = "item"; break;
511         case VLC_OBJECT_INPUT: psz_object = "input"; break;
512         case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
513         case VLC_OBJECT_PACKETIZER: psz_object = "packetizer"; break;
514         case VLC_OBJECT_ENCODER: psz_object = "encoder"; break;
515         case VLC_OBJECT_VOUT: psz_object = "video output"; break;
516         case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
517         case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
518         case VLC_OBJECT_HTTPD: psz_object = "http server"; break;
519         case VLC_OBJECT_HTTPD_HOST: psz_object = "http server"; break;
520         case VLC_OBJECT_DIALOGS: psz_object = "dialogs provider"; break;
521         case VLC_OBJECT_VLM: psz_object = "vlm"; break;
522         case VLC_OBJECT_ANNOUNCE: psz_object = "announce handler"; break;
523         case VLC_OBJECT_DEMUX: psz_object = "demuxer"; break;
524         case VLC_OBJECT_ACCESS: psz_object = "access"; break;
525     }
526
527 #ifdef UNDER_CE
528 #   define CE_WRITE(str) WriteFile( QUEUE(MSG_QUEUE_NORMAL).logfile, \
529                                     str, strlen(str), &i_dummy, NULL );
530     CE_WRITE( p_item->psz_module );
531     CE_WRITE( " " );
532     CE_WRITE( psz_object );
533     CE_WRITE( ppsz_type[i_type] );
534     CE_WRITE( ": " );
535     CE_WRITE( p_item->psz_msg );
536     CE_WRITE( "\r\n" );
537     FlushFileBuffers( QUEUE(MSG_QUEUE_NORMAL).logfile );
538
539 #else
540     /* Send the message to stderr */
541     if( p_this->p_libvlc->b_color )
542     {
543         if( p_item->psz_header )
544         {
545             utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
546                               "\n",
547                          p_item->i_object_id, p_item->psz_header,
548                          p_item->psz_module, psz_object,
549                          ppsz_type[i_type], ppsz_color[i_type],
550                          p_item->psz_msg );
551         }
552         else
553         {
554              utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
555                          p_item->i_object_id, p_item->psz_module, psz_object,
556                          ppsz_type[i_type], ppsz_color[i_type],
557                          p_item->psz_msg );
558         }
559     }
560     else
561     {
562         if( p_item->psz_header )
563         {
564             utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
565                          p_item->psz_header, p_item->psz_module,
566                          psz_object, ppsz_type[i_type], p_item->psz_msg );
567         }
568         else
569         {
570             utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
571                          p_item->psz_module, psz_object, ppsz_type[i_type],
572                          p_item->psz_msg );
573         }
574     }
575
576 #   if defined(WIN32)
577     fflush( stderr );
578 #   endif
579 #endif
580 }