]> git.sesse.net Git - vlc/blob - src/misc/messages.c
Remove picture quantization table (same reason as qtype)
[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 <stdarg.h>                                       /* va_list for BSD */
36 #ifdef __APPLE__
37 # include <xlocale.h>
38 #elif defined(HAVE_LOCALE_H)
39 # include <locale.h>
40 #endif
41 #include <errno.h>                                                  /* errno */
42 #include <assert.h>
43
44 #include <vlc_common.h>
45 #include <vlc_interface.h>
46 #ifdef WIN32
47 #   include <vlc_network.h>          /* 'net_strerror' and 'WSAGetLastError' */
48 #endif
49 #include <vlc_charset.h>
50 #include "../libvlc.h"
51
52 /**
53  * Store all data required by messages interfaces.
54  */
55 vlc_rwlock_t msg_lock = VLC_STATIC_RWLOCK;
56 msg_subscription_t *msg_head;
57
58 /**
59  * Subscribe to the message queue.
60  * Whenever a message is emitted, a callback will be called.
61  * Callback invocation are serialized within a subscription.
62  *
63  * @param cb callback function
64  * @param opaque data for the callback function
65  */
66 void vlc_Subscribe (msg_subscription_t *sub, msg_callback_t cb, void *opaque)
67 {
68     sub->prev = NULL;
69     sub->func = cb;
70     sub->opaque = opaque;
71
72     vlc_rwlock_wrlock (&msg_lock);
73     sub->next = msg_head;
74     msg_head = sub;
75     vlc_rwlock_unlock (&msg_lock);
76 }
77
78 /**
79  * Unsubscribe from the message queue.
80  * This function waits for the message callback to return if needed.
81  */
82 void vlc_Unsubscribe (msg_subscription_t *sub)
83 {
84     vlc_rwlock_wrlock (&msg_lock);
85     if (sub->next != NULL)
86         sub->next->prev = sub->prev;
87     if (sub->prev != NULL)
88         sub->prev->next = sub->next;
89     else
90     {
91         assert (msg_head == sub);
92         msg_head = sub->next;
93     }
94     vlc_rwlock_unlock (&msg_lock);
95 }
96
97 /**
98  * Emit a log message.
99  * \param obj VLC object emitting the message or NULL
100  * \param type VLC_MSG_* message type (info, error, warning or debug)
101  * \param module name of module from which the message come
102  *               (normally MODULE_STRING)
103  * \param format printf-like message format
104  */
105 void vlc_Log (vlc_object_t *obj, int type, const char *module,
106               const char *format, ... )
107 {
108     va_list args;
109
110     va_start (args, format);
111     vlc_vaLog (obj, type, module, format, args);
112     va_end (args);
113 }
114
115 static void PrintColorMsg (void *, int, const msg_item_t *,
116                            const char *, va_list);
117 static void PrintMsg (void *, int, const msg_item_t *, const char *, va_list);
118 #ifdef WIN32
119 static void Win32DebugOutputMsg (void *, int , const msg_item_t *,
120                                  const char *, va_list);
121 #endif
122
123 /**
124  * Emit a log message. This function is the variable argument list equivalent
125  * to vlc_Log().
126  */
127 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
128                 const char *format, va_list args)
129 {
130     if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
131         return;
132
133     /* C locale to get error messages in English in the logs */
134     locale_t c = newlocale (LC_MESSAGES_MASK, "C", (locale_t)0);
135     locale_t locale = uselocale (c);
136
137 #ifndef __GLIBC__
138     /* Expand %m to strerror(errno) - only once */
139     char buf[strlen(format) + 2001], *ptr;
140     strcpy (buf, format);
141     ptr = (char*)buf;
142     format = (const char*) buf;
143
144     for( ;; )
145     {
146         ptr = strchr( ptr, '%' );
147         if( ptr == NULL )
148             break;
149
150         if( ptr[1] == 'm' )
151         {
152             char errbuf[2001];
153             size_t errlen;
154
155 #ifndef WIN32
156             strerror_r( errno, errbuf, 1001 );
157 #else
158             int sockerr = WSAGetLastError( );
159             if( sockerr )
160             {
161                 strncpy( errbuf, net_strerror( sockerr ), 1001 );
162                 WSASetLastError( sockerr );
163             }
164             if ((sockerr == 0)
165              || (strcmp ("Unknown network stack error", errbuf) == 0))
166                 strncpy( errbuf, strerror( errno ), 1001 );
167 #endif
168             errbuf[1000] = 0;
169
170             /* Escape '%' from the error string */
171             for( char *percent = strchr( errbuf, '%' );
172                  percent != NULL;
173                  percent = strchr( percent + 2, '%' ) )
174             {
175                 memmove( percent + 1, percent, strlen( percent ) + 1 );
176             }
177
178             errlen = strlen( errbuf );
179             memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
180             memcpy( ptr, errbuf, errlen );
181             break; /* Only once, so we don't overflow */
182         }
183
184         /* Looks for conversion specifier... */
185         do
186             ptr++;
187         while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
188         if( *ptr )
189             ptr++; /* ...and skip it */
190     }
191 #endif
192
193     /* Fill message information fields */
194     msg_item_t msg;
195
196     msg.i_object_id = (uintptr_t)obj;
197     msg.psz_object_type = (obj != NULL) ? obj->psz_object_type : "generic";
198     msg.psz_module = module;
199     msg.psz_header = NULL;
200
201     for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
202         if (o->psz_header != NULL)
203         {
204             msg.psz_header = o->psz_header;
205             break;
206         }
207
208     /* Pass message to subscribers */
209     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
210
211     va_list ap;
212
213     va_copy (ap, args);
214     if (priv->b_color)
215         PrintColorMsg (&priv->i_verbose, type, &msg, format, ap);
216     else
217         PrintMsg (&priv->i_verbose, type, &msg, format, ap);
218     va_end (ap);
219
220 #ifdef WIN32
221     va_copy (ap, args);
222     Win32DebugOutputMsg (&priv->i_verbose, type, &msg, format, ap);
223     va_end (ap);
224 #endif
225
226     vlc_rwlock_rdlock (&msg_lock);
227     for (msg_subscription_t *sub = msg_head; sub != NULL; sub = sub->next)
228     {
229         va_copy (ap, args);
230         sub->func (sub->opaque, type, &msg, format, ap);
231         va_end (ap);
232     }
233     vlc_rwlock_unlock (&msg_lock);
234
235     uselocale (locale);
236     freelocale (c);
237 }
238
239 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
240 #define COL(x,y)  "\033[" #x ";" #y "m"
241 #define RED     COL(31,1)
242 #define GREEN   COL(32,1)
243 #define YELLOW  COL(0,33)
244 #define WHITE   COL(0,1)
245 #define GRAY    "\033[0m"
246 static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY };
247
248 static void PrintColorMsg (void *d, int type, const msg_item_t *p_item,
249                            const char *format, va_list ap)
250 {
251     const signed char *pverbose = d;
252     FILE *stream = stderr;
253
254     if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
255         return;
256
257     int canc = vlc_savecancel ();
258
259     flockfile (stream);
260     fprintf (stream, "["GREEN"%p"GRAY"] ", (void *)p_item->i_object_id);
261     if (p_item->psz_header != NULL)
262         utf8_fprintf (stream, "[%s] ", p_item->psz_header);
263     utf8_fprintf (stream, "%s %s%s: %s", p_item->psz_module,
264                   p_item->psz_object_type, msg_type[type], msg_color[type]);
265     utf8_vfprintf (stream, format, ap);
266     fputs (GRAY"\n", stream);
267 #if defined (WIN32) || defined (__OS2__)
268     fflush (stream);
269 #endif
270     funlockfile (stream);
271     vlc_restorecancel (canc);
272 }
273
274 static void PrintMsg (void *d, int type, const msg_item_t *p_item,
275                       const char *format, va_list ap)
276 {
277     const signed char *pverbose = d;
278     FILE *stream = stderr;
279
280     if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
281         return;
282
283     int canc = vlc_savecancel ();
284
285     flockfile (stream);
286     fprintf (stream, "[%p] ", (void *)p_item->i_object_id);
287     if (p_item->psz_header != NULL)
288         utf8_fprintf (stream, "[%s] ", p_item->psz_header);
289     utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module,
290                   p_item->psz_object_type, msg_type[type]);
291     utf8_vfprintf (stream, format, ap);
292     putc_unlocked ('\n', stream);
293 #if defined (WIN32) || defined (__OS2__)
294     fflush (stream);
295 #endif
296     funlockfile (stream);
297     vlc_restorecancel (canc);
298 }
299
300 #ifdef WIN32
301 static void Win32DebugOutputMsg (void* d, int type, const msg_item_t *p_item,
302                                  const char *format, va_list dol)
303 {
304     VLC_UNUSED(p_item);
305
306     const signed char *pverbose = d;
307     if (pverbose && (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)))
308         return;
309
310     va_list dol2;
311     va_copy (dol2, dol);
312     int msg_len = vsnprintf(NULL, 0, format, dol2);
313     va_end (dol2);
314
315     if(msg_len <= 0)
316         return;
317
318     char *msg = malloc(msg_len + 1 + 1);
319     if (!msg)
320         return;
321
322     msg_len = vsnprintf(msg, msg_len+1, format, dol);
323     if (msg_len > 0){
324         if(msg[msg_len-1] != '\n'){
325             msg[msg_len] = '\n';
326             msg[msg_len + 1] = '\0';
327         }
328         char* psz_msg = NULL;
329         if(asprintf(&psz_msg, "%s %s%s: %s", p_item->psz_module,
330                     p_item->psz_object_type, msg_type[type], msg) > 0) {
331             wchar_t* wmsg = ToWide(psz_msg);
332             OutputDebugStringW(wmsg);
333             free(wmsg);
334             free(psz_msg);
335         }
336     }
337     free(msg);
338 }
339 #endif