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