messages.c 17.7 KB
Newer Older
1
/*****************************************************************************
2
3
 * messages.c: messages interface
 * 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
 *****************************************************************************
Jean-Paul Saman's avatar
Jean-Paul Saman committed
6
 * Copyright (C) 1998-2005 the VideoLAN team
zorglub's avatar
zorglub committed
7
 * $Id$
8
 *
Sam Hocevar's avatar
   
Sam Hocevar committed
9
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10
 *          Samuel Hocevar <sam@zoy.org>
11
12
13
14
15
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
16
 *
17
18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
21
 *
22
23
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
26

27
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
28
 * Preamble
29
 *****************************************************************************/
30

31
32
33
34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

35
#include <vlc_common.h>
36

37
#include <stdarg.h>                                       /* va_list for BSD */
Sam Hocevar's avatar
   
Sam Hocevar committed
38

39
40
41
42
#ifdef HAVE_FCNTL_H
#   include <fcntl.h>                  /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
#endif

43
#include <errno.h>                                                  /* errno */
44

45
46
47
48
#ifdef WIN32
#   include <vlc_network.h>          /* 'net_strerror' and 'WSAGetLastError' */
#endif

Sam Hocevar's avatar
   
Sam Hocevar committed
49
#ifdef HAVE_UNISTD_H
50
#   include <unistd.h>                                   /* close(), write() */
Sam Hocevar's avatar
   
Sam Hocevar committed
51
#endif
Michel Kaempf's avatar
Michel Kaempf committed
52

53
54
#include <assert.h>

zorglub's avatar
zorglub committed
55
#include <vlc_charset.h>
56
#include "../libvlc.h"
57

58
59
60
61
62
63
typedef struct
{
    int i_code;
    char * psz_message;
} msg_context_t;

64
65
66
67
68
69
70
static void cleanup_msg_context (void *data)
{
    msg_context_t *ctx = data;
    free (ctx->psz_message);
    free (ctx);
}

71
72
73
static vlc_threadvar_t msg_context;
static uintptr_t banks = 0;

74
75
76
77
78
79
80
81
82
83
84
/*****************************************************************************
 * Local macros
 *****************************************************************************/
#if defined(HAVE_VA_COPY)
#   define vlc_va_copy(dest,src) va_copy(dest,src)
#elif defined(HAVE___VA_COPY)
#   define vlc_va_copy(dest,src) __va_copy(dest,src)
#else
#   define vlc_va_copy(dest,src) (dest)=(src)
#endif

85
#define QUEUE priv->msg_bank
86
87
88
89
static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
{
    return &(libvlc_priv (inst))->msg_bank;
}
90

Vincent Seguin's avatar
Vincent Seguin committed
91
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
92
 * Local prototypes
Vincent Seguin's avatar
Vincent Seguin committed
93
 *****************************************************************************/
94
static void QueueMsg ( vlc_object_t *, int, const char *,
95
                       const char *, va_list );
96
static void PrintMsg ( vlc_object_t *, msg_item_t * );
Michel Kaempf's avatar
Michel Kaempf committed
97

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
98
99
static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;

sigmunau's avatar
all:    
sigmunau committed
100
/**
101
102
 * Initialize messages queues
 * This function initializes all message queues
sigmunau's avatar
all:    
sigmunau committed
103
 */
104
void msg_Create (libvlc_int_t *p_libvlc)
Michel Kaempf's avatar
Michel Kaempf committed
105
{
106
    libvlc_priv_t *priv = libvlc_priv (p_libvlc);
107
108
    msg_bank_t *bank = libvlc_bank (p_libvlc);

109
    vlc_rwlock_init (&bank->lock);
110
111
112
    vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
    priv->msg_all_objects_enabled = true;

113
    QUEUE.i_sub = 0;
114
    QUEUE.pp_sub = NULL;
115
116

#ifdef UNDER_CE
117
    QUEUE.logfile =
118
119
        CreateFile( L"vlc-log.txt", GENERIC_WRITE,
                    FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
120
                    CREATE_ALWAYS, 0, NULL );
121
    SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
122
#endif
123

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
124
    vlc_mutex_lock( &msg_stack_lock );
125
    if( banks++ == 0 )
126
        vlc_threadvar_create( &msg_context, cleanup_msg_context );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
127
    vlc_mutex_unlock( &msg_stack_lock );
Michel Kaempf's avatar
Michel Kaempf committed
128
129
}

130
131
132
/**
 * Object Printing selection
 */
133
134
static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
135

136
void __msg_EnableObjectPrinting (vlc_object_t *p_this, const char * psz_object)
137
138
{
    libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
139
    vlc_rwlock_wrlock( &QUEUE.lock );
140
141
142
    if( !strcmp(psz_object, "all") )
        priv->msg_all_objects_enabled = true;
    else
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
143
        vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
144
    vlc_rwlock_unlock( &QUEUE.lock );
145
146
}

147
void __msg_DisableObjectPrinting (vlc_object_t *p_this, const char * psz_object)
148
149
{
    libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
150
    vlc_rwlock_wrlock( &QUEUE.lock );
151
152
153
    if( !strcmp(psz_object, "all") )
        priv->msg_all_objects_enabled = false;
    else
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
154
        vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
155
    vlc_rwlock_unlock( &QUEUE.lock );
156
157
}

sigmunau's avatar
all:    
sigmunau committed
158
/**
159
 * Destroy the message queues
sigmunau's avatar
all:    
sigmunau committed
160
 *
161
 * This functions prints all messages remaining in the queues,
162
 * then frees all the allocated resources
Michel Kaempf's avatar
Michel Kaempf committed
163
 * No other messages interface functions should be called after this one.
sigmunau's avatar
all:    
sigmunau committed
164
 */
165
void msg_Destroy (libvlc_int_t *p_libvlc)
Michel Kaempf's avatar
Michel Kaempf committed
166
{
167
    libvlc_priv_t *priv = libvlc_priv (p_libvlc);
168
    msg_bank_t *bank = libvlc_bank (p_libvlc);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
169

170
    if( QUEUE.i_sub )
171
        msg_Err( p_libvlc, "stale interface subscribers (VLC might crash)" );
172

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
173
    vlc_mutex_lock( &msg_stack_lock );
174
    assert(banks > 0);
175
176
    if( --banks == 0 )
        vlc_threadvar_delete( &msg_context );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
177
    vlc_mutex_unlock( &msg_stack_lock );
178

179
#ifdef UNDER_CE
180
    CloseHandle( QUEUE.logfile );
181
#endif
182

183
    vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
184

185
    vlc_rwlock_destroy (&bank->lock);
186
187
188
189
190
191
192
193
194
}

struct msg_subscription_t
{
    libvlc_int_t   *instance;
    msg_callback_t  func;
    msg_cb_data_t  *opaque;
};

sigmunau's avatar
all:    
sigmunau committed
195
/**
196
197
198
199
200
201
202
203
 * Subscribe to the message queue.
 * Whenever a message is emitted, a callback will be called.
 * Callback invocation are serialized within a subscription.
 *
 * @param instance LibVLC instance to get messages from
 * @param cb callback function
 * @param opaque data for the callback function
 * @return a subscription pointer, or NULL in case of failure
sigmunau's avatar
all:    
sigmunau committed
204
 */
205
206
msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
                                   msg_cb_data_t *opaque)
Sam Hocevar's avatar
   
Sam Hocevar committed
207
{
208
209
    msg_subscription_t *sub = malloc (sizeof (*sub));
    if (sub == NULL)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
210
211
        return NULL;

212
213
214
    sub->instance = instance;
    sub->func = cb;
    sub->opaque = opaque;
Sam Hocevar's avatar
   
Sam Hocevar committed
215

216
    msg_bank_t *bank = libvlc_bank (instance);
217
    vlc_rwlock_wrlock (&bank->lock);
218
    TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
219
    vlc_rwlock_unlock (&bank->lock);
Sam Hocevar's avatar
   
Sam Hocevar committed
220

221
    return sub;
Sam Hocevar's avatar
   
Sam Hocevar committed
222
223
}

sigmunau's avatar
all:    
sigmunau committed
224
/**
225
226
 * Unsubscribe from the message queue.
 * This function waits for the message callback to return if needed.
sigmunau's avatar
all:    
sigmunau committed
227
 */
228
void msg_Unsubscribe (msg_subscription_t *sub)
Sam Hocevar's avatar
   
Sam Hocevar committed
229
{
230
    msg_bank_t *bank = libvlc_bank (sub->instance);
Sam Hocevar's avatar
   
Sam Hocevar committed
231

232
    vlc_rwlock_wrlock (&bank->lock);
233
    TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
234
    vlc_rwlock_unlock (&bank->lock);
235
    free (sub);
Michel Kaempf's avatar
Michel Kaempf committed
236
237
}

238
/*****************************************************************************
239
 * __msg_*: print a message
240
 *****************************************************************************
241
 * These functions queue a message for later printing.
242
 *****************************************************************************/
243
void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
244
                    const char *psz_format, ... )
Michel Kaempf's avatar
Michel Kaempf committed
245
{
246
    va_list args;
Michel Kaempf's avatar
Michel Kaempf committed
247

248
    va_start( args, psz_format );
249
    QueueMsg( p_this, i_type, psz_module, psz_format, args );
250
    va_end( args );
Michel Kaempf's avatar
Michel Kaempf committed
251
}
252

253
void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
254
255
                      const char *psz_format, va_list args )
{
256
    QueueMsg( p_this, i_type, psz_module, psz_format, args );
257
258
}

259
260
261
262
263
264
265
266
267
268
269
270
271
/**
 * Destroys a message.
 */
static void msg_Free (gc_object_t *gc)
{
    msg_item_t *msg = vlc_priv (gc, msg_item_t);

    free (msg->psz_module);
    free (msg->psz_msg);
    free (msg->psz_header);
    free (msg);
}

sigmunau's avatar
all:    
sigmunau committed
272
273
274
/**
 * Add a message to a queue
 *
275
 * This function provides basic functionnalities to other msg_* functions.
276
277
278
 * It adds a message to a queue (after having printed all stored messages if it
 * is full). If the message can't be converted to string in memory, it issues
 * a warning.
sigmunau's avatar
all:    
sigmunau committed
279
 */
280
static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
281
                      const char *psz_format, va_list _args )
Michel Kaempf's avatar
Michel Kaempf committed
282
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
283
    assert (p_this);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
284
    libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
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;
gbazin's avatar
   
gbazin committed
290

291
292
    if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
        (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
293
        return;
294

295
296
297
298
#ifndef __GLIBC__
    /* Expand %m to strerror(errno) - only once */
    char buf[strlen( psz_format ) + 2001], *ptr;
    strcpy( buf, psz_format );
Rafaël Carré's avatar
Rafaël Carré committed
299
300
    ptr = (char*)buf;
    psz_format = (const char*) buf;
301
302
303
304
305
306
307
308
309
310
311
312

    for( ;; )
    {
        ptr = strchr( ptr, '%' );
        if( ptr == NULL )
            break;

        if( ptr[1] == 'm' )
        {
            char errbuf[2001];
            size_t errlen;

313
#ifndef WIN32
314
            strerror_r( errno, errbuf, 1001 );
315
316
317
318
319
320
321
#else
            int sockerr = WSAGetLastError( );
            if( sockerr )
            {
                strncpy( errbuf, net_strerror( sockerr ), 1001 );
                WSASetLastError( sockerr );
            }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
322
323
            if ((sockerr == 0)
             || (strcmp ("Unknown network stack error", errbuf) == 0))
324
325
                strncpy( errbuf, strerror( errno ), 1001 );
#endif
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
            errbuf[1000] = 0;

            /* Escape '%' from the error string */
            for( char *percent = strchr( errbuf, '%' );
                 percent != NULL;
                 percent = strchr( percent + 2, '%' ) )
            {
                memmove( percent + 1, percent, strlen( percent ) + 1 );
            }

            errlen = strlen( errbuf );
            memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
            memcpy( ptr, errbuf, errlen );
            break; /* Only once, so we don't overflow */
        }

        /* Looks for conversion specifier... */
        do
            ptr++;
        while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
        if( *ptr )
            ptr++; /* ...and skip it */
    }
#endif

351
    /* Convert message to string  */
352
    vlc_va_copy( args, _args );
353
354
    if( vasprintf( &psz_str, psz_format, args ) == -1 )
        psz_str = NULL;
355
    va_end( args );
Sam Hocevar's avatar
   
Sam Hocevar committed
356

Michel Kaempf's avatar
Michel Kaempf committed
357
358
    if( psz_str == NULL )
    {
359
        int canc = vlc_savecancel (); /* Do not print half of a message... */
Rafaël Carré's avatar
Rafaël Carré committed
360
361
362
#ifdef __GLIBC__
        fprintf( stderr, "main warning: can't store message (%m): " );
#else
Rafaël Carré's avatar
Rafaël Carré committed
363
        char psz_err[1001];
Rafaël Carré's avatar
Rafaël Carré committed
364
365
366
367
368
#ifndef WIN32
        /* we're not using GLIBC, so we are sure that the error description
         * will be stored in the buffer we provide to strerror_r() */
        strerror_r( errno, psz_err, 1001 );
#else
369
        strncpy( psz_err, strerror( errno ), 1001 );
Rafaël Carré's avatar
Rafaël Carré committed
370
371
372
373
#endif
        psz_err[1000] = '\0';
        fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
#endif
374
        vlc_va_copy( args, _args );
375
        /* We should use utf8_vfprintf - but it calls malloc()... */
376
        vfprintf( stderr, psz_format, args );
377
        va_end( args );
378
        fputs( "\n", stderr );
379
        vlc_restorecancel (canc);
380
        return;
Michel Kaempf's avatar
Michel Kaempf committed
381
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
382

383
384
385
386
387
388
389
390
391
    msg_item_t * p_item = malloc (sizeof (*p_item));
    if (p_item == NULL)
        return; /* Uho! */

    vlc_gc_init (p_item, msg_Free);
    p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;



392
393
394
395
396
397
398
399
400
401
402
    i_header_size = 0;
    p_obj = p_this;
    while( p_obj != NULL )
    {
        char *psz_old = NULL;
        if( p_obj->psz_header )
        {
            i_header_size += strlen( p_obj->psz_header ) + 4;
            if( psz_header )
            {
                psz_old = strdup( psz_header );
403
                psz_header = xrealloc( psz_header, i_header_size );
404
405
406
407
408
                snprintf( psz_header, i_header_size , "[%s] %s",
                          p_obj->psz_header, psz_old );
            }
            else
            {
409
                psz_header = xmalloc( i_header_size );
410
411
412
413
                snprintf( psz_header, i_header_size, "[%s]",
                          p_obj->psz_header );
            }
        }
414
        free( psz_old );
415
416
417
        p_obj = p_obj->p_parent;
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
418
    /* Fill message information fields */
419
    p_item->i_type =        i_type;
420
    p_item->i_object_id =   (uintptr_t)p_this;
421
    p_item->psz_object_type = p_this->psz_object_type;
422
423
    p_item->psz_module =    strdup( psz_module );
    p_item->psz_msg =       psz_str;
424
    p_item->psz_header =    psz_header;
425

426
    PrintMsg( p_this, p_item );
427

428
429
    msg_bank_t *bank = &QUEUE;
    vlc_rwlock_rdlock (&bank->lock);
430
    for (int i = 0; i < bank->i_sub; i++)
431
    {
432
        msg_subscription_t *sub = bank->pp_sub[i];
433
        sub->func (sub->opaque, p_item, 0);
434
    }
435
    vlc_rwlock_unlock (&bank->lock);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
436
    msg_Release (p_item);
Michel Kaempf's avatar
Michel Kaempf committed
437
438
}

439
/*****************************************************************************
440
 * PrintMsg: output a standard message item to stderr
441
442
443
444
445
446
447
448
449
 *****************************************************************************
 * Print a message to stderr, with colour formatting if needed.
 *****************************************************************************/
static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
{
#   define COL(x)  "\033[" #x ";1m"
#   define RED     COL(31)
#   define GREEN   COL(32)
#   define YELLOW  COL(33)
450
#   define WHITE   COL(0)
451
452
#   define GRAY    "\033[0m"

453
454
455
#ifdef UNDER_CE
    int i_dummy;
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
456
457
    static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
    static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
458
    const char *psz_object;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
459
    libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
460
461
    int i_type = p_item->i_type;

462
    switch( i_type )
463
    {
464
        case VLC_MSG_ERR:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
465
            if( priv->i_verbose < 0 ) return;
466
467
            break;
        case VLC_MSG_INFO:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
468
            if( priv->i_verbose < 0 ) return;
469
470
            break;
        case VLC_MSG_WARN:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
471
            if( priv->i_verbose < 1 ) return;
472
473
            break;
        case VLC_MSG_DBG:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
474
            if( priv->i_verbose < 2 ) return;
475
            break;
476
477
    }

478
    psz_object = p_item->psz_object_type;
479
    void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
480
                                               p_item->psz_module );
481
482
483
484
    if( val == kObjectPrintingDisabled )
        return;
    if( val == kObjectPrintingEnabled )
        /* Allowed */;
485
486
487
488
489
490
491
492
493
494
495
    else
    {
        val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
                                            psz_object );
        if( val == kObjectPrintingDisabled )
            return;
        if( val == kObjectPrintingEnabled )
            /* Allowed */;
        else if( !priv->msg_all_objects_enabled )
            return;
    }
496

497
#ifdef UNDER_CE
498
#   define CE_WRITE(str) WriteFile( QUEUE.logfile, \
499
500
501
502
503
504
505
                                    str, strlen(str), &i_dummy, NULL );
    CE_WRITE( p_item->psz_module );
    CE_WRITE( " " );
    CE_WRITE( psz_object );
    CE_WRITE( ppsz_type[i_type] );
    CE_WRITE( ": " );
    CE_WRITE( p_item->psz_msg );
506
    CE_WRITE( "\r\n" );
507
    FlushFileBuffers( QUEUE.logfile );
508
509

#else
Pierre Ynard's avatar
Pierre Ynard committed
510
    int canc = vlc_savecancel ();
511
    /* Send the message to stderr */
512
513
514
515
516
517
518
519
520
521
522
    utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
                  priv->b_color ? GREEN : "",
                  (void *)p_item->i_object_id,
                  priv->b_color ? GRAY : "",
                  p_item->psz_header ? p_item->psz_header : "",
                  p_item->psz_header ? " " : "",
                  p_item->psz_module, psz_object,
                  ppsz_type[i_type],
                  priv->b_color ? ppsz_color[i_type] : "",
                  p_item->psz_msg,
                  priv->b_color ? GRAY : "" );
523

524
#   if defined(WIN32)
525
    fflush( stderr );
526
#   endif
527
    vlc_restorecancel (canc);
528
#endif
529
}
530
531
532

static msg_context_t* GetContext(void)
{
533
    msg_context_t *p_ctx = vlc_threadvar_get( msg_context );
534
535
    if( p_ctx == NULL )
    {
536
537
538
        p_ctx = malloc( sizeof( msg_context_t ) );
        if( !p_ctx )
            return NULL;
539
        p_ctx->psz_message = NULL;
540
        vlc_threadvar_set( msg_context, p_ctx );
541
542
543
544
    }
    return p_ctx;
}

545
546
547
548
549
550
551
552
void msg_StackDestroy (void *data)
{
    msg_context_t *p_ctx = data;

    free (p_ctx->psz_message);
    free (p_ctx);
}

553
554
555
556
void msg_StackSet( int i_code, const char *psz_message, ... )
{
    va_list ap;
    msg_context_t *p_ctx = GetContext();
557
558
559

    if( p_ctx == NULL )
        return;
560
    free( p_ctx->psz_message );
561

562
    va_start( ap, psz_message );
563
564
    if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
        p_ctx->psz_message = NULL;
565
    va_end( ap );
566

567
568
569
570
571
572
573
574
    p_ctx->i_code = i_code;
}

void msg_StackAdd( const char *psz_message, ... )
{
    char *psz_tmp;
    va_list ap;
    msg_context_t *p_ctx = GetContext();
575
576
577

    if( p_ctx == NULL )
        return;
578
579

    va_start( ap, psz_message );
580
581
    if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
        psz_tmp = NULL;
582
583
584
585
586
587
    va_end( ap );

    if( !p_ctx->psz_message )
        p_ctx->psz_message = psz_tmp;
    else
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
588
589
590
591
592
593
594
        char *psz_new;
        if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
            psz_new = NULL;

        free( p_ctx->psz_message );
        p_ctx->psz_message = psz_new;
        free( psz_tmp );
595
596
597
598
599
600
601
602
603
    }
}

const char* msg_StackMsg( void )
{
    msg_context_t *p_ctx = GetContext();
    assert( p_ctx );
    return p_ctx->psz_message;
}