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