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