messages.c 15.5 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 */
38
#ifdef __APPLE__
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
39
# include <xlocale.h>
40
#elif defined(HAVE_LOCALE_H)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
41
# include <locale.h>
42
#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
64
65
66
67
68
/*****************************************************************************
 * 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

69
70
static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
{
71
    return (libvlc_priv (inst))->msg_bank;
72
}
73

Vincent Seguin's avatar
Vincent Seguin committed
74
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
75
 * Local prototypes
Vincent Seguin's avatar
Vincent Seguin committed
76
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
77
static void PrintMsg ( vlc_object_t *, const msg_item_t * );
Michel Kaempf's avatar
Michel Kaempf committed
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
 * Store all data required by messages interfaces.
 */
struct msg_bank_t
{
    /** Message queue lock */
    vlc_rwlock_t lock;

    /* Subscribers */
    int i_sub;
    msg_subscription_t **pp_sub;

    locale_t locale; /**< C locale for error messages */
    vlc_dictionary_t enabled_objects; ///< Enabled objects
    bool all_objects_enabled; ///< Should we print all objects?
};

sigmunau's avatar
all:    
sigmunau committed
96
/**
97
98
 * Initialize messages queues
 * This function initializes all message queues
sigmunau's avatar
all:    
sigmunau committed
99
 */
100
msg_bank_t *msg_Create (void)
Michel Kaempf's avatar
Michel Kaempf committed
101
{
102
    msg_bank_t *bank = malloc (sizeof (*bank));
103

104
    vlc_rwlock_init (&bank->lock);
105
106
    vlc_dictionary_init (&bank->enabled_objects, 0);
    bank->all_objects_enabled = true;
107

108
109
    bank->i_sub = 0;
    bank->pp_sub = NULL;
110

111
112
    /* C locale to get error messages in English in the logs */
    bank->locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t)0);
113
    return bank;
Michel Kaempf's avatar
Michel Kaempf committed
114
115
}

116
117
118
/**
 * Object Printing selection
 */
119
120
static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
121

122

123
124
#undef msg_EnableObjectPrinting
void msg_EnableObjectPrinting (vlc_object_t *obj, const char * psz_object)
125
{
126
127
128
    msg_bank_t *bank = libvlc_bank (obj->p_libvlc);

    vlc_rwlock_wrlock (&bank->lock);
129
    if( !strcmp(psz_object, "all") )
130
        bank->all_objects_enabled = true;
131
    else
132
133
134
        vlc_dictionary_insert (&bank->enabled_objects, psz_object,
                               (void *)kObjectPrintingEnabled);
    vlc_rwlock_unlock (&bank->lock);
135
136
}

137
138
#undef msg_DisableObjectPrinting
void msg_DisableObjectPrinting (vlc_object_t *obj, const char * psz_object)
139
{
140
141
142
    msg_bank_t *bank = libvlc_bank (obj->p_libvlc);

    vlc_rwlock_wrlock (&bank->lock);
143
    if( !strcmp(psz_object, "all") )
144
        bank->all_objects_enabled = false;
145
    else
146
147
148
        vlc_dictionary_insert (&bank->enabled_objects, psz_object,
                               (void *)kObjectPrintingDisabled);
    vlc_rwlock_unlock (&bank->lock);
149
150
}

sigmunau's avatar
all:    
sigmunau committed
151
/**
152
 * Destroy the message queues
sigmunau's avatar
all:    
sigmunau committed
153
 *
154
 * This functions prints all messages remaining in the queues,
155
 * then frees all the allocated resources
Michel Kaempf's avatar
Michel Kaempf committed
156
 * No other messages interface functions should be called after this one.
sigmunau's avatar
all:    
sigmunau committed
157
 */
158
void msg_Destroy (msg_bank_t *bank)
Michel Kaempf's avatar
Michel Kaempf committed
159
{
160
161
    if (unlikely(bank->i_sub != 0))
        fputs ("stale interface subscribers (LibVLC might crash)\n", stderr);
162

163
164
    if (bank->locale != (locale_t)0)
       freelocale (bank->locale);
165

166
    vlc_dictionary_clear (&bank->enabled_objects, NULL, NULL);
167

168
    vlc_rwlock_destroy (&bank->lock);
169
    free (bank);
170
171
172
173
174
175
176
}

struct msg_subscription_t
{
    libvlc_int_t   *instance;
    msg_callback_t  func;
    msg_cb_data_t  *opaque;
177
    int             verbosity;
178
179
};

sigmunau's avatar
all:    
sigmunau committed
180
/**
181
182
183
184
185
186
187
188
 * 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
189
 */
190
191
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
192
{
193
194
    msg_subscription_t *sub = malloc (sizeof (*sub));
    if (sub == NULL)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
195
196
        return NULL;

197
198
199
    sub->instance = instance;
    sub->func = cb;
    sub->opaque = opaque;
200
    sub->verbosity = 2; /* by default, give all the messages */
Sam Hocevar's avatar
   
Sam Hocevar committed
201

202
    msg_bank_t *bank = libvlc_bank (instance);
203
    vlc_rwlock_wrlock (&bank->lock);
204
    TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
205
    vlc_rwlock_unlock (&bank->lock);
Sam Hocevar's avatar
   
Sam Hocevar committed
206

207
    return sub;
Sam Hocevar's avatar
   
Sam Hocevar committed
208
209
}

sigmunau's avatar
all:    
sigmunau committed
210
/**
211
212
 * Unsubscribe from the message queue.
 * This function waits for the message callback to return if needed.
sigmunau's avatar
all:    
sigmunau committed
213
 */
214
void msg_Unsubscribe (msg_subscription_t *sub)
Sam Hocevar's avatar
   
Sam Hocevar committed
215
{
216
    msg_bank_t *bank = libvlc_bank (sub->instance);
Sam Hocevar's avatar
   
Sam Hocevar committed
217

218
    vlc_rwlock_wrlock (&bank->lock);
219
    TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
220
    vlc_rwlock_unlock (&bank->lock);
221
    free (sub);
Michel Kaempf's avatar
Michel Kaempf committed
222
223
}

224
225
226
227
228
229
230
231
232
233
234
235
void msg_SubscriptionSetVerbosity( msg_subscription_t *sub, const int i_verbosity )
{
    if( i_verbosity < 0 || i_verbosity > 2 ) return;

    msg_bank_t *bank = libvlc_bank ( sub->instance );

    vlc_rwlock_wrlock (&bank->lock);

    sub->verbosity = i_verbosity;

    vlc_rwlock_unlock (&bank->lock);
}
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, ... )
Michel Kaempf's avatar
Michel Kaempf committed
243
{
244
    va_list args;
Michel Kaempf's avatar
Michel Kaempf committed
245

246
    va_start( args, psz_format );
247
    msg_GenericVa (p_this, i_type, psz_module, psz_format, args);
248
    va_end( args );
Michel Kaempf's avatar
Michel Kaempf committed
249
}
250

251
#undef msg_GenericVa
sigmunau's avatar
all:    
sigmunau committed
252
253
254
/**
 * Add a message to a queue
 *
255
 * This function provides basic functionnalities to other msg_* functions.
256
257
258
 * 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
259
 */
260
261
262
void msg_GenericVa (vlc_object_t *p_this, int i_type,
                           const char *psz_module,
                           const char *psz_format, va_list _args)
Michel Kaempf's avatar
Michel Kaempf committed
263
{
264
    char *       psz_str = NULL;                 /* formatted message string */
265
    va_list      args;
gbazin's avatar
   
gbazin committed
266

267
268
    assert (p_this);

269
270
    if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
        (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
271
        return;
272

273
    msg_bank_t *bank = libvlc_bank (p_this->p_libvlc);
274
275
    locale_t locale = uselocale (bank->locale);

276
277
278
279
#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
280
281
    ptr = (char*)buf;
    psz_format = (const char*) buf;
282
283
284
285
286
287
288
289
290
291
292
293

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

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

294
#ifndef WIN32
295
            strerror_r( errno, errbuf, 1001 );
296
297
298
299
300
301
302
#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
303
304
            if ((sockerr == 0)
             || (strcmp ("Unknown network stack error", errbuf) == 0))
305
306
                strncpy( errbuf, strerror( errno ), 1001 );
#endif
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
            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

332
    /* Convert message to string  */
333
    vlc_va_copy( args, _args );
334
335
    if( vasprintf( &psz_str, psz_format, args ) == -1 )
        psz_str = NULL;
336
    va_end( args );
Sam Hocevar's avatar
   
Sam Hocevar committed
337

Michel Kaempf's avatar
Michel Kaempf committed
338
339
    if( psz_str == NULL )
    {
340
        int canc = vlc_savecancel (); /* Do not print half of a message... */
Rafaël Carré's avatar
Rafaël Carré committed
341
342
343
#ifdef __GLIBC__
        fprintf( stderr, "main warning: can't store message (%m): " );
#else
Rafaël Carré's avatar
Rafaël Carré committed
344
        char psz_err[1001];
Rafaël Carré's avatar
Rafaël Carré committed
345
346
347
348
349
#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
350
        strncpy( psz_err, strerror( errno ), 1001 );
Rafaël Carré's avatar
Rafaël Carré committed
351
352
353
354
#endif
        psz_err[1000] = '\0';
        fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
#endif
355
        vlc_va_copy( args, _args );
356
        /* We should use utf8_vfprintf - but it calls malloc()... */
357
        vfprintf( stderr, psz_format, args );
358
        va_end( args );
359
        fputs( "\n", stderr );
360
        vlc_restorecancel (canc);
361
        uselocale (locale);
362
        return;
Michel Kaempf's avatar
Michel Kaempf committed
363
    }
364
    uselocale (locale);
Sam Hocevar's avatar
   
Sam Hocevar committed
365

366
367
    /* Fill message information fields */
    msg_item_t msg;
368

369
370
371
    msg.i_type = i_type;
    msg.i_object_id = (uintptr_t)p_this;
    msg.psz_object_type = p_this->psz_object_type;
372
    msg.psz_module = psz_module;
373
    msg.psz_msg = psz_str;
374
    msg.psz_header = NULL;
375

376
377
    for (vlc_object_t *o = p_this; o != NULL; o = o->p_parent)
        if (o->psz_header != NULL)
378
        {
379
380
381
            if (asprintf (&msg.psz_header, "[%s]", o->psz_header) == -1)
                msg.psz_header = NULL;
            break;
382
        }
383

384
    PrintMsg( p_this, &msg );
385

386
    vlc_rwlock_rdlock (&bank->lock);
387
    for (int i = 0; i < bank->i_sub; i++)
388
    {
389
        msg_subscription_t *sub = bank->pp_sub[i];
390
391
392
        libvlc_priv_t *priv = libvlc_priv( sub->instance );
        msg_bank_t *bank = priv->msg_bank;
        void *val = vlc_dictionary_value_for_key( &bank->enabled_objects,
393
                                                  msg.psz_module );
394
395
396
397
        if( val == kObjectPrintingDisabled ) continue;
        if( val != kObjectPrintingEnabled  ) /*if not allowed */
        {
            val = vlc_dictionary_value_for_key( &bank->enabled_objects,
398
                                                msg.psz_object_type );
399
400
401
402
            if( val == kObjectPrintingDisabled ) continue;
            if( val == kObjectPrintingEnabled  ); /* Allowed */
            else if( !bank->all_objects_enabled ) continue;
        }
403
        switch( msg.i_type )
404
405
406
407
408
409
410
411
412
413
414
415
        {
            case VLC_MSG_INFO:
            case VLC_MSG_ERR:
                if( sub->verbosity < 0 ) continue;
                break;
            case VLC_MSG_WARN:
                if( sub->verbosity < 1 ) continue;
                break;
            case VLC_MSG_DBG:
                if( sub->verbosity < 2 ) continue;
                break;
        }
416

417
        sub->func (sub->opaque, &msg, 0);
418
    }
419
    vlc_rwlock_unlock (&bank->lock);
420
421
    free (msg.psz_msg);
    free (msg.psz_header);
Michel Kaempf's avatar
Michel Kaempf committed
422
423
}

424
/*****************************************************************************
425
 * PrintMsg: output a standard message item to stderr
426
427
428
 *****************************************************************************
 * Print a message to stderr, with colour formatting if needed.
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
429
static void PrintMsg ( vlc_object_t *p_this, const msg_item_t *p_item )
430
{
sebastien's avatar
sebastien committed
431
432
433
434
435
#   define COL(x,y)  "\033[" #x ";" #y "m"
#   define RED     COL(31,1)
#   define GREEN   COL(32,1)
#   define YELLOW  COL(0,33)
#   define WHITE   COL(0,1)
436
437
#   define GRAY    "\033[0m"

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
438
439
    static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
    static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
440
    const char *psz_object;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
441
    libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
442
    msg_bank_t *bank = priv->msg_bank;
443
444
    int i_type = p_item->i_type;

445
    switch( i_type )
446
    {
447
        case VLC_MSG_ERR:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
448
            if( priv->i_verbose < 0 ) return;
449
450
            break;
        case VLC_MSG_INFO:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
451
            if( priv->i_verbose < 0 ) return;
452
453
            break;
        case VLC_MSG_WARN:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
454
            if( priv->i_verbose < 1 ) return;
455
456
            break;
        case VLC_MSG_DBG:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
457
            if( priv->i_verbose < 2 ) return;
458
            break;
459
460
    }

461
    psz_object = p_item->psz_object_type;
462
463
    void * val = vlc_dictionary_value_for_key (&bank->enabled_objects,
                                               p_item->psz_module);
464
465
466
467
    if( val == kObjectPrintingDisabled )
        return;
    if( val == kObjectPrintingEnabled )
        /* Allowed */;
468
469
    else
    {
470
471
        val = vlc_dictionary_value_for_key (&bank->enabled_objects,
                                            psz_object);
472
473
474
475
        if( val == kObjectPrintingDisabled )
            return;
        if( val == kObjectPrintingEnabled )
            /* Allowed */;
476
        else if( !bank->all_objects_enabled )
477
478
            return;
    }
479

Pierre Ynard's avatar
Pierre Ynard committed
480
    int canc = vlc_savecancel ();
481
    /* Send the message to stderr */
482
483
484
485
486
487
488
489
490
491
492
    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 : "" );
493

494
#if defined( WIN32 ) || defined( __OS2__ )
495
    fflush( stderr );
496
#endif
497
    vlc_restorecancel (canc);
498
}