]> git.sesse.net Git - vlc/blob - src/misc/messages.c
Inline SaveConfigFile()
[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 #ifdef WIN32
136 static void Win32DebugOutputMsg (void *, int , const msg_item_t *,
137                                  const char *, va_list);
138 #endif
139
140 /**
141  * Emit a log message. This function is the variable argument list equivalent
142  * to vlc_Log().
143  */
144 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
145                 const char *format, va_list args)
146 {
147     if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
148         return;
149
150     /* C locale to get error messages in English in the logs */
151     locale_t c = newlocale (LC_MESSAGES_MASK, "C", (locale_t)0);
152     locale_t locale = uselocale (c);
153
154 #ifndef __GLIBC__
155     /* Expand %m to strerror(errno) - only once */
156     char buf[strlen(format) + 2001], *ptr;
157     strcpy (buf, format);
158     ptr = (char*)buf;
159     format = (const char*) buf;
160
161     for( ;; )
162     {
163         ptr = strchr( ptr, '%' );
164         if( ptr == NULL )
165             break;
166
167         if( ptr[1] == 'm' )
168         {
169             char errbuf[2001];
170             size_t errlen;
171
172 #ifndef WIN32
173             strerror_r( errno, errbuf, 1001 );
174 #else
175             int sockerr = WSAGetLastError( );
176             if( sockerr )
177             {
178                 strncpy( errbuf, net_strerror( sockerr ), 1001 );
179                 WSASetLastError( sockerr );
180             }
181             if ((sockerr == 0)
182              || (strcmp ("Unknown network stack error", errbuf) == 0))
183                 strncpy( errbuf, strerror( errno ), 1001 );
184 #endif
185             errbuf[1000] = 0;
186
187             /* Escape '%' from the error string */
188             for( char *percent = strchr( errbuf, '%' );
189                  percent != NULL;
190                  percent = strchr( percent + 2, '%' ) )
191             {
192                 memmove( percent + 1, percent, strlen( percent ) + 1 );
193             }
194
195             errlen = strlen( errbuf );
196             memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
197             memcpy( ptr, errbuf, errlen );
198             break; /* Only once, so we don't overflow */
199         }
200
201         /* Looks for conversion specifier... */
202         do
203             ptr++;
204         while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
205         if( *ptr )
206             ptr++; /* ...and skip it */
207     }
208 #endif
209
210     /* Fill message information fields */
211     msg_item_t msg;
212
213     msg.i_object_id = (uintptr_t)obj;
214     msg.psz_object_type = (obj != NULL) ? obj->psz_object_type : "generic";
215     msg.psz_module = module;
216     msg.psz_header = NULL;
217
218     for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
219         if (o->psz_header != NULL)
220         {
221             msg.psz_header = o->psz_header;
222             break;
223         }
224
225     /* Pass message to subscribers */
226     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
227
228     va_list ap;
229
230     va_copy (ap, args);
231     if (priv->b_color)
232         PrintColorMsg (&priv->i_verbose, type, &msg, format, ap);
233     else
234         PrintMsg (&priv->i_verbose, type, &msg, format, ap);
235     va_end (ap);
236
237 #ifdef WIN32
238     va_list dol;
239     va_copy (dol, args);
240     Win32DebugOutputMsg (&priv->i_verbose, type, &msg, format, dol);
241     va_end (dol);
242 #endif
243
244     vlc_rwlock_rdlock (&msg_lock);
245     for (msg_subscription_t *sub = msg_head; sub != NULL; sub = sub->next)
246     {
247         va_copy (ap, args);
248         sub->func (sub->opaque, type, &msg, format, ap);
249         va_end (ap);
250     }
251     vlc_rwlock_unlock (&msg_lock);
252
253     uselocale (locale);
254     freelocale (c);
255 }
256
257 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
258 #define COL(x,y)  "\033[" #x ";" #y "m"
259 #define RED     COL(31,1)
260 #define GREEN   COL(32,1)
261 #define YELLOW  COL(0,33)
262 #define WHITE   COL(0,1)
263 #define GRAY    "\033[0m"
264 static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY };
265
266 static void PrintColorMsg (void *d, int type, const msg_item_t *p_item,
267                            const char *format, va_list ap)
268 {
269     const signed char *pverbose = d;
270     FILE *stream = stderr;
271
272     if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
273         return;
274
275     int canc = vlc_savecancel ();
276
277     flockfile (stream);
278     fprintf (stream, "["GREEN"%p"GRAY"] ", (void *)p_item->i_object_id);
279     if (p_item->psz_header != NULL)
280         utf8_fprintf (stream, "[%s] ", p_item->psz_header);
281     utf8_fprintf (stream, "%s %s%s: %s", p_item->psz_module,
282                   p_item->psz_object_type, msg_type[type], msg_color[type]);
283     utf8_vfprintf (stream, format, ap);
284     fputs (GRAY"\n", stream);
285 #if defined (WIN32) || defined (__OS2__)
286     fflush (stream);
287 #endif
288     funlockfile (stream);
289     vlc_restorecancel (canc);
290 }
291
292 static void PrintMsg (void *d, int type, const msg_item_t *p_item,
293                       const char *format, va_list ap)
294 {
295     const signed char *pverbose = d;
296     FILE *stream = stderr;
297
298     if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
299         return;
300
301     int canc = vlc_savecancel ();
302
303     flockfile (stream);
304     fprintf (stream, "[%p] ", (void *)p_item->i_object_id);
305     if (p_item->psz_header != NULL)
306         utf8_fprintf (stream, "[%s] ", p_item->psz_header);
307     utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module,
308                   p_item->psz_object_type, msg_type[type]);
309     utf8_vfprintf (stream, format, ap);
310     putc_unlocked ('\n', stream);
311 #if defined (WIN32) || defined (__OS2__)
312     fflush (stream);
313 #endif
314     funlockfile (stream);
315     vlc_restorecancel (canc);
316 }
317
318 #ifdef WIN32
319 static void Win32DebugOutputMsg (void* d, int type, const msg_item_t *p_item,
320                                  const char *format, va_list dol)
321 {
322     const signed char *pverbose = d;
323     if (pverbose && (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)))
324         return;
325
326     va_list dol2;
327     va_copy (dol2, dol);
328     int msg_len = vsnprintf(NULL, 0, format, dol2);
329     va_end (dol2);
330
331     if(msg_len <= 0)
332         return;
333
334     char *msg = malloc(msg_len + 1 + 1);
335     if (!msg)
336         return;
337
338     msg_len = vsnprintf(msg, msg_len+1, format, dol);
339     if (msg_len > 0){
340         if(msg[msg_len-1] != '\n'){
341             msg[msg_len] = '\n';
342             msg[msg_len + 1] = '\0';
343         }
344         OutputDebugString(msg);
345     }
346     free(msg);
347 }
348 #endif