]> git.sesse.net Git - vlc/blob - src/misc/messages.c
LGPL
[vlc] / src / misc / messages.c
1 /*****************************************************************************
2  * messages.c: messages interface
3  * 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  *****************************************************************************
6  * Copyright (C) 1998-2005 VLC authors and VideoLAN
7  * $Id$
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36
37 #include <stdarg.h>                                       /* va_list for BSD */
38 #ifdef __APPLE__
39 # include <xlocale.h>
40 #elif defined(HAVE_LOCALE_H)
41 # include <locale.h>
42 #endif
43 #include <errno.h>                                                  /* errno */
44
45 #ifdef WIN32
46 #   include <vlc_network.h>          /* 'net_strerror' and 'WSAGetLastError' */
47 #endif
48
49 #include <assert.h>
50
51 #include <vlc_charset.h>
52 #include "../libvlc.h"
53
54 /**
55  * Store all data required by messages interfaces.
56  */
57 vlc_rwlock_t msg_lock = VLC_STATIC_RWLOCK;
58 msg_subscription_t *msg_head;
59
60 struct msg_subscription_t
61 {
62     msg_subscription_t *prev, *next;
63     msg_callback_t  func;
64     void           *opaque;
65 };
66
67 /**
68  * Subscribe to the message queue.
69  * Whenever a message is emitted, a callback will be called.
70  * Callback invocation are serialized within a subscription.
71  *
72  * @param cb callback function
73  * @param opaque data for the callback function
74  * @return a subscription pointer, or NULL in case of failure
75  */
76 msg_subscription_t *vlc_Subscribe (msg_callback_t cb, void *opaque)
77 {
78     msg_subscription_t *sub = malloc (sizeof (*sub));
79     if (sub == NULL)
80         return NULL;
81
82     sub->prev = NULL;
83     sub->func = cb;
84     sub->opaque = opaque;
85
86     vlc_rwlock_wrlock (&msg_lock);
87     sub->next = msg_head;
88     msg_head = sub;
89     vlc_rwlock_unlock (&msg_lock);
90
91     return sub;
92 }
93
94 /**
95  * Unsubscribe from the message queue.
96  * This function waits for the message callback to return if needed.
97  */
98 void vlc_Unsubscribe (msg_subscription_t *sub)
99 {
100     vlc_rwlock_wrlock (&msg_lock);
101     if (sub->next != NULL)
102         sub->next->prev = sub->prev;
103     if (sub->prev != NULL)
104         sub->prev->next = sub->next;
105     else
106     {
107         assert (msg_head == sub);
108         msg_head = sub->next;
109     }
110     vlc_rwlock_unlock (&msg_lock);
111     free (sub);
112 }
113
114 /**
115  * Emit a log message.
116  * \param obj VLC object emitting the message or NULL
117  * \param type VLC_MSG_* message type (info, error, warning or debug)
118  * \param module name of module from which the message come
119  *               (normally MODULE_STRING)
120  * \param format printf-like message format
121  */
122 void vlc_Log (vlc_object_t *obj, int type, const char *module,
123               const char *format, ... )
124 {
125     va_list args;
126
127     va_start (args, format);
128     vlc_vaLog (obj, type, module, format, args);
129     va_end (args);
130 }
131
132 static void PrintColorMsg (void *, int, const msg_item_t *,
133                            const char *, va_list);
134 static void PrintMsg (void *, int, const msg_item_t *, const char *, va_list);
135
136 /**
137  * Emit a log message. This function is the variable argument list equivalent
138  * to vlc_Log().
139  */
140 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
141                 const char *format, va_list args)
142 {
143     if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
144         return;
145
146     /* C locale to get error messages in English in the logs */
147     locale_t c = newlocale (LC_MESSAGES_MASK, "C", (locale_t)0);
148     locale_t locale = uselocale (c);
149
150 #ifndef __GLIBC__
151     /* Expand %m to strerror(errno) - only once */
152     char buf[strlen(format) + 2001], *ptr;
153     strcpy (buf, format);
154     ptr = (char*)buf;
155     format = (const char*) buf;
156
157     for( ;; )
158     {
159         ptr = strchr( ptr, '%' );
160         if( ptr == NULL )
161             break;
162
163         if( ptr[1] == 'm' )
164         {
165             char errbuf[2001];
166             size_t errlen;
167
168 #ifndef WIN32
169             strerror_r( errno, errbuf, 1001 );
170 #else
171             int sockerr = WSAGetLastError( );
172             if( sockerr )
173             {
174                 strncpy( errbuf, net_strerror( sockerr ), 1001 );
175                 WSASetLastError( sockerr );
176             }
177             if ((sockerr == 0)
178              || (strcmp ("Unknown network stack error", errbuf) == 0))
179                 strncpy( errbuf, strerror( errno ), 1001 );
180 #endif
181             errbuf[1000] = 0;
182
183             /* Escape '%' from the error string */
184             for( char *percent = strchr( errbuf, '%' );
185                  percent != NULL;
186                  percent = strchr( percent + 2, '%' ) )
187             {
188                 memmove( percent + 1, percent, strlen( percent ) + 1 );
189             }
190
191             errlen = strlen( errbuf );
192             memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
193             memcpy( ptr, errbuf, errlen );
194             break; /* Only once, so we don't overflow */
195         }
196
197         /* Looks for conversion specifier... */
198         do
199             ptr++;
200         while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
201         if( *ptr )
202             ptr++; /* ...and skip it */
203     }
204 #endif
205
206     /* Fill message information fields */
207     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     /* Pass message to subscribers */
222     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
223
224     va_list ap;
225
226     va_copy (ap, args);
227     if (priv->b_color)
228         PrintColorMsg (&priv->i_verbose, type, &msg, format, ap);
229     else
230         PrintMsg (&priv->i_verbose, type, &msg, format, ap);
231     va_end (ap);
232
233     vlc_rwlock_rdlock (&msg_lock);
234     for (msg_subscription_t *sub = msg_head; sub != NULL; sub = sub->next)
235     {
236         va_copy (ap, args);
237         sub->func (sub->opaque, type, &msg, format, ap);
238         va_end (ap);
239     }
240     vlc_rwlock_unlock (&msg_lock);
241
242     uselocale (locale);
243     freelocale (c);
244 }
245
246 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
247 #define COL(x,y)  "\033[" #x ";" #y "m"
248 #define RED     COL(31,1)
249 #define GREEN   COL(32,1)
250 #define YELLOW  COL(0,33)
251 #define WHITE   COL(0,1)
252 #define GRAY    "\033[0m"
253 static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY };
254
255 static void PrintColorMsg (void *d, int type, const msg_item_t *p_item,
256                            const char *format, va_list ap)
257 {
258     const int *pverbose = d;
259     FILE *stream = stderr;
260
261     if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
262         return;
263
264     int canc = vlc_savecancel ();
265
266     flockfile (stream);
267     fprintf (stream, "["GREEN"%p"GRAY"] ", (void *)p_item->i_object_id);
268     if (p_item->psz_header != NULL)
269         utf8_fprintf (stream, "[%s] ", p_item->psz_header);
270     utf8_fprintf (stream, "%s %s%s: %s", p_item->psz_module,
271                   p_item->psz_object_type, msg_type[type], msg_color[type]);
272     utf8_vfprintf (stream, format, ap);
273     fputs (GRAY"\n", stream);
274 #if defined (WIN32) || defined (__OS2__)
275     fflush (stream);
276 #endif
277     funlockfile (stream);
278     vlc_restorecancel (canc);
279 }
280
281 static void PrintMsg (void *d, int type, const msg_item_t *p_item,
282                       const char *format, va_list ap)
283 {
284     const int *pverbose = d;
285     FILE *stream = stderr;
286
287     if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
288         return;
289
290     int canc = vlc_savecancel ();
291
292     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     utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module,
297                   p_item->psz_object_type, msg_type[type]);
298     utf8_vfprintf (stream, format, ap);
299     putc_unlocked ('\n', stream);
300 #if defined (WIN32) || defined (__OS2__)
301     fflush (stream);
302 #endif
303     funlockfile (stream);
304     vlc_restorecancel (canc);
305 }