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