]> git.sesse.net Git - vlc/blob - src/misc/messages.c
Use object address for logging - fixes uniqueness problems
[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 /**
91  * Initialize messages queues
92  * This function initializes all message queues
93  */
94 void msg_Create (libvlc_int_t *p_libvlc)
95 {
96     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
97     vlc_mutex_init( &priv->msg_bank.lock );
98     vlc_mutex_init( &QUEUE.lock );
99     vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
100     priv->msg_all_objects_enabled = true;
101
102     QUEUE.b_overflow = false;
103     QUEUE.i_start = 0;
104     QUEUE.i_stop = 0;
105     QUEUE.i_sub = 0;
106     QUEUE.pp_sub = 0;
107
108 #ifdef UNDER_CE
109     QUEUE.logfile =
110         CreateFile( L"vlc-log.txt", GENERIC_WRITE,
111                     FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
112                     CREATE_ALWAYS, 0, NULL );
113     SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
114 #endif
115
116     vlc_mutex_t *lock = var_AcquireMutex( "msg-stack" );
117     if( banks++ == 0 )
118         vlc_threadvar_create( &msg_context, NULL );
119     vlc_mutex_unlock( lock );
120 }
121
122 /**
123  * Flush all message queues
124  */
125 void msg_Flush (libvlc_int_t *p_libvlc)
126 {
127     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
128     vlc_mutex_lock( &QUEUE.lock );
129     FlushMsg( &QUEUE );
130     vlc_mutex_unlock( &QUEUE.lock );
131 }
132
133
134 /**
135  * Object Printing selection
136  */
137 static void const * kObjectPrintingEnabled = (void *) 1;
138 static void const * kObjectPrintingDisabled = (void *) -1;
139
140 void __msg_EnableObjectPrinting (vlc_object_t *p_this, char * psz_object)
141 {
142     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
143     vlc_mutex_lock( &QUEUE.lock );
144     if( !strcmp(psz_object, "all") )
145         priv->msg_all_objects_enabled = true;
146     else
147         vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
148     vlc_mutex_unlock( &QUEUE.lock );
149 }
150
151 void __msg_DisableObjectPrinting (vlc_object_t *p_this, char * psz_object)
152 {
153     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
154     vlc_mutex_lock( &QUEUE.lock );
155     if( !strcmp(psz_object, "all") )
156         priv->msg_all_objects_enabled = false;
157     else
158         vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
159     vlc_mutex_unlock( &QUEUE.lock );
160 }
161
162 /**
163  * Destroy the message queues
164  *
165  * This functions prints all messages remaining in the queues,
166  * then frees all the allocated ressources
167  * No other messages interface functions should be called after this one.
168  */
169 void msg_Destroy (libvlc_int_t *p_libvlc)
170 {
171     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
172
173     if( QUEUE.i_sub )
174         msg_Err( p_libvlc, "stale interface subscribers" );
175
176     FlushMsg( &QUEUE );
177
178     vlc_mutex_t *lock = var_AcquireMutex( "msg-stack" );
179     if( --banks == 0 )
180         vlc_threadvar_delete( &msg_context );
181     vlc_mutex_unlock( lock );
182
183 #ifdef UNDER_CE
184     CloseHandle( QUEUE.logfile );
185 #endif
186
187     vlc_dictionary_clear( &priv->msg_enabled_objects );
188
189     /* Destroy lock */
190     vlc_mutex_destroy( &QUEUE.lock );
191     vlc_mutex_destroy( &priv->msg_bank.lock);
192 }
193
194 /**
195  * Subscribe to a message queue.
196  */
197 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
198 {
199     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
200     msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
201
202     if (p_sub == NULL)
203         return NULL;
204
205     LOCK_BANK;
206     vlc_mutex_lock( &QUEUE.lock );
207
208     TAB_APPEND( QUEUE.i_sub, QUEUE.pp_sub, p_sub );
209
210     p_sub->i_start = QUEUE.i_start;
211     p_sub->pi_stop = &QUEUE.i_stop;
212     p_sub->p_msg   = QUEUE.msg;
213     p_sub->p_lock  = &QUEUE.lock;
214
215     vlc_mutex_unlock( &QUEUE.lock );
216     UNLOCK_BANK;
217
218     return p_sub;
219 }
220
221 /**
222  * Unsubscribe from a message queue.
223  */
224 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
225 {
226     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
227
228     LOCK_BANK;
229     vlc_mutex_lock( &QUEUE.lock );
230     for( int j = 0 ; j< QUEUE.i_sub ; j++ )
231     {
232         if( QUEUE.pp_sub[j] == p_sub )
233         {
234             REMOVE_ELEM( QUEUE.pp_sub, QUEUE.i_sub, j );
235             free( p_sub );
236         }
237     }
238     vlc_mutex_unlock( &QUEUE.lock );
239     UNLOCK_BANK;
240 }
241
242 /*****************************************************************************
243  * __msg_*: print a message
244  *****************************************************************************
245  * These functions queue a message for later printing.
246  *****************************************************************************/
247 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
248                     const char *psz_format, ... )
249 {
250     va_list args;
251
252     va_start( args, psz_format );
253     QueueMsg( p_this, i_type, psz_module, psz_format, args );
254     va_end( args );
255 }
256
257 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
258                       const char *psz_format, va_list args )
259 {
260     QueueMsg( p_this, i_type, psz_module, psz_format, args );
261 }
262
263 /* Generic functions used when variadic macros are not available. */
264 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
265     void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
266     { \
267         va_list args; \
268         va_start( args, psz_format ); \
269         QueueMsg( p_this, FN_TYPE, "unknown", psz_format, args ); \
270         va_end( args ); \
271     } \
272     struct _
273 /**
274  * Output an informational message.
275  * \note Do not use this for debug messages
276  * \see input_AddInfo
277  */
278 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
279 /**
280  * Output an error message.
281  */
282 DECLARE_MSG_FN( __msg_Err,  VLC_MSG_ERR );
283 /**
284  * Output a waring message
285  */
286 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
287 /**
288  * Output a debug message
289  */
290 DECLARE_MSG_FN( __msg_Dbg,  VLC_MSG_DBG );
291
292 /**
293  * Add a message to a queue
294  *
295  * This function provides basic functionnalities to other msg_* functions.
296  * It adds a message to a queue (after having printed all stored messages if it
297  * is full). If the message can't be converted to string in memory, it issues
298  * a warning.
299  */
300 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
301                       const char *psz_format, va_list _args )
302 {
303     assert (p_this);
304     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
305     int         i_header_size;             /* Size of the additionnal header */
306     vlc_object_t *p_obj;
307     char *       psz_str = NULL;                 /* formatted message string */
308     char *       psz_header = NULL;
309     va_list      args;
310     msg_item_t * p_item = NULL;                        /* pointer to message */
311     msg_item_t   item;                    /* message in case of a full queue */
312     msg_queue_t *p_queue;
313
314 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
315     int          i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
316 #endif
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         int canc = vlc_savecancel (); /* Do not print half of a message... */
391 #ifdef __GLIBC__
392         fprintf( stderr, "main warning: can't store message (%m): " );
393 #else
394         char psz_err[1001];
395 #ifndef WIN32
396         /* we're not using GLIBC, so we are sure that the error description
397          * will be stored in the buffer we provide to strerror_r() */
398         strerror_r( errno, psz_err, 1001 );
399 #else
400         strncpy( psz_err, strerror( errno ), 1001 );
401 #endif
402         psz_err[1000] = '\0';
403         fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
404 #endif
405         vlc_va_copy( args, _args );
406         /* We should use utf8_vfprintf - but it calls malloc()... */
407         vfprintf( stderr, psz_format, args );
408         va_end( args );
409         fputs( "\n", stderr );
410         vlc_restorecancel (canc);
411         return;
412     }
413
414     i_header_size = 0;
415     p_obj = p_this;
416     while( p_obj != NULL )
417     {
418         char *psz_old = NULL;
419         if( p_obj->psz_header )
420         {
421             i_header_size += strlen( p_obj->psz_header ) + 4;
422             if( psz_header )
423             {
424                 psz_old = strdup( psz_header );
425                 psz_header = (char*)realloc( psz_header, i_header_size );
426                 snprintf( psz_header, i_header_size , "[%s] %s",
427                           p_obj->psz_header, psz_old );
428             }
429             else
430             {
431                 psz_header = (char *)malloc( i_header_size );
432                 snprintf( psz_header, i_header_size, "[%s]",
433                           p_obj->psz_header );
434             }
435         }
436         free( psz_old );
437         p_obj = p_obj->p_parent;
438     }
439
440 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
441     vlc_va_copy( args, _args );
442     vsnprintf( psz_str, i_size, psz_format, args );
443     va_end( args );
444     psz_str[ i_size - 1 ] = 0; /* Just in case */
445 #endif
446
447     LOCK_BANK;
448     p_queue = &QUEUE;
449     vlc_mutex_lock( &p_queue->lock );
450
451     /* Check there is room in the queue for our message */
452     if( p_queue->b_overflow )
453     {
454         FlushMsg( p_queue );
455
456         if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
457         {
458             /* Still in overflow mode, print from a dummy item */
459             p_item = &item;
460         }
461         else
462         {
463             /* Pheeew, at last, there is room in the queue! */
464             p_queue->b_overflow = false;
465         }
466     }
467     else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
468     {
469         FlushMsg( p_queue );
470
471         if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
472         {
473             p_queue->b_overflow = true;
474
475             /* Put the overflow message in the queue */
476             p_item = p_queue->msg + p_queue->i_stop;
477             p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
478
479             p_item->i_type =        VLC_MSG_WARN;
480             p_item->i_object_id =   (uintptr_t)p_this;
481             p_item->psz_object_type = p_this->psz_object_type;
482             p_item->psz_module =    strdup( "message" );
483             p_item->psz_msg =       strdup( "message queue overflowed" );
484             p_item->psz_header =    NULL;
485
486             PrintMsg( p_this, p_item );
487             /* We print from a dummy item */
488             p_item = &item;
489         }
490     }
491
492     if( !p_queue->b_overflow )
493     {
494         /* Put the message in the queue */
495         p_item = p_queue->msg + p_queue->i_stop;
496         p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
497     }
498
499     /* Fill message information fields */
500     p_item->i_type =        i_type;
501     p_item->i_object_id =   (uintptr_t)p_this;
502     p_item->psz_object_type = p_this->psz_object_type;
503     p_item->psz_module =    strdup( psz_module );
504     p_item->psz_msg =       psz_str;
505     p_item->psz_header =    psz_header;
506
507     PrintMsg( p_this, p_item );
508
509     if( p_queue->b_overflow )
510     {
511         free( p_item->psz_module );
512         free( p_item->psz_msg );
513         free( p_item->psz_header );
514     }
515
516     vlc_mutex_unlock ( &p_queue->lock );
517     UNLOCK_BANK;
518 }
519
520 /* following functions are local */
521
522 /*****************************************************************************
523  * FlushMsg
524  *****************************************************************************
525  * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
526  * this function does not check the lock.
527  *****************************************************************************/
528 static void FlushMsg ( msg_queue_t *p_queue )
529 {
530     int i_index, i_start, i_stop;
531
532     /* Get the maximum message index that can be freed */
533     i_stop = p_queue->i_stop;
534
535     /* Check until which value we can free messages */
536     for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
537     {
538         i_start = p_queue->pp_sub[ i_index ]->i_start;
539
540         /* If this subscriber is late, we don't free messages before
541          * his i_start value, otherwise he'll miss messages */
542         if(   ( i_start < i_stop
543                && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
544            || ( i_stop < i_start
545                && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
546         {
547             i_stop = i_start;
548         }
549     }
550
551     /* Free message data */
552     for( i_index = p_queue->i_start;
553          i_index != i_stop;
554          i_index = (i_index+1) % VLC_MSG_QSIZE )
555     {
556         free( p_queue->msg[i_index].psz_msg );
557         free( p_queue->msg[i_index].psz_module );
558         free( p_queue->msg[i_index].psz_header );
559     }
560
561     /* Update the new start value */
562     p_queue->i_start = i_index;
563 }
564
565 /*****************************************************************************
566  * PrintMsg: output a standard message item to stderr
567  *****************************************************************************
568  * Print a message to stderr, with colour formatting if needed.
569  *****************************************************************************/
570 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
571 {
572 #   define COL(x)  "\033[" #x ";1m"
573 #   define RED     COL(31)
574 #   define GREEN   COL(32)
575 #   define YELLOW  COL(33)
576 #   define WHITE   COL(0)
577 #   define GRAY    "\033[0m"
578
579 #ifdef UNDER_CE
580     int i_dummy;
581 #endif
582     static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
583     static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
584     const char *psz_object;
585     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
586     int i_type = p_item->i_type;
587
588     switch( i_type )
589     {
590         case VLC_MSG_ERR:
591             if( priv->i_verbose < 0 ) return;
592             break;
593         case VLC_MSG_INFO:
594             if( priv->i_verbose < 0 ) return;
595             break;
596         case VLC_MSG_WARN:
597             if( priv->i_verbose < 1 ) return;
598             break;
599         case VLC_MSG_DBG:
600             if( priv->i_verbose < 2 ) return;
601             break;
602     }
603
604     psz_object = p_item->psz_object_type;
605     void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
606                                                p_item->psz_module );
607     if( val == kObjectPrintingDisabled )
608         return;
609     if( val == kObjectPrintingEnabled )
610         /* Allowed */;
611     else
612     {
613         val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
614                                             psz_object );
615         if( val == kObjectPrintingDisabled )
616             return;
617         if( val == kObjectPrintingEnabled )
618             /* Allowed */;
619         else if( !priv->msg_all_objects_enabled )
620             return;
621     }
622
623     int canc = vlc_savecancel ();
624 #ifdef UNDER_CE
625 #   define CE_WRITE(str) WriteFile( QUEUE.logfile, \
626                                     str, strlen(str), &i_dummy, NULL );
627     CE_WRITE( p_item->psz_module );
628     CE_WRITE( " " );
629     CE_WRITE( psz_object );
630     CE_WRITE( ppsz_type[i_type] );
631     CE_WRITE( ": " );
632     CE_WRITE( p_item->psz_msg );
633     CE_WRITE( "\r\n" );
634     FlushFileBuffers( QUEUE.logfile );
635
636 #else
637     /* Send the message to stderr */
638     utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
639                   priv->b_color ? GREEN : "",
640                   (void *)p_item->i_object_id,
641                   priv->b_color ? GRAY : "",
642                   p_item->psz_header ? p_item->psz_header : "",
643                   p_item->psz_header ? " " : "",
644                   p_item->psz_module, psz_object,
645                   ppsz_type[i_type],
646                   priv->b_color ? ppsz_color[i_type] : "",
647                   p_item->psz_msg,
648                   priv->b_color ? GRAY : "" );
649
650 #   if defined(WIN32)
651     fflush( stderr );
652 #   endif
653     vlc_restorecancel (canc);
654 #endif
655 }
656
657 static msg_context_t* GetContext(void)
658 {
659     msg_context_t *p_ctx = vlc_threadvar_get( &msg_context );
660     if( p_ctx == NULL )
661     {
662         MALLOC_NULL( p_ctx, msg_context_t );
663         p_ctx->psz_message = NULL;
664         vlc_threadvar_set( &msg_context, p_ctx );
665     }
666     return p_ctx;
667 }
668
669 void msg_StackDestroy (void *data)
670 {
671     msg_context_t *p_ctx = data;
672
673     free (p_ctx->psz_message);
674     free (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
682     if( p_ctx == NULL )
683         return;
684     free( p_ctx->psz_message );
685
686     va_start( ap, psz_message );
687     if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
688         p_ctx->psz_message = NULL;
689     va_end( ap );
690
691     p_ctx->i_code = i_code;
692 }
693
694 void msg_StackAdd( const char *psz_message, ... )
695 {
696     char *psz_tmp;
697     va_list ap;
698     msg_context_t *p_ctx = GetContext();
699
700     if( p_ctx == NULL )
701         return;
702
703     va_start( ap, psz_message );
704     if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
705         psz_tmp = NULL;
706     va_end( ap );
707
708     if( !p_ctx->psz_message )
709         p_ctx->psz_message = psz_tmp;
710     else
711     {
712         char *psz_new;
713         if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
714             psz_new = NULL;
715
716         free( p_ctx->psz_message );
717         p_ctx->psz_message = psz_new;
718         free( psz_tmp );
719     }
720 }
721
722 const char* msg_StackMsg( void )
723 {
724     msg_context_t *p_ctx = GetContext();
725     assert( p_ctx );
726     return p_ctx->psz_message;
727 }