]> git.sesse.net Git - vlc/blob - src/misc/messages.c
* ./mozilla/vlcshell.cpp: ported the Mozilla plugin to Windows.
[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-2002 VideoLAN
7  * $Id: messages.c,v 1.16 2002/10/22 21:10:28 sam Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                  /* errno */
31 #include <fcntl.h>                     /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
32 #include <stdio.h>                                               /* required */
33 #include <stdarg.h>                                       /* va_list for BSD */
34 #include <stdlib.h>                                              /* malloc() */
35 #include <string.h>                                            /* strerror() */
36
37 #include <vlc/vlc.h>
38
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>                                      /* close(), write() */
41 #endif
42
43 #include "interface.h"
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static void QueueMsg ( vlc_object_t *, int , const char *,
49                        const char *, va_list );
50 static void FlushMsg ( msg_bank_t * );
51 static void PrintMsg ( vlc_object_t *, msg_item_t * );
52
53 #if defined( WIN32 )
54 static char *ConvertPrintfFormatString ( const char *psz_format );
55 #endif
56
57 /*****************************************************************************
58  * msg_Create: initialize messages interface
59  *****************************************************************************
60  * This functions has to be called before any call to other msg_* functions.
61  * It set up the locks and the message queue if it is used.
62  *****************************************************************************/
63 void __msg_Create( vlc_object_t *p_this )
64 {
65     /* Message queue initialization */
66     vlc_mutex_init( p_this, &p_this->p_libvlc->msg_bank.lock );
67
68     p_this->p_libvlc->msg_bank.b_configured = VLC_FALSE;
69     p_this->p_libvlc->msg_bank.b_overflow = VLC_FALSE;
70
71     p_this->p_libvlc->msg_bank.i_start = 0;
72     p_this->p_libvlc->msg_bank.i_stop = 0;
73
74     p_this->p_libvlc->msg_bank.i_sub = 0;
75     p_this->p_libvlc->msg_bank.pp_sub = NULL;
76 }
77
78 /*****************************************************************************
79  * msg_Flush: flush the message queue
80  *****************************************************************************/
81 void __msg_Flush( vlc_object_t *p_this )
82 {
83     int i_index;
84
85     vlc_mutex_lock( &p_this->p_libvlc->msg_bank.lock );
86
87     p_this->p_libvlc->msg_bank.b_configured = VLC_TRUE;
88
89     for( i_index = p_this->p_libvlc->msg_bank.i_start;
90          i_index != p_this->p_libvlc->msg_bank.i_stop;
91          i_index = (i_index+1) % VLC_MSG_QSIZE )
92     {
93         PrintMsg( p_this, &p_this->p_libvlc->msg_bank.msg[i_index] );
94     }
95
96     FlushMsg( &p_this->p_libvlc->msg_bank );
97
98     vlc_mutex_unlock( &p_this->p_libvlc->msg_bank.lock );
99 }
100
101 /*****************************************************************************
102  * msg_Destroy: free resources allocated by msg_Create
103  *****************************************************************************
104  * This functions prints all messages remaining in queue, then free all the
105  * resources allocated by msg_Create.
106  * No other messages interface functions should be called after this one.
107  *****************************************************************************/
108 void __msg_Destroy( vlc_object_t *p_this )
109 {
110     if( p_this->p_libvlc->msg_bank.i_sub )
111     {
112         msg_Err( p_this, "stale interface subscribers" );
113     }
114
115     /* Flush the queue */
116     if( !p_this->p_libvlc->msg_bank.b_configured )
117     {
118         msg_Flush( p_this );
119     }
120     else
121     {
122         FlushMsg( &p_this->p_libvlc->msg_bank );
123     }
124
125     /* Destroy lock */
126     vlc_mutex_destroy( &p_this->p_libvlc->msg_bank.lock );
127 }
128
129 /*****************************************************************************
130  * msg_Subscribe: subscribe to the message queue.
131  *****************************************************************************/
132 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
133 {
134     msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
135     msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
136
137     vlc_mutex_lock( &p_bank->lock );
138
139     /* Add subscription to the list */
140     p_bank->i_sub++;
141     p_bank->pp_sub = realloc( p_bank->pp_sub,
142                               p_bank->i_sub * sizeof( msg_subscription_t* ) );
143
144     p_bank->pp_sub[ p_bank->i_sub - 1 ] = p_sub;
145
146     p_sub->i_start = p_bank->i_start;
147     p_sub->pi_stop = &p_bank->i_stop;
148
149     p_sub->p_msg   = p_bank->msg;
150     p_sub->p_lock  = &p_bank->lock;
151
152     vlc_mutex_unlock( &p_bank->lock );
153
154     return p_sub;
155 }
156
157 /*****************************************************************************
158  * msg_Unsubscribe: unsubscribe from the message queue.
159  *****************************************************************************/
160 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
161 {
162     msg_bank_t *p_bank = &p_this->p_libvlc->msg_bank;
163     int i_index;
164
165     vlc_mutex_lock( &p_bank->lock );
166
167     /* Sanity check */
168     if( !p_bank->i_sub )
169     {
170         msg_Err( p_this, "no subscriber in the list" );
171         return;
172     }
173
174     /* Look for the appropriate subscription */
175     for( i_index = 0; i_index < p_bank->i_sub; i_index++ )
176     {
177         if( p_bank->pp_sub[ i_index ] == p_sub )
178         {
179             break;
180         }
181     }
182
183     if( p_bank->pp_sub[ i_index ] != p_sub )
184     {
185         msg_Err( p_this, "subscriber not found" );
186         vlc_mutex_unlock( &p_bank->lock );
187         return;
188     }
189
190     /* Remove this subscription */
191     for( ; i_index < (p_bank->i_sub - 1); i_index++ )
192     {
193         p_bank->pp_sub[ i_index ] = p_bank->pp_sub[ i_index+1 ];
194     }
195
196     p_bank->i_sub--;
197     if( p_bank->i_sub )
198     {
199         p_bank->pp_sub = realloc( p_bank->pp_sub, p_bank->i_sub
200                                    * sizeof( msg_subscription_t* ) );
201     }
202     else
203     {
204         free( p_bank->pp_sub );
205         p_bank->pp_sub = NULL;
206     }
207
208
209     vlc_mutex_unlock( &p_bank->lock );
210 }
211
212 /*****************************************************************************
213  * __msg_*: print a message
214  *****************************************************************************
215  * These functions queue a message for later printing.
216  *****************************************************************************/
217 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
218                     const char *psz_format, ... )
219 {
220     va_list args;
221
222     va_start( args, psz_format );
223     QueueMsg( p_this, i_type, psz_module, psz_format, args );
224     va_end( args );
225 }
226
227 /* Generic functions used when variadic macros are not available. */
228 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
229     void FN_NAME( void *p_this, const char *psz_format, ... ) \
230     { \
231         va_list args; \
232         va_start( args, psz_format ); \
233         QueueMsg( (vlc_object_t *)p_this, FN_TYPE, "unknown", \
234                   psz_format, args ); \
235         va_end( args ); \
236     } \
237     struct _
238
239 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
240 DECLARE_MSG_FN( __msg_Err,  VLC_MSG_ERR );
241 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
242 DECLARE_MSG_FN( __msg_Dbg,  VLC_MSG_DBG );
243
244 /*****************************************************************************
245  * QueueMsg: add a message to a queue
246  *****************************************************************************
247  * This function provides basic functionnalities to other msg_* functions.
248  * It adds a message to a queue (after having printed all stored messages if it
249  * is full). If the message can't be converted to string in memory, it issues
250  * a warning.
251  *****************************************************************************/
252 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
253                       const char *psz_format, va_list args )
254 {
255     msg_bank_t * p_bank = &p_this->p_libvlc->msg_bank;       /* message bank */
256     char *       psz_str = NULL;                 /* formatted message string */
257     msg_item_t * p_item = NULL;                        /* pointer to message */
258     msg_item_t   item;                    /* message in case of a full queue */
259 #ifdef WIN32
260     char *       psz_temp;
261 #endif
262 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
263     int          i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
264 #endif
265
266     /*
267      * Convert message to string
268      */
269 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
270     vasprintf( &psz_str, psz_format, args );
271 #else
272     psz_str = (char*) malloc( i_size * sizeof(char) );
273 #endif
274
275     if( psz_str == NULL )
276     {
277         fprintf( stderr, "main warning: can't store message (%s): ",
278                  strerror(errno) );
279         vfprintf( stderr, psz_format, args );
280         fprintf( stderr, "\n" );
281         return;
282     }
283
284 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
285 #   ifdef WIN32
286     psz_temp = ConvertPrintfFormatString( psz_format );
287     if( !psz_temp )
288     {
289         fprintf( stderr, "main warning: couldn't print message\n" );
290         return;
291     }
292     vsnprintf( psz_str, i_size, psz_temp, args );
293     free( psz_temp );
294 #   else
295     vsnprintf( psz_str, i_size, psz_format, args );
296 #   endif
297     psz_str[ i_size - 1 ] = 0; /* Just in case */
298 #endif
299
300     /* Put message in queue */
301     vlc_mutex_lock( &p_bank->lock );
302
303     /* Check there is room in the queue for our message */
304     if( p_bank->b_overflow )
305     {
306         FlushMsg( p_bank );
307
308         if( ((p_bank->i_stop - p_bank->i_start + 1) % VLC_MSG_QSIZE) == 0 )
309         {
310             /* Still in overflow mode, print from a dummy item */
311             p_item = &item;
312         }
313         else
314         {
315             /* Pheeew, at last, there is room in the queue! */
316             p_bank->b_overflow = VLC_FALSE;
317         }
318     }
319     else if( ((p_bank->i_stop - p_bank->i_start + 2) % VLC_MSG_QSIZE) == 0 )
320     {
321         FlushMsg( p_bank );
322
323         if( ((p_bank->i_stop - p_bank->i_start + 2) % VLC_MSG_QSIZE) == 0 )
324         {
325             p_bank->b_overflow = VLC_TRUE;
326
327             /* Put the overflow message in the queue */
328             p_item = p_bank->msg + p_bank->i_stop;
329             p_bank->i_stop = (p_bank->i_stop + 1) % VLC_MSG_QSIZE;
330
331             p_item->i_type =        VLC_MSG_ERR;
332             p_item->i_object_id =   p_this->i_object_id;
333             p_item->i_object_type = p_this->i_object_type;
334             p_item->psz_module =    strdup( "message" );
335             p_item->psz_msg =       strdup( "message queue overflowed" );
336
337             PrintMsg( p_this, p_item );
338
339             /* We print from a dummy item */
340             p_item = &item;
341         }
342     }
343
344     if( !p_bank->b_overflow )
345     {
346         /* Put the message in the queue */
347         p_item = p_bank->msg + p_bank->i_stop;
348         p_bank->i_stop = (p_bank->i_stop + 1) % VLC_MSG_QSIZE;
349     }
350
351     /* Fill message information fields */
352     p_item->i_type =        i_type;
353     p_item->i_object_id =   p_this->i_object_id;
354     p_item->i_object_type = p_this->i_object_type;
355     p_item->psz_module =    strdup( psz_module );
356     p_item->psz_msg =       psz_str;
357
358     PrintMsg( p_this, p_item );
359
360     if( p_bank->b_overflow )
361     {
362         free( p_item->psz_module );
363         free( p_item->psz_msg );
364     }
365
366     vlc_mutex_unlock( &p_bank->lock );
367 }
368
369 /* following functions are local */
370
371 /*****************************************************************************
372  * FlushMsg
373  *****************************************************************************
374  * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
375  * this function does not check the lock.
376  *****************************************************************************/
377 static void FlushMsg ( msg_bank_t *p_bank )
378 {
379     int i_index, i_start, i_stop;
380
381     /* Only flush the queue if it has been properly configured */
382     if( !p_bank->b_configured )
383     {
384         return;
385     }
386
387     /* Get the maximum message index that can be freed */
388     i_stop = p_bank->i_stop;
389
390     /* Check until which value we can free messages */
391     for( i_index = 0; i_index < p_bank->i_sub; i_index++ )
392     {
393         i_start = p_bank->pp_sub[ i_index ]->i_start;
394
395         /* If this subscriber is late, we don't free messages before
396          * his i_start value, otherwise he'll miss messages */
397         if(   ( i_start < i_stop
398                && (p_bank->i_stop <= i_start || i_stop <= p_bank->i_stop) )
399            || ( i_stop < i_start
400                && (i_stop <= p_bank->i_stop && p_bank->i_stop <= i_start) ) )
401         {
402             i_stop = i_start;
403         }
404     }
405
406     /* Free message data */
407     for( i_index = p_bank->i_start;
408          i_index != i_stop;
409          i_index = (i_index+1) % VLC_MSG_QSIZE )
410     {
411         free( p_bank->msg[i_index].psz_msg );
412         free( p_bank->msg[i_index].psz_module );
413     }
414
415     /* Update the new start value */
416     p_bank->i_start = i_index;
417 }
418
419 /*****************************************************************************
420  * PrintMsg: output a message item to stderr
421  *****************************************************************************
422  * Print a message to stderr, with colour formatting if needed.
423  *****************************************************************************/
424 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
425 {
426 #   define COL(x)  "\033[" #x ";1m"
427 #   define RED     COL(31)
428 #   define GREEN   COL(32)
429 #   define YELLOW  COL(33)
430 #   define WHITE   COL(37)
431 #   define GRAY    "\033[0m"
432
433     static const char * ppsz_type[4] = { "", " error", " warning", " debug" };
434     static const char *ppsz_color[4] = { WHITE, RED, YELLOW, GRAY };
435     char *psz_object = "private";
436     int i_type = p_item->i_type;
437
438     switch( i_type )
439     {
440         case VLC_MSG_ERR:
441             if( p_this->p_libvlc->i_verbose < 0 ) return;
442             break;
443         case VLC_MSG_INFO:
444             if( p_this->p_libvlc->i_verbose < 0 ) return;
445             break;
446         case VLC_MSG_WARN:
447             if( p_this->p_libvlc->i_verbose < 1 ) return;
448             break;
449         case VLC_MSG_DBG:
450             if( p_this->p_libvlc->i_verbose < 2 ) return;
451             break;
452     }
453
454     switch( p_item->i_object_type )
455     {
456         case VLC_OBJECT_ROOT: psz_object = "root"; break;
457         case VLC_OBJECT_VLC: psz_object = "vlc"; break;
458         case VLC_OBJECT_MODULE: psz_object = "module"; break;
459         case VLC_OBJECT_INTF: psz_object = "interface"; break;
460         case VLC_OBJECT_PLAYLIST: psz_object = "playlist"; break;
461         case VLC_OBJECT_ITEM: psz_object = "item"; break;
462         case VLC_OBJECT_INPUT: psz_object = "input"; break;
463         case VLC_OBJECT_DECODER: psz_object = "decoder"; break;
464         case VLC_OBJECT_VOUT: psz_object = "video output"; break;
465         case VLC_OBJECT_AOUT: psz_object = "audio output"; break;
466         case VLC_OBJECT_SOUT: psz_object = "stream output"; break;
467     }
468
469     /* Send the message to stderr */
470     if( p_this->p_libvlc->b_color )
471     {
472         fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
473                          p_item->i_object_id, p_item->psz_module, psz_object,
474                          ppsz_type[i_type], ppsz_color[i_type],
475                          p_item->psz_msg );
476     }
477     else
478     {
479         fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
480                          p_item->psz_module, psz_object, ppsz_type[i_type],
481                          p_item->psz_msg );
482     }
483
484     fflush( stderr );
485 }
486
487 /*****************************************************************************
488  * ConvertPrintfFormatString: replace all occurrences of %ll with %I64 in the
489  *                            printf format string.
490  *****************************************************************************
491  * Win32 doesn't recognize the "%ll" format in a printf string, so we have
492  * to convert this string to something that win32 can handle.
493  * This is a REALLY UGLY HACK which won't even work in every situation,
494  * but hey I don't want to put an ifdef WIN32 each time I use printf with
495  * a "long long" type!!!
496  * By the way, if we don't do this we can sometimes end up with segfaults.
497  *****************************************************************************/
498 #if defined( WIN32 )
499 static char *ConvertPrintfFormatString( const char *psz_format )
500 {
501   int i, i_counter=0, i_pos=0;
502   char *psz_dest;
503
504   /* We first need to check how many occurences of %ll there are in the
505    * psz_format string. Once we'll know that we'll be able to malloc the
506    * destination string */
507
508   if( strlen( psz_format ) <= 3 )
509       return strdup( psz_format );
510
511   for( i=0; i <= (strlen(psz_format) - 3); i++ )
512   {
513       if( !strncmp( (char *)(psz_format + i), "%ll", 3 ) )
514       {
515           i_counter++;
516       }
517   }
518
519   /* malloc the destination string */
520   psz_dest = malloc( strlen(psz_format) + i_counter + 1 );
521   if( psz_dest == NULL )
522   {
523       fprintf( stderr, "main warning: ConvertPrintfFormatString failed\n" );
524       return NULL;
525   }
526
527   /* Now build the modified string */
528   i_counter = 0;
529   for( i=0; i <= (strlen(psz_format) - 3); i++ )
530   {
531       if( !strncmp( (char *)(psz_format + i), "%ll", 3 ) )
532       {
533           memcpy( psz_dest+i_pos+i_counter, psz_format+i_pos, i-i_pos+1);
534           *(psz_dest+i+i_counter+1)='I';
535           *(psz_dest+i+i_counter+2)='6';
536           *(psz_dest+i+i_counter+3)='4';
537           i_pos = i+3;
538           i_counter++;
539       }
540   }
541   strcpy( psz_dest+i_pos+i_counter, psz_format+i_pos );
542
543   return psz_dest;
544 }
545 #endif