messages.c 15.1 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
    if( p_this->i_flags & OBJECT_FLAGS_QUIET )
270
        return;
271

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

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

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

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

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

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

Michel Kaempf's avatar
Michel Kaempf committed
337
338
    if( psz_str == NULL )
    {
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
        uselocale (locale);
361
        return;
Michel Kaempf's avatar
Michel Kaempf committed
362
    }
363
    uselocale (locale);
Sam Hocevar's avatar
   
Sam Hocevar committed
364

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

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

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

382
    PrintMsg( p_this, &msg );
383

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

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

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

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

440
441
    if (priv->i_verbose < 0 || priv->i_verbose < (type - VLC_MSG_ERR))
        return;
442

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

463
    /* Send the message to stderr */
464
465
466
467
468
469
470
    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)
471
        utf8_fprintf (stream, "[%s] ", p_item->psz_header);
472
    utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module, objtype,
473
                  msgtype[type]);
474
    if (priv->b_color)
475
        fputs (msgcolor[type], stream);
476
477
478
479
480
481
    fputs (p_item->psz_msg, stream);
    if (priv->b_color)
        fputs (GRAY, stream);
    putc_unlocked ('\n', stream);
#if defined (WIN32) || defined (__OS2__)
    fflush (stream);
482
#endif
483
    funlockfile (stream);
484
    vlc_restorecancel (canc);
485
}