messages.c 14.9 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
/**
 * 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;

    vlc_dictionary_t enabled_objects; ///< Enabled objects
    bool all_objects_enabled; ///< Should we print all objects?
};

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

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

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

110
    return bank;
Michel Kaempf's avatar
Michel Kaempf committed
111
112
}

113
114
115
/**
 * Object Printing selection
 */
116
117
static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
118

119

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

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

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

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

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

160
    vlc_dictionary_clear (&bank->enabled_objects, NULL, NULL);
161

162
    vlc_rwlock_destroy (&bank->lock);
163
    free (bank);
164
165
166
167
168
169
170
}

struct msg_subscription_t
{
    libvlc_int_t   *instance;
    msg_callback_t  func;
    msg_cb_data_t  *opaque;
171
    int             verbosity;
172
173
};

sigmunau's avatar
all:    
sigmunau committed
174
/**
175
176
177
178
179
180
181
182
 * 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
183
 */
184
185
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
186
{
187
188
    msg_subscription_t *sub = malloc (sizeof (*sub));
    if (sub == NULL)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
189
190
        return NULL;

191
192
193
    sub->instance = instance;
    sub->func = cb;
    sub->opaque = opaque;
194
    sub->verbosity = 2; /* by default, give all the messages */
Sam Hocevar's avatar
   
Sam Hocevar committed
195

196
    msg_bank_t *bank = libvlc_bank (instance);
197
    vlc_rwlock_wrlock (&bank->lock);
198
    TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
199
    vlc_rwlock_unlock (&bank->lock);
Sam Hocevar's avatar
   
Sam Hocevar committed
200

201
    return sub;
Sam Hocevar's avatar
   
Sam Hocevar committed
202
203
}

sigmunau's avatar
all:    
sigmunau committed
204
/**
205
206
 * Unsubscribe from the message queue.
 * This function waits for the message callback to return if needed.
sigmunau's avatar
all:    
sigmunau committed
207
 */
208
void msg_Unsubscribe (msg_subscription_t *sub)
Sam Hocevar's avatar
   
Sam Hocevar committed
209
{
210
    msg_bank_t *bank = libvlc_bank (sub->instance);
Sam Hocevar's avatar
   
Sam Hocevar committed
211

212
    vlc_rwlock_wrlock (&bank->lock);
213
    TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
214
    vlc_rwlock_unlock (&bank->lock);
215
    free (sub);
Michel Kaempf's avatar
Michel Kaempf committed
216
217
}

218
219
220
221
222
223
224
225
226
227
228
229
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);
}
230
/*****************************************************************************
231
 * msg_*: print a message
232
 *****************************************************************************
233
 * These functions queue a message for later printing.
234
 *****************************************************************************/
235
void msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
236
                    const char *psz_format, ... )
Michel Kaempf's avatar
Michel Kaempf committed
237
{
238
    va_list args;
Michel Kaempf's avatar
Michel Kaempf committed
239

240
    va_start( args, psz_format );
241
    msg_GenericVa (p_this, i_type, psz_module, psz_format, args);
242
    va_end( args );
Michel Kaempf's avatar
Michel Kaempf committed
243
}
244

245
#undef msg_GenericVa
sigmunau's avatar
all:    
sigmunau committed
246
247
248
/**
 * Add a message to a queue
 *
249
 * This function provides basic functionnalities to other msg_* functions.
250
251
252
 * 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
253
 */
254
255
256
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
257
{
258
    char *       psz_str = NULL;                 /* formatted message string */
259
    va_list      args;
gbazin's avatar
   
gbazin committed
260

261
262
    assert (p_this);

263
    if( p_this->i_flags & OBJECT_FLAGS_QUIET )
264
        return;
265

266
    msg_bank_t *bank = libvlc_bank (p_this->p_libvlc);
267
268
269
270

    /* C locale to get error messages in English in the logs */
    locale_t c = newlocale (LC_MESSAGES_MASK, "C", (locale_t)0);
    locale_t locale = uselocale (c);
271

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

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

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

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

328
    /* Convert message to string  */
329
    vlc_va_copy( args, _args );
330
331
    if( vasprintf( &psz_str, psz_format, args ) == -1 )
        psz_str = NULL;
332
    va_end( args );
Sam Hocevar's avatar
   
Sam Hocevar committed
333

334
335
336
337
    uselocale (locale);
    freelocale (c);

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

363
364
    /* Fill message information fields */
    msg_item_t msg;
365

366
367
368
    msg.i_type = i_type;
    msg.i_object_id = (uintptr_t)p_this;
    msg.psz_object_type = p_this->psz_object_type;
369
    msg.psz_module = psz_module;
370
    msg.psz_msg = psz_str;
371
    msg.psz_header = NULL;
372

373
374
    for (vlc_object_t *o = p_this; o != NULL; o = o->p_parent)
        if (o->psz_header != NULL)
375
        {
376
            msg.psz_header = o->psz_header;
377
            break;
378
        }
379

380
    PrintMsg( p_this, &msg );
381

382
    vlc_rwlock_rdlock (&bank->lock);
383
    for (int i = 0; i < bank->i_sub; i++)
384
    {
385
        msg_subscription_t *sub = bank->pp_sub[i];
386
387
388
        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,
389
                                                  msg.psz_module );
390
391
392
393
        if( val == kObjectPrintingDisabled ) continue;
        if( val != kObjectPrintingEnabled  ) /*if not allowed */
        {
            val = vlc_dictionary_value_for_key( &bank->enabled_objects,
394
                                                msg.psz_object_type );
395
396
397
398
            if( val == kObjectPrintingDisabled ) continue;
            if( val == kObjectPrintingEnabled  ); /* Allowed */
            else if( !bank->all_objects_enabled ) continue;
        }
399
        switch( msg.i_type )
400
401
402
403
404
405
406
407
408
409
410
411
        {
            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;
        }
412

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
413
        sub->func (sub->opaque, &msg);
414
    }
415
    vlc_rwlock_unlock (&bank->lock);
416
    free (msg.psz_msg);
Michel Kaempf's avatar
Michel Kaempf committed
417
418
}

419
/*****************************************************************************
420
 * PrintMsg: output a standard message item to stderr
421
422
423
 *****************************************************************************
 * Print a message to stderr, with colour formatting if needed.
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
424
static void PrintMsg ( vlc_object_t *p_this, const msg_item_t *p_item )
425
{
sebastien's avatar
sebastien committed
426
427
428
429
430
#   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)
431
#   define GRAY    "\033[0m"
432
433
    static const char msgtype[4][9] = { "", " error", " warning", " debug" };
    static const char msgcolor[4][8] = { WHITE, RED, YELLOW, GRAY };
434

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
435
    libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
436
    int type = p_item->i_type;
437

438
439
    if (priv->i_verbose < 0 || priv->i_verbose < (type - VLC_MSG_ERR))
        return;
440

441
    const char *objtype = p_item->psz_object_type;
442
    msg_bank_t *bank = priv->msg_bank;
443
444
    void * val = vlc_dictionary_value_for_key (&bank->enabled_objects,
                                               p_item->psz_module);
445
446
447
448
    if( val == kObjectPrintingDisabled )
        return;
    if( val == kObjectPrintingEnabled )
        /* Allowed */;
449
450
    else
    {
451
        val = vlc_dictionary_value_for_key (&bank->enabled_objects,
452
                                            objtype);
453
454
455
456
        if( val == kObjectPrintingDisabled )
            return;
        if( val == kObjectPrintingEnabled )
            /* Allowed */;
457
        else if( !bank->all_objects_enabled )
458
459
            return;
    }
460

461
    /* Send the message to stderr */
462
463
464
465
466
467
468
    FILE *stream = stderr;
    int canc = vlc_savecancel ();

    flockfile (stream);
    fprintf (stream, priv->b_color ? "["GREEN"%p"GRAY"] " : "[%p] ",
            (void *)p_item->i_object_id);
    if (p_item->psz_header != NULL)
469
        utf8_fprintf (stream, "[%s] ", p_item->psz_header);
470
    utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module, objtype,
471
                  msgtype[type]);
472
    if (priv->b_color)
473
        fputs (msgcolor[type], stream);
474
475
476
477
478
479
    fputs (p_item->psz_msg, stream);
    if (priv->b_color)
        fputs (GRAY, stream);
    putc_unlocked ('\n', stream);
#if defined (WIN32) || defined (__OS2__)
    fflush (stream);
480
#endif
481
    funlockfile (stream);
482
    vlc_restorecancel (canc);
483
}