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