messages.c 13.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

49
50
#include <assert.h>

zorglub's avatar
zorglub committed
51
#include <vlc_charset.h>
52
#include "../libvlc.h"
53

54
55
static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
{
56
    return (libvlc_priv (inst))->msg_bank;
57
}
58

Vincent Seguin's avatar
Vincent Seguin committed
59
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
60
 * Local prototypes
Vincent Seguin's avatar
Vincent Seguin committed
61
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
62
static void PrintMsg ( vlc_object_t *, const msg_item_t * );
Michel Kaempf's avatar
Michel Kaempf committed
63

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * 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
80
/**
81
82
 * Initialize messages queues
 * This function initializes all message queues
sigmunau's avatar
all:    
sigmunau committed
83
 */
84
msg_bank_t *msg_Create (void)
Michel Kaempf's avatar
Michel Kaempf committed
85
{
86
    msg_bank_t *bank = malloc (sizeof (*bank));
87

88
    vlc_rwlock_init (&bank->lock);
89
90
    vlc_dictionary_init (&bank->enabled_objects, 0);
    bank->all_objects_enabled = true;
91

92
93
    bank->i_sub = 0;
    bank->pp_sub = NULL;
94

95
    return bank;
Michel Kaempf's avatar
Michel Kaempf committed
96
97
}

98
99
100
/**
 * Object Printing selection
 */
101
102
static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
103

104

105
106
#undef msg_EnableObjectPrinting
void msg_EnableObjectPrinting (vlc_object_t *obj, const char * psz_object)
107
{
108
109
110
    msg_bank_t *bank = libvlc_bank (obj->p_libvlc);

    vlc_rwlock_wrlock (&bank->lock);
111
    if( !strcmp(psz_object, "all") )
112
        bank->all_objects_enabled = true;
113
    else
114
115
116
        vlc_dictionary_insert (&bank->enabled_objects, psz_object,
                               (void *)kObjectPrintingEnabled);
    vlc_rwlock_unlock (&bank->lock);
117
118
}

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

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

sigmunau's avatar
all:    
sigmunau committed
133
/**
134
 * Destroy the message queues
sigmunau's avatar
all:    
sigmunau committed
135
 *
136
 * This functions prints all messages remaining in the queues,
137
 * then frees all the allocated resources
Michel Kaempf's avatar
Michel Kaempf committed
138
 * No other messages interface functions should be called after this one.
sigmunau's avatar
all:    
sigmunau committed
139
 */
140
void msg_Destroy (msg_bank_t *bank)
Michel Kaempf's avatar
Michel Kaempf committed
141
{
142
143
    if (unlikely(bank->i_sub != 0))
        fputs ("stale interface subscribers (LibVLC might crash)\n", stderr);
144

145
    vlc_dictionary_clear (&bank->enabled_objects, NULL, NULL);
146

147
    vlc_rwlock_destroy (&bank->lock);
148
    free (bank);
149
150
151
152
153
154
155
}

struct msg_subscription_t
{
    libvlc_int_t   *instance;
    msg_callback_t  func;
    msg_cb_data_t  *opaque;
156
    int             verbosity;
157
158
};

sigmunau's avatar
all:    
sigmunau committed
159
/**
160
161
162
163
164
165
166
167
 * 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
168
 */
169
170
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
171
{
172
173
    msg_subscription_t *sub = malloc (sizeof (*sub));
    if (sub == NULL)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
174
175
        return NULL;

176
177
178
    sub->instance = instance;
    sub->func = cb;
    sub->opaque = opaque;
179
    sub->verbosity = 2; /* by default, give all the messages */
Sam Hocevar's avatar
   
Sam Hocevar committed
180

181
    msg_bank_t *bank = libvlc_bank (instance);
182
    vlc_rwlock_wrlock (&bank->lock);
183
    TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
184
    vlc_rwlock_unlock (&bank->lock);
Sam Hocevar's avatar
   
Sam Hocevar committed
185

186
    return sub;
Sam Hocevar's avatar
   
Sam Hocevar committed
187
188
}

sigmunau's avatar
all:    
sigmunau committed
189
/**
190
191
 * Unsubscribe from the message queue.
 * This function waits for the message callback to return if needed.
sigmunau's avatar
all:    
sigmunau committed
192
 */
193
void msg_Unsubscribe (msg_subscription_t *sub)
Sam Hocevar's avatar
   
Sam Hocevar committed
194
{
195
    msg_bank_t *bank = libvlc_bank (sub->instance);
Sam Hocevar's avatar
   
Sam Hocevar committed
196

197
    vlc_rwlock_wrlock (&bank->lock);
198
    TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
199
    vlc_rwlock_unlock (&bank->lock);
200
    free (sub);
Michel Kaempf's avatar
Michel Kaempf committed
201
202
}

203
204
205
206
207
208
209
210
211
212
213
214
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);
}
215
/*****************************************************************************
216
 * msg_*: print a message
217
 *****************************************************************************
218
 * These functions queue a message for later printing.
219
 *****************************************************************************/
220
void msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
221
                    const char *psz_format, ... )
Michel Kaempf's avatar
Michel Kaempf committed
222
{
223
    va_list args;
Michel Kaempf's avatar
Michel Kaempf committed
224

225
    va_start( args, psz_format );
226
    msg_GenericVa (p_this, i_type, psz_module, psz_format, args);
227
    va_end( args );
Michel Kaempf's avatar
Michel Kaempf committed
228
}
229

230
#undef msg_GenericVa
sigmunau's avatar
all:    
sigmunau committed
231
232
233
/**
 * Add a message to a queue
 *
234
 * This function provides basic functionnalities to other msg_* functions.
235
236
237
 * 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
238
 */
239
240
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
241
{
242
243
    assert (p_this);

244
    if( p_this->i_flags & OBJECT_FLAGS_QUIET )
245
        return;
246

247
    msg_bank_t *bank = libvlc_bank (p_this->p_libvlc);
248
249
250
251

    /* 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);
252

253
254
255
256
#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
257
258
    ptr = (char*)buf;
    psz_format = (const char*) buf;
259
260
261
262
263
264
265
266
267
268
269
270

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

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

271
#ifndef WIN32
272
            strerror_r( errno, errbuf, 1001 );
273
274
275
276
277
278
279
#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
280
281
            if ((sockerr == 0)
             || (strcmp ("Unknown network stack error", errbuf) == 0))
282
283
                strncpy( errbuf, strerror( errno ), 1001 );
#endif
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
            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

309
    /* Convert message to string  */
310
311
312
313
314
    static const char nomemstr[] = "<not enough memory to format message>";
    char *str;

    if (unlikely(vasprintf (&str, psz_format, args) == -1))
        str = (char *)nomemstr;
Sam Hocevar's avatar
   
Sam Hocevar committed
315

316
317
318
    uselocale (locale);
    freelocale (c);

319
320
    /* Fill message information fields */
    msg_item_t msg;
321

322
323
324
    msg.i_type = i_type;
    msg.i_object_id = (uintptr_t)p_this;
    msg.psz_object_type = p_this->psz_object_type;
325
    msg.psz_module = psz_module;
326
    msg.psz_msg = str;
327
    msg.psz_header = NULL;
328

329
330
    for (vlc_object_t *o = p_this; o != NULL; o = o->p_parent)
        if (o->psz_header != NULL)
331
        {
332
            msg.psz_header = o->psz_header;
333
            break;
334
        }
335

336
    PrintMsg( p_this, &msg );
337

338
    vlc_rwlock_rdlock (&bank->lock);
339
    for (int i = 0; i < bank->i_sub; i++)
340
    {
341
        msg_subscription_t *sub = bank->pp_sub[i];
342
343
344
        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,
345
                                                  msg.psz_module );
346
347
348
349
        if( val == kObjectPrintingDisabled ) continue;
        if( val != kObjectPrintingEnabled  ) /*if not allowed */
        {
            val = vlc_dictionary_value_for_key( &bank->enabled_objects,
350
                                                msg.psz_object_type );
351
352
353
354
            if( val == kObjectPrintingDisabled ) continue;
            if( val == kObjectPrintingEnabled  ); /* Allowed */
            else if( !bank->all_objects_enabled ) continue;
        }
355
        switch( msg.i_type )
356
357
358
359
360
361
362
363
364
365
366
367
        {
            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;
        }
368

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
369
        sub->func (sub->opaque, &msg);
370
    }
371
    vlc_rwlock_unlock (&bank->lock);
372
373
374

    if (likely(str != (char *)nomemstr))
        free (str);
Michel Kaempf's avatar
Michel Kaempf committed
375
376
}

377
/*****************************************************************************
378
 * PrintMsg: output a standard message item to stderr
379
380
381
 *****************************************************************************
 * Print a message to stderr, with colour formatting if needed.
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
382
static void PrintMsg ( vlc_object_t *p_this, const msg_item_t *p_item )
383
{
sebastien's avatar
sebastien committed
384
385
386
387
388
#   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)
389
#   define GRAY    "\033[0m"
390
391
    static const char msgtype[4][9] = { "", " error", " warning", " debug" };
    static const char msgcolor[4][8] = { WHITE, RED, YELLOW, GRAY };
392

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

396
397
    if (priv->i_verbose < 0 || priv->i_verbose < (type - VLC_MSG_ERR))
        return;
398

399
    const char *objtype = p_item->psz_object_type;
400
    msg_bank_t *bank = priv->msg_bank;
401
402
    void * val = vlc_dictionary_value_for_key (&bank->enabled_objects,
                                               p_item->psz_module);
403
404
405
406
    if( val == kObjectPrintingDisabled )
        return;
    if( val == kObjectPrintingEnabled )
        /* Allowed */;
407
408
    else
    {
409
        val = vlc_dictionary_value_for_key (&bank->enabled_objects,
410
                                            objtype);
411
412
413
414
        if( val == kObjectPrintingDisabled )
            return;
        if( val == kObjectPrintingEnabled )
            /* Allowed */;
415
        else if( !bank->all_objects_enabled )
416
417
            return;
    }
418

419
    /* Send the message to stderr */
420
421
422
423
424
425
426
    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)
427
        utf8_fprintf (stream, "[%s] ", p_item->psz_header);
428
    utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module, objtype,
429
                  msgtype[type]);
430
    if (priv->b_color)
431
        fputs (msgcolor[type], stream);
432
433
434
435
436
437
    fputs (p_item->psz_msg, stream);
    if (priv->b_color)
        fputs (GRAY, stream);
    putc_unlocked ('\n', stream);
#if defined (WIN32) || defined (__OS2__)
    fflush (stream);
438
#endif
439
    funlockfile (stream);
440
    vlc_restorecancel (canc);
441
}