]> git.sesse.net Git - vlc/blob - src/misc/messages.c
FSF address change.
[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     msg_bank_t * p_bank = &p_this->p_libvlc->msg_bank;       /* message bank */
296     msg_queue_t *p_queue = NULL;
297     char *       psz_str = NULL;                 /* formatted message string */
298     va_list      args;
299     msg_item_t * p_item = NULL;                        /* pointer to message */
300     msg_item_t   item;                    /* message in case of a full queue */
301
302 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
303     int          i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
304 #endif
305     int i;
306
307     /*
308      * Convert message to string
309      */
310 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined( SYS_BEOS )
311     vlc_va_copy( args, _args );
312     vasprintf( &psz_str, psz_format, args );
313     va_end( args );
314 #else
315     psz_str = (char*) malloc( i_size * sizeof(char) );
316 #endif
317
318     if( psz_str == NULL )
319     {
320         fprintf( stderr, "main warning: can't store message (%s): ",
321                  strerror(errno) );
322         vlc_va_copy( args, _args );
323         vfprintf( stderr, psz_format, args );
324         va_end( args );
325         fprintf( stderr, "\n" );
326         return;
327     }
328
329 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || 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     /* Put message in queue */
337     vlc_mutex_lock( &p_bank->lock );
338     for( i = 0 ; i <p_bank->i_queues ;i++ )
339     {
340         if( p_bank->pp_queues[i]->i_id == i_queue_id )
341         {
342             p_queue = p_bank->pp_queues[i];
343         }
344     }
345
346     if( p_queue == NULL )
347     {
348         vlc_mutex_unlock( &p_bank->lock );
349         return;
350     }
351
352     vlc_mutex_lock( &p_queue->lock );
353
354     /* Check there is room in the queue for our message */
355     if( p_queue->b_overflow )
356     {
357         FlushMsg( p_queue );
358
359         if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
360         {
361             /* Still in overflow mode, print from a dummy item */
362             p_item = &item;
363         }
364         else
365         {
366             /* Pheeew, at last, there is room in the queue! */
367             p_queue->b_overflow = VLC_FALSE;
368         }
369     }
370     else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
371     {
372         FlushMsg( p_queue );
373
374         if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
375         {
376             p_queue->b_overflow = VLC_TRUE;
377
378             if( p_queue->i_id == MSG_QUEUE_NORMAL )
379             {
380                /* Put the overflow message in the queue */
381                 p_item = p_queue->msg + p_queue->i_stop;
382                 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
383
384                 p_item->i_type =        VLC_MSG_WARN;
385                 p_item->i_object_id =   p_this->i_object_id;
386                 p_item->i_object_type = p_this->i_object_type;
387                 p_item->psz_module =    strdup( "message" );
388                 p_item->psz_msg =       strdup( "message queue overflowed" );
389
390                PrintMsg( p_this, p_item );
391                /* We print from a dummy item */
392                p_item = &item;
393             }
394         }
395     }
396
397     if( !p_queue->b_overflow )
398     {
399         /* Put the message in the queue */
400         p_item = p_queue->msg + p_queue->i_stop;
401         p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
402     }
403
404     /* Fill message information fields */
405     p_item->i_type =        i_type;
406     p_item->i_object_id =   p_this->i_object_id;
407     p_item->i_object_type = p_this->i_object_type;
408     p_item->psz_module =    strdup( psz_module );
409     p_item->psz_msg =       psz_str;
410
411     if( p_queue->i_id == MSG_QUEUE_NORMAL )
412         PrintMsg( p_this, p_item );
413
414     if( p_queue->b_overflow )
415     {
416         if( p_item->psz_module )
417             free( p_item->psz_module );
418         if( p_item->psz_msg )
419             free( p_item->psz_msg );
420     }
421
422     vlc_mutex_unlock ( &p_queue->lock );
423     vlc_mutex_unlock( &p_bank->lock );
424 }
425
426 /* following functions are local */
427
428 /*****************************************************************************
429  * FlushMsg
430  *****************************************************************************
431  * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
432  * this function does not check the lock.
433  *****************************************************************************/
434 static void FlushMsg ( msg_queue_t *p_queue )
435 {
436     int i_index, i_start, i_stop;
437
438     /* Get the maximum message index that can be freed */
439     i_stop = p_queue->i_stop;
440
441     /* Check until which value we can free messages */
442     for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
443     {
444         i_start = p_queue->pp_sub[ i_index ]->i_start;
445
446         /* If this subscriber is late, we don't free messages before
447          * his i_start value, otherwise he'll miss messages */
448         if(   ( i_start < i_stop
449                && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
450            || ( i_stop < i_start
451                && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
452         {
453             i_stop = i_start;
454         }
455     }
456
457     /* Free message data */
458     for( i_index = p_queue->i_start;
459          i_index != i_stop;
460          i_index = (i_index+1) % VLC_MSG_QSIZE )
461     {
462         if( p_queue->msg[i_index].psz_msg )
463             free( p_queue->msg[i_index].psz_msg );
464         if( p_queue->msg[i_index].psz_module )
465             free( p_queue->msg[i_index].psz_module );
466     }
467
468     /* Update the new start value */
469     p_queue->i_start = i_index;
470 }
471
472 /*****************************************************************************
473  * PrintMsg: output a standard message item to stderr
474  *****************************************************************************
475  * Print a message to stderr, with colour formatting if needed.
476  *****************************************************************************/
477 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
478 {
479 #   define COL(x)  "\033[" #x ";1m"
480 #   define RED     COL(31)
481 #   define GREEN   COL(32)
482 #   define YELLOW  COL(33)
483 #   define WHITE   COL(37)
484 #   define GRAY    "\033[0m"
485
486 #ifdef UNDER_CE
487     int i_dummy;
488 #endif
489     static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
490     static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
491     char *psz_object = "private";
492     int i_type = p_item->i_type;
493
494     switch( i_type )
495     {
496         case VLC_MSG_ERR:
497             if( p_this->p_libvlc->i_verbose < 0 ) return;
498             break;
499         case VLC_MSG_INFO:
500             if( p_this->p_libvlc->i_verbose < 0 ) return;
501             break;
502         case VLC_MSG_WARN:
503             if( p_this->p_libvlc->i_verbose < 1 ) return;
504             break;
505         case VLC_MSG_DBG:
506             if( p_this->p_libvlc->i_verbose < 2 ) return;
507             break;
508     }
509
510     switch( p_item->i_object_type )
511     {
512         case VLC_OBJECT_ROOT: psz_object = "root"; break;
513         case VLC_OBJECT_VLC: psz_object = "vlc"; break;
514         case VLC_OBJECT_MODULE: psz_object = "module"; break;
515         case VLC_OBJECT_INTF: psz_object = "interface"; break;
516         case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
517         case VLC_OBJECT_ITEM: psz_object = "item"; break;
518         case VLC_OBJECT_INPUT: psz_object = "input"; break;
519         case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
520         case VLC_OBJECT_PACKETIZER: psz_object = "packetizer"; break;
521         case VLC_OBJECT_ENCODER: psz_object = "encoder"; break;
522         case VLC_OBJECT_VOUT: psz_object = "video output"; break;
523         case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
524         case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
525         case VLC_OBJECT_HTTPD: psz_object = "http daemon"; break;
526         case VLC_OBJECT_DIALOGS: psz_object = "dialogs provider"; break;
527         case VLC_OBJECT_VLM: psz_object = "vlm"; break;
528         case VLC_OBJECT_ANNOUNCE: psz_object = "announce handler"; break;
529         case VLC_OBJECT_DEMUX: psz_object = "demuxer"; break;
530         case VLC_OBJECT_ACCESS: psz_object = "access"; break;
531     }
532
533 #ifdef UNDER_CE
534 #   define CE_WRITE(str) WriteFile( p_this->p_libvlc->msg_bank.pp_queues[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( p_this->p_libvlc->msg_bank.pp_queues[MSG_QUEUE_NORMAL]->logfile );
544
545 #else
546     /* Send the message to stderr */
547     if( p_this->p_libvlc->b_color )
548     {
549         fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
550                          p_item->i_object_id, p_item->psz_module, psz_object,
551                          ppsz_type[i_type], ppsz_color[i_type],
552                          p_item->psz_msg );
553     }
554     else
555     {
556         fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
557                          p_item->psz_module, psz_object, ppsz_type[i_type],
558                          p_item->psz_msg );
559     }
560
561 #   if defined(WIN32)
562     fflush( stderr );
563 #   endif
564 #endif
565 }