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