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