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