messages.c 8.95 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-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
6
 * Copyright (C) 1998-2005 VLC authors and VideoLAN
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
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
12
13
14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15
 * (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
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
19
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
22
23
24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software 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
56
/**
 * Store all data required by messages interfaces.
 */
57
58
vlc_rwlock_t msg_lock = VLC_STATIC_RWLOCK;
msg_subscription_t *msg_head;
59
60
61

struct msg_subscription_t
{
62
    msg_subscription_t *prev, *next;
63
    msg_callback_t  func;
64
    void           *opaque;
65
66
};

sigmunau's avatar
all:    
sigmunau committed
67
/**
68
69
70
71
72
73
74
 * Subscribe to the message queue.
 * Whenever a message is emitted, a callback will be called.
 * Callback invocation are serialized within a subscription.
 *
 * @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
75
 */
76
msg_subscription_t *vlc_Subscribe (msg_callback_t cb, void *opaque)
Sam Hocevar's avatar
   
Sam Hocevar committed
77
{
78
79
    msg_subscription_t *sub = malloc (sizeof (*sub));
    if (sub == NULL)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
80
81
        return NULL;

82
    sub->prev = NULL;
83
84
    sub->func = cb;
    sub->opaque = opaque;
Sam Hocevar's avatar
   
Sam Hocevar committed
85

86
87
88
89
    vlc_rwlock_wrlock (&msg_lock);
    sub->next = msg_head;
    msg_head = sub;
    vlc_rwlock_unlock (&msg_lock);
Sam Hocevar's avatar
   
Sam Hocevar committed
90

91
    return sub;
Sam Hocevar's avatar
   
Sam Hocevar committed
92
93
}

sigmunau's avatar
all:    
sigmunau committed
94
/**
95
96
 * Unsubscribe from the message queue.
 * This function waits for the message callback to return if needed.
sigmunau's avatar
all:    
sigmunau committed
97
 */
98
void vlc_Unsubscribe (msg_subscription_t *sub)
Sam Hocevar's avatar
   
Sam Hocevar committed
99
{
100
101
102
103
104
105
106
107
108
109
110
    vlc_rwlock_wrlock (&msg_lock);
    if (sub->next != NULL)
        sub->next->prev = sub->prev;
    if (sub->prev != NULL)
        sub->prev->next = sub->next;
    else
    {
        assert (msg_head == sub);
        msg_head = sub->next;
    }
    vlc_rwlock_unlock (&msg_lock);
111
    free (sub);
Michel Kaempf's avatar
Michel Kaempf committed
112
113
}

114
115
/**
 * Emit a log message.
116
 * \param obj VLC object emitting the message or NULL
117
118
119
120
121
122
123
 * \param type VLC_MSG_* message type (info, error, warning or debug)
 * \param module name of module from which the message come
 *               (normally MODULE_STRING)
 * \param format printf-like message format
 */
void vlc_Log (vlc_object_t *obj, int type, const char *module,
              const char *format, ... )
Michel Kaempf's avatar
Michel Kaempf committed
124
{
125
    va_list args;
Michel Kaempf's avatar
Michel Kaempf committed
126

127
128
129
    va_start (args, format);
    vlc_vaLog (obj, type, module, format, args);
    va_end (args);
Michel Kaempf's avatar
Michel Kaempf committed
130
}
131

132
133
134
static void PrintColorMsg (void *, int, const msg_item_t *,
                           const char *, va_list);
static void PrintMsg (void *, int, const msg_item_t *, const char *, va_list);
135

sigmunau's avatar
all:    
sigmunau committed
136
/**
137
138
 * Emit a log message. This function is the variable argument list equivalent
 * to vlc_Log().
sigmunau's avatar
all:    
sigmunau committed
139
 */
140
141
void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
                const char *format, va_list args)
Michel Kaempf's avatar
Michel Kaempf committed
142
{
143
    if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
144
        return;
145

146
147
148
    /* 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);
149

150
151
#ifndef __GLIBC__
    /* Expand %m to strerror(errno) - only once */
152
153
    char buf[strlen(format) + 2001], *ptr;
    strcpy (buf, format);
Rafaël Carré's avatar
Rafaël Carré committed
154
    ptr = (char*)buf;
155
    format = (const char*) buf;
156
157
158
159
160
161
162
163
164
165
166
167

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

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

168
#ifndef WIN32
169
            strerror_r( errno, errbuf, 1001 );
170
171
172
173
174
175
176
#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
177
178
            if ((sockerr == 0)
             || (strcmp ("Unknown network stack error", errbuf) == 0))
179
180
                strncpy( errbuf, strerror( errno ), 1001 );
#endif
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
            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

206
207
    /* Fill message information fields */
    msg_item_t msg;
208

209
    msg.i_object_id = (uintptr_t)obj;
210
    msg.psz_object_type = (obj != NULL) ? obj->psz_object_type : "generic";
211
    msg.psz_module = module;
212
    msg.psz_header = NULL;
213

214
    for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
215
        if (o->psz_header != NULL)
216
        {
217
            msg.psz_header = o->psz_header;
218
            break;
219
        }
220

221
222
223
    /* Pass message to subscribers */
    libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);

224
225
226
    va_list ap;

    va_copy (ap, args);
227
    if (priv->b_color)
228
        PrintColorMsg (&priv->i_verbose, type, &msg, format, ap);
229
    else
230
231
        PrintMsg (&priv->i_verbose, type, &msg, format, ap);
    va_end (ap);
232

233
234
    vlc_rwlock_rdlock (&msg_lock);
    for (msg_subscription_t *sub = msg_head; sub != NULL; sub = sub->next)
235
236
237
238
239
    {
        va_copy (ap, args);
        sub->func (sub->opaque, type, &msg, format, ap);
        va_end (ap);
    }
240
    vlc_rwlock_unlock (&msg_lock);
241

242
243
    uselocale (locale);
    freelocale (c);
Michel Kaempf's avatar
Michel Kaempf committed
244
245
}

246
247
248
249
250
251
252
253
254
static const char msg_type[4][9] = { "", " error", " warning", " debug" };
#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)
#define GRAY    "\033[0m"
static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY };

255
256
static void PrintColorMsg (void *d, int type, const msg_item_t *p_item,
                           const char *format, va_list ap)
257
{
258
259
    const int *pverbose = d;
    FILE *stream = stderr;
260

261
    if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
262
        return;
263

264
265
266
267
268
269
    int canc = vlc_savecancel ();

    flockfile (stream);
    fprintf (stream, "["GREEN"%p"GRAY"] ", (void *)p_item->i_object_id);
    if (p_item->psz_header != NULL)
        utf8_fprintf (stream, "[%s] ", p_item->psz_header);
270
271
272
273
    utf8_fprintf (stream, "%s %s%s: %s", p_item->psz_module,
                  p_item->psz_object_type, msg_type[type], msg_color[type]);
    utf8_vfprintf (stream, format, ap);
    fputs (GRAY"\n", stream);
274
275
276
277
278
279
280
#if defined (WIN32) || defined (__OS2__)
    fflush (stream);
#endif
    funlockfile (stream);
    vlc_restorecancel (canc);
}

281
282
static void PrintMsg (void *d, int type, const msg_item_t *p_item,
                      const char *format, va_list ap)
283
284
{
    const int *pverbose = d;
285
    FILE *stream = stderr;
286
287
288
289

    if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
        return;

290
291
292
    int canc = vlc_savecancel ();

    flockfile (stream);
293
    fprintf (stream, "[%p] ", (void *)p_item->i_object_id);
294
    if (p_item->psz_header != NULL)
295
        utf8_fprintf (stream, "[%s] ", p_item->psz_header);
296
297
298
    utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module,
                  p_item->psz_object_type, msg_type[type]);
    utf8_vfprintf (stream, format, ap);
299
    putc_unlocked ('\n', stream);
300
301
#if defined (WIN32) || defined (__OS2__)
    fflush (stream);
302
#endif
303
    funlockfile (stream);
304
    vlc_restorecancel (canc);
305
}