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 the VideoLAN team
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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 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 General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, 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);
137 * Emit a log message. This function is the variable argument list equivalent
140 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
141 const char *format, va_list args)
143 if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
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);
151 /* Expand %m to strerror(errno) - only once */
152 char buf[strlen(format) + 2001], *ptr;
153 strcpy (buf, format);
155 format = (const char*) buf;
159 ptr = strchr( ptr, '%' );
169 strerror_r( errno, errbuf, 1001 );
171 int sockerr = WSAGetLastError( );
174 strncpy( errbuf, net_strerror( sockerr ), 1001 );
175 WSASetLastError( sockerr );
178 || (strcmp ("Unknown network stack error", errbuf) == 0))
179 strncpy( errbuf, strerror( errno ), 1001 );
183 /* Escape '%' from the error string */
184 for( char *percent = strchr( errbuf, '%' );
186 percent = strchr( percent + 2, '%' ) )
188 memmove( percent + 1, percent, strlen( percent ) + 1 );
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 */
197 /* Looks for conversion specifier... */
200 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
202 ptr++; /* ...and skip it */
206 /* Fill message information fields */
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;
214 for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
215 if (o->psz_header != NULL)
217 msg.psz_header = o->psz_header;
221 /* Pass message to subscribers */
222 libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
228 PrintColorMsg (&priv->i_verbose, type, &msg, format, ap);
230 PrintMsg (&priv->i_verbose, type, &msg, format, ap);
233 vlc_rwlock_rdlock (&msg_lock);
234 for (msg_subscription_t *sub = msg_head; sub != NULL; sub = sub->next)
237 sub->func (sub->opaque, type, &msg, format, ap);
240 vlc_rwlock_unlock (&msg_lock);
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 };
255 static void PrintColorMsg (void *d, int type, const msg_item_t *p_item,
256 const char *format, va_list ap)
258 const int *pverbose = d;
259 FILE *stream = stderr;
261 if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
264 int canc = vlc_savecancel ();
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__)
277 funlockfile (stream);
278 vlc_restorecancel (canc);
281 static void PrintMsg (void *d, int type, const msg_item_t *p_item,
282 const char *format, va_list ap)
284 const int *pverbose = d;
285 FILE *stream = stderr;
287 if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
290 int canc = vlc_savecancel ();
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__)
303 funlockfile (stream);
304 vlc_restorecancel (canc);