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
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_common.h>
37 #include <stdarg.h> /* va_list for BSD */
40 #elif defined(HAVE_LOCALE_H)
43 #include <errno.h> /* errno */
46 # include <vlc_network.h> /* 'net_strerror' and 'WSAGetLastError' */
51 #include <vlc_charset.h>
52 #include "../libvlc.h"
55 * Store all data required by messages interfaces.
57 vlc_rwlock_t msg_lock = VLC_STATIC_RWLOCK;
58 msg_subscription_t *msg_head;
60 struct msg_subscription_t
62 msg_subscription_t *prev, *next;
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.
72 * @param cb callback function
73 * @param opaque data for the callback function
74 * @return a subscription pointer, or NULL in case of failure
76 msg_subscription_t *vlc_Subscribe (msg_callback_t cb, void *opaque)
78 msg_subscription_t *sub = malloc (sizeof (*sub));
86 vlc_rwlock_wrlock (&msg_lock);
89 vlc_rwlock_unlock (&msg_lock);
95 * Unsubscribe from the message queue.
96 * This function waits for the message callback to return if needed.
98 void vlc_Unsubscribe (msg_subscription_t *sub)
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;
107 assert (msg_head == sub);
108 msg_head = sub->next;
110 vlc_rwlock_unlock (&msg_lock);
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
122 void vlc_Log (vlc_object_t *obj, int type, const char *module,
123 const char *format, ... )
127 va_start (args, format);
128 vlc_vaLog (obj, type, module, format, args);
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);
136 static void Win32DebugOutputMsg (void *, int , const msg_item_t *,
137 const char *, va_list);
141 * Emit a log message. This function is the variable argument list equivalent
144 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
145 const char *format, va_list args)
147 if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
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);
155 /* Expand %m to strerror(errno) - only once */
156 char buf[strlen(format) + 2001], *ptr;
157 strcpy (buf, format);
159 format = (const char*) buf;
163 ptr = strchr( ptr, '%' );
173 strerror_r( errno, errbuf, 1001 );
175 int sockerr = WSAGetLastError( );
178 strncpy( errbuf, net_strerror( sockerr ), 1001 );
179 WSASetLastError( sockerr );
182 || (strcmp ("Unknown network stack error", errbuf) == 0))
183 strncpy( errbuf, strerror( errno ), 1001 );
187 /* Escape '%' from the error string */
188 for( char *percent = strchr( errbuf, '%' );
190 percent = strchr( percent + 2, '%' ) )
192 memmove( percent + 1, percent, strlen( percent ) + 1 );
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 */
201 /* Looks for conversion specifier... */
204 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
206 ptr++; /* ...and skip it */
210 /* Fill message information fields */
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;
218 for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
219 if (o->psz_header != NULL)
221 msg.psz_header = o->psz_header;
225 /* Pass message to subscribers */
226 libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
232 PrintColorMsg (&priv->i_verbose, type, &msg, format, ap);
234 PrintMsg (&priv->i_verbose, type, &msg, format, ap);
240 Win32DebugOutputMsg (&priv->i_verbose, type, &msg, format, dol);
244 vlc_rwlock_rdlock (&msg_lock);
245 for (msg_subscription_t *sub = msg_head; sub != NULL; sub = sub->next)
248 sub->func (sub->opaque, type, &msg, format, ap);
251 vlc_rwlock_unlock (&msg_lock);
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 };
266 static void PrintColorMsg (void *d, int type, const msg_item_t *p_item,
267 const char *format, va_list ap)
269 const signed char *pverbose = d;
270 FILE *stream = stderr;
272 if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
275 int canc = vlc_savecancel ();
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__)
288 funlockfile (stream);
289 vlc_restorecancel (canc);
292 static void PrintMsg (void *d, int type, const msg_item_t *p_item,
293 const char *format, va_list ap)
295 const signed char *pverbose = d;
296 FILE *stream = stderr;
298 if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
301 int canc = vlc_savecancel ();
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__)
314 funlockfile (stream);
315 vlc_restorecancel (canc);
319 static void Win32DebugOutputMsg (void* d, int type, const msg_item_t *p_item,
320 const char *format, va_list dol)
322 const signed char *pverbose = d;
323 if (pverbose && (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)))
328 int msg_len = vsnprintf(NULL, 0, format, dol2);
334 char *msg = malloc(msg_len + 1 + 1);
338 msg_len = vsnprintf(msg, msg_len+1, format, dol);
340 if(msg[msg_len-1] != '\n'){
342 msg[msg_len + 1] = '\0';
344 OutputDebugString(msg);