]> git.sesse.net Git - vlc/blob - src/misc/messages.c
Use static mutexes
[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_common.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 #include "../libvlc.h"
57
58 typedef struct
59 {
60     int i_code;
61     char * psz_message;
62 } msg_context_t;
63
64 static vlc_threadvar_t msg_context;
65 static uintptr_t banks = 0;
66
67 /*****************************************************************************
68  * Local macros
69  *****************************************************************************/
70 #if defined(HAVE_VA_COPY)
71 #   define vlc_va_copy(dest,src) va_copy(dest,src)
72 #elif defined(HAVE___VA_COPY)
73 #   define vlc_va_copy(dest,src) __va_copy(dest,src)
74 #else
75 #   define vlc_va_copy(dest,src) (dest)=(src)
76 #endif
77
78 #define QUEUE priv->msg_bank.queue
79 #define LOCK_BANK vlc_mutex_lock( &priv->msg_bank.lock );
80 #define UNLOCK_BANK vlc_mutex_unlock( &priv->msg_bank.lock );
81
82 /*****************************************************************************
83  * Local prototypes
84  *****************************************************************************/
85 static void QueueMsg ( vlc_object_t *, int, const char *,
86                        const char *, va_list );
87 static void FlushMsg ( msg_queue_t * );
88 static void PrintMsg ( vlc_object_t *, msg_item_t * );
89
90 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
91
92 /**
93  * Initialize messages queues
94  * This function initializes all message queues
95  */
96 void msg_Create (libvlc_int_t *p_libvlc)
97 {
98     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
99     vlc_mutex_init( &priv->msg_bank.lock );
100     vlc_mutex_init( &QUEUE.lock );
101     vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
102     priv->msg_all_objects_enabled = true;
103
104     QUEUE.b_overflow = false;
105     QUEUE.i_start = 0;
106     QUEUE.i_stop = 0;
107     QUEUE.i_sub = 0;
108     QUEUE.pp_sub = 0;
109
110 #ifdef UNDER_CE
111     QUEUE.logfile =
112         CreateFile( L"vlc-log.txt", GENERIC_WRITE,
113                     FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
114                     CREATE_ALWAYS, 0, NULL );
115     SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
116 #endif
117
118     vlc_mutex_lock( &msg_stack_lock );
119     if( banks++ == 0 )
120         vlc_threadvar_create( &msg_context, NULL );
121     vlc_mutex_unlock( &msg_stack_lock );
122 }
123
124 /**
125  * Flush all message queues
126  */
127 void msg_Flush (libvlc_int_t *p_libvlc)
128 {
129     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
130     vlc_mutex_lock( &QUEUE.lock );
131     FlushMsg( &QUEUE );
132     vlc_mutex_unlock( &QUEUE.lock );
133 }
134
135
136 /**
137  * Object Printing selection
138  */
139 static void const * kObjectPrintingEnabled = (void *) 1;
140 static void const * kObjectPrintingDisabled = (void *) -1;
141
142 void __msg_EnableObjectPrinting (vlc_object_t *p_this, char * psz_object)
143 {
144     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
145     vlc_mutex_lock( &QUEUE.lock );
146     if( !strcmp(psz_object, "all") )
147         priv->msg_all_objects_enabled = true;
148     else
149         vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
150     vlc_mutex_unlock( &QUEUE.lock );
151 }
152
153 void __msg_DisableObjectPrinting (vlc_object_t *p_this, char * psz_object)
154 {
155     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
156     vlc_mutex_lock( &QUEUE.lock );
157     if( !strcmp(psz_object, "all") )
158         priv->msg_all_objects_enabled = false;
159     else
160         vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
161     vlc_mutex_unlock( &QUEUE.lock );
162 }
163
164 /**
165  * Destroy the message queues
166  *
167  * This functions prints all messages remaining in the queues,
168  * then frees all the allocated ressources
169  * No other messages interface functions should be called after this one.
170  */
171 void msg_Destroy (libvlc_int_t *p_libvlc)
172 {
173     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
174
175     if( QUEUE.i_sub )
176         msg_Err( p_libvlc, "stale interface subscribers" );
177
178     FlushMsg( &QUEUE );
179
180     vlc_mutex_lock( &msg_stack_lock );
181     if( --banks == 0 )
182         vlc_threadvar_delete( &msg_context );
183     vlc_mutex_unlock( &msg_stack_lock );
184
185 #ifdef UNDER_CE
186     CloseHandle( QUEUE.logfile );
187 #endif
188
189     vlc_dictionary_clear( &priv->msg_enabled_objects );
190
191     /* Destroy lock */
192     vlc_mutex_destroy( &QUEUE.lock );
193     vlc_mutex_destroy( &priv->msg_bank.lock);
194 }
195
196 /**
197  * Subscribe to a message queue.
198  */
199 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
200 {
201     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
202     msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
203
204     if (p_sub == NULL)
205         return NULL;
206
207     LOCK_BANK;
208     vlc_mutex_lock( &QUEUE.lock );
209
210     TAB_APPEND( QUEUE.i_sub, QUEUE.pp_sub, p_sub );
211
212     p_sub->i_start = QUEUE.i_start;
213     p_sub->pi_stop = &QUEUE.i_stop;
214     p_sub->p_msg   = QUEUE.msg;
215     p_sub->p_lock  = &QUEUE.lock;
216
217     vlc_mutex_unlock( &QUEUE.lock );
218     UNLOCK_BANK;
219
220     return p_sub;
221 }
222
223 /**
224  * Unsubscribe from a message queue.
225  */
226 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
227 {
228     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
229
230     LOCK_BANK;
231     vlc_mutex_lock( &QUEUE.lock );
232     for( int j = 0 ; j< QUEUE.i_sub ; j++ )
233     {
234         if( QUEUE.pp_sub[j] == p_sub )
235         {
236             REMOVE_ELEM( QUEUE.pp_sub, QUEUE.i_sub, j );
237             free( p_sub );
238         }
239     }
240     vlc_mutex_unlock( &QUEUE.lock );
241     UNLOCK_BANK;
242 }
243
244 /*****************************************************************************
245  * __msg_*: print a message
246  *****************************************************************************
247  * These functions queue a message for later printing.
248  *****************************************************************************/
249 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
250                     const char *psz_format, ... )
251 {
252     va_list args;
253
254     va_start( args, psz_format );
255     QueueMsg( p_this, i_type, psz_module, psz_format, args );
256     va_end( args );
257 }
258
259 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
260                       const char *psz_format, va_list args )
261 {
262     QueueMsg( p_this, i_type, psz_module, psz_format, args );
263 }
264
265 /* Generic functions used when variadic macros are not available. */
266 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
267     void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
268     { \
269         va_list args; \
270         va_start( args, psz_format ); \
271         QueueMsg( p_this, FN_TYPE, "unknown", psz_format, args ); \
272         va_end( args ); \
273     } \
274     struct _
275 /**
276  * Output an informational message.
277  * \note Do not use this for debug messages
278  * \see input_AddInfo
279  */
280 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
281 /**
282  * Output an error message.
283  */
284 DECLARE_MSG_FN( __msg_Err,  VLC_MSG_ERR );
285 /**
286  * Output a waring message
287  */
288 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
289 /**
290  * Output a debug message
291  */
292 DECLARE_MSG_FN( __msg_Dbg,  VLC_MSG_DBG );
293
294 /**
295  * Add a message to a queue
296  *
297  * This function provides basic functionnalities to other msg_* functions.
298  * It adds a message to a queue (after having printed all stored messages if it
299  * is full). If the message can't be converted to string in memory, it issues
300  * a warning.
301  */
302 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
303                       const char *psz_format, va_list _args )
304 {
305     assert (p_this);
306     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
307     int         i_header_size;             /* Size of the additionnal header */
308     vlc_object_t *p_obj;
309     char *       psz_str = NULL;                 /* formatted message string */
310     char *       psz_header = NULL;
311     va_list      args;
312     msg_item_t * p_item = NULL;                        /* pointer to message */
313     msg_item_t   item;                    /* message in case of a full queue */
314     msg_queue_t *p_queue;
315
316 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
317     int          i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
318 #endif
319
320     if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
321         (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
322         return;
323
324 #ifndef __GLIBC__
325     /* Expand %m to strerror(errno) - only once */
326     char buf[strlen( psz_format ) + 2001], *ptr;
327     strcpy( buf, psz_format );
328     ptr = (char*)buf;
329     psz_format = (const char*) buf;
330
331     for( ;; )
332     {
333         ptr = strchr( ptr, '%' );
334         if( ptr == NULL )
335             break;
336
337         if( ptr[1] == 'm' )
338         {
339             char errbuf[2001];
340             size_t errlen;
341
342 #ifndef WIN32
343             strerror_r( errno, errbuf, 1001 );
344 #else
345             int sockerr = WSAGetLastError( );
346             if( sockerr )
347             {
348                 strncpy( errbuf, net_strerror( sockerr ), 1001 );
349                 WSASetLastError( sockerr );
350             }
351             if ((sockerr == 0)
352              || (strcmp ("Unknown network stack error", errbuf) == 0))
353                 strncpy( errbuf, strerror( errno ), 1001 );
354 #endif
355             errbuf[1000] = 0;
356
357             /* Escape '%' from the error string */
358             for( char *percent = strchr( errbuf, '%' );
359                  percent != NULL;
360                  percent = strchr( percent + 2, '%' ) )
361             {
362                 memmove( percent + 1, percent, strlen( percent ) + 1 );
363             }
364
365             errlen = strlen( errbuf );
366             memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
367             memcpy( ptr, errbuf, errlen );
368             break; /* Only once, so we don't overflow */
369         }
370
371         /* Looks for conversion specifier... */
372         do
373             ptr++;
374         while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
375         if( *ptr )
376             ptr++; /* ...and skip it */
377     }
378 #endif
379
380     /* Convert message to string  */
381 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
382     vlc_va_copy( args, _args );
383     if( vasprintf( &psz_str, psz_format, args ) == -1 )
384         psz_str = NULL;
385     va_end( args );
386 #else
387     psz_str = (char*) malloc( i_size );
388 #endif
389
390     if( psz_str == NULL )
391     {
392         int canc = vlc_savecancel (); /* Do not print half of a message... */
393 #ifdef __GLIBC__
394         fprintf( stderr, "main warning: can't store message (%m): " );
395 #else
396         char psz_err[1001];
397 #ifndef WIN32
398         /* we're not using GLIBC, so we are sure that the error description
399          * will be stored in the buffer we provide to strerror_r() */
400         strerror_r( errno, psz_err, 1001 );
401 #else
402         strncpy( psz_err, strerror( errno ), 1001 );
403 #endif
404         psz_err[1000] = '\0';
405         fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
406 #endif
407         vlc_va_copy( args, _args );
408         /* We should use utf8_vfprintf - but it calls malloc()... */
409         vfprintf( stderr, psz_format, args );
410         va_end( args );
411         fputs( "\n", stderr );
412         vlc_restorecancel (canc);
413         return;
414     }
415
416     i_header_size = 0;
417     p_obj = p_this;
418     while( p_obj != NULL )
419     {
420         char *psz_old = NULL;
421         if( p_obj->psz_header )
422         {
423             i_header_size += strlen( p_obj->psz_header ) + 4;
424             if( psz_header )
425             {
426                 psz_old = strdup( psz_header );
427                 psz_header = (char*)realloc( psz_header, i_header_size );
428                 snprintf( psz_header, i_header_size , "[%s] %s",
429                           p_obj->psz_header, psz_old );
430             }
431             else
432             {
433                 psz_header = (char *)malloc( i_header_size );
434                 snprintf( psz_header, i_header_size, "[%s]",
435                           p_obj->psz_header );
436             }
437         }
438         free( psz_old );
439         p_obj = p_obj->p_parent;
440     }
441
442 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
443     vlc_va_copy( args, _args );
444     vsnprintf( psz_str, i_size, psz_format, args );
445     va_end( args );
446     psz_str[ i_size - 1 ] = 0; /* Just in case */
447 #endif
448
449     LOCK_BANK;
450     p_queue = &QUEUE;
451     vlc_mutex_lock( &p_queue->lock );
452
453     /* Check there is room in the queue for our message */
454     if( p_queue->b_overflow )
455     {
456         FlushMsg( p_queue );
457
458         if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
459         {
460             /* Still in overflow mode, print from a dummy item */
461             p_item = &item;
462         }
463         else
464         {
465             /* Pheeew, at last, there is room in the queue! */
466             p_queue->b_overflow = false;
467         }
468     }
469     else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
470     {
471         FlushMsg( p_queue );
472
473         if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
474         {
475             p_queue->b_overflow = true;
476
477             /* Put the overflow message in the queue */
478             p_item = p_queue->msg + p_queue->i_stop;
479             p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
480
481             p_item->i_type =        VLC_MSG_WARN;
482             p_item->i_object_id =   (uintptr_t)p_this;
483             p_item->psz_object_type = p_this->psz_object_type;
484             p_item->psz_module =    strdup( "message" );
485             p_item->psz_msg =       strdup( "message queue overflowed" );
486             p_item->psz_header =    NULL;
487
488             PrintMsg( p_this, p_item );
489             /* We print from a dummy item */
490             p_item = &item;
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 =   (uintptr_t)p_this;
504     p_item->psz_object_type = p_this->psz_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     PrintMsg( p_this, p_item );
510
511     if( p_queue->b_overflow )
512     {
513         free( p_item->psz_module );
514         free( p_item->psz_msg );
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         free( p_queue->msg[i_index].psz_msg );
559         free( p_queue->msg[i_index].psz_module );
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][9] = { "", " error", " warning", " debug" };
585     static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
586     const char *psz_object;
587     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
588     int i_type = p_item->i_type;
589
590     switch( i_type )
591     {
592         case VLC_MSG_ERR:
593             if( priv->i_verbose < 0 ) return;
594             break;
595         case VLC_MSG_INFO:
596             if( priv->i_verbose < 0 ) return;
597             break;
598         case VLC_MSG_WARN:
599             if( priv->i_verbose < 1 ) return;
600             break;
601         case VLC_MSG_DBG:
602             if( priv->i_verbose < 2 ) return;
603             break;
604     }
605
606     psz_object = p_item->psz_object_type;
607     void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
608                                                p_item->psz_module );
609     if( val == kObjectPrintingDisabled )
610         return;
611     if( val == kObjectPrintingEnabled )
612         /* Allowed */;
613     else
614     {
615         val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
616                                             psz_object );
617         if( val == kObjectPrintingDisabled )
618             return;
619         if( val == kObjectPrintingEnabled )
620             /* Allowed */;
621         else if( !priv->msg_all_objects_enabled )
622             return;
623     }
624
625     int canc = vlc_savecancel ();
626 #ifdef UNDER_CE
627 #   define CE_WRITE(str) WriteFile( QUEUE.logfile, \
628                                     str, strlen(str), &i_dummy, NULL );
629     CE_WRITE( p_item->psz_module );
630     CE_WRITE( " " );
631     CE_WRITE( psz_object );
632     CE_WRITE( ppsz_type[i_type] );
633     CE_WRITE( ": " );
634     CE_WRITE( p_item->psz_msg );
635     CE_WRITE( "\r\n" );
636     FlushFileBuffers( QUEUE.logfile );
637
638 #else
639     /* Send the message to stderr */
640     utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
641                   priv->b_color ? GREEN : "",
642                   (void *)p_item->i_object_id,
643                   priv->b_color ? GRAY : "",
644                   p_item->psz_header ? p_item->psz_header : "",
645                   p_item->psz_header ? " " : "",
646                   p_item->psz_module, psz_object,
647                   ppsz_type[i_type],
648                   priv->b_color ? ppsz_color[i_type] : "",
649                   p_item->psz_msg,
650                   priv->b_color ? GRAY : "" );
651
652 #   if defined(WIN32)
653     fflush( stderr );
654 #   endif
655     vlc_restorecancel (canc);
656 #endif
657 }
658
659 static msg_context_t* GetContext(void)
660 {
661     msg_context_t *p_ctx = vlc_threadvar_get( &msg_context );
662     if( p_ctx == NULL )
663     {
664         MALLOC_NULL( p_ctx, msg_context_t );
665         p_ctx->psz_message = NULL;
666         vlc_threadvar_set( &msg_context, p_ctx );
667     }
668     return p_ctx;
669 }
670
671 void msg_StackDestroy (void *data)
672 {
673     msg_context_t *p_ctx = data;
674
675     free (p_ctx->psz_message);
676     free (p_ctx);
677 }
678
679 void msg_StackSet( int i_code, const char *psz_message, ... )
680 {
681     va_list ap;
682     msg_context_t *p_ctx = GetContext();
683
684     if( p_ctx == NULL )
685         return;
686     free( p_ctx->psz_message );
687
688     va_start( ap, psz_message );
689     if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
690         p_ctx->psz_message = NULL;
691     va_end( ap );
692
693     p_ctx->i_code = i_code;
694 }
695
696 void msg_StackAdd( const char *psz_message, ... )
697 {
698     char *psz_tmp;
699     va_list ap;
700     msg_context_t *p_ctx = GetContext();
701
702     if( p_ctx == NULL )
703         return;
704
705     va_start( ap, psz_message );
706     if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
707         psz_tmp = NULL;
708     va_end( ap );
709
710     if( !p_ctx->psz_message )
711         p_ctx->psz_message = psz_tmp;
712     else
713     {
714         char *psz_new;
715         if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
716             psz_new = NULL;
717
718         free( p_ctx->psz_message );
719         p_ctx->psz_message = psz_new;
720         free( psz_tmp );
721     }
722 }
723
724 const char* msg_StackMsg( void )
725 {
726     msg_context_t *p_ctx = GetContext();
727     assert( p_ctx );
728     return p_ctx->psz_message;
729 }