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 <stdarg.h> /* va_list for BSD */
38 #elif defined(HAVE_LOCALE_H)
41 #include <errno.h> /* errno */
44 #include <vlc_common.h>
45 #include <vlc_interface.h>
47 # include <vlc_network.h> /* 'net_strerror' and 'WSAGetLastError' */
49 #include <vlc_charset.h>
50 #include "../libvlc.h"
53 * Store all data required by messages interfaces.
55 vlc_rwlock_t msg_lock = VLC_STATIC_RWLOCK;
56 msg_subscription_t *msg_head;
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.
63 * @param cb callback function
64 * @param opaque data for the callback function
66 void vlc_Subscribe (msg_subscription_t *sub, msg_callback_t cb, void *opaque)
72 vlc_rwlock_wrlock (&msg_lock);
75 vlc_rwlock_unlock (&msg_lock);
79 * Unsubscribe from the message queue.
80 * This function waits for the message callback to return if needed.
82 void vlc_Unsubscribe (msg_subscription_t *sub)
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;
91 assert (msg_head == sub);
94 vlc_rwlock_unlock (&msg_lock);
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
105 void vlc_Log (vlc_object_t *obj, int type, const char *module,
106 const char *format, ... )
110 va_start (args, format);
111 vlc_vaLog (obj, type, module, format, args);
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);
119 static void Win32DebugOutputMsg (void *, int , const msg_item_t *,
120 const char *, va_list);
124 * Emit a log message. This function is the variable argument list equivalent
127 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
128 const char *format, va_list args)
130 if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
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);
138 /* Expand %m to strerror(errno) - only once */
139 char buf[strlen(format) + 2001], *ptr;
140 strcpy (buf, format);
142 format = (const char*) buf;
146 ptr = strchr( ptr, '%' );
156 strerror_r( errno, errbuf, 1001 );
158 int sockerr = WSAGetLastError( );
161 strncpy( errbuf, net_strerror( sockerr ), 1001 );
162 WSASetLastError( sockerr );
165 || (strcmp ("Unknown network stack error", errbuf) == 0))
166 strncpy( errbuf, strerror( errno ), 1001 );
170 /* Escape '%' from the error string */
171 for( char *percent = strchr( errbuf, '%' );
173 percent = strchr( percent + 2, '%' ) )
175 memmove( percent + 1, percent, strlen( percent ) + 1 );
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 */
184 /* Looks for conversion specifier... */
187 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
189 ptr++; /* ...and skip it */
193 /* Fill message information fields */
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;
201 for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
202 if (o->psz_header != NULL)
204 msg.psz_header = o->psz_header;
208 /* Pass message to subscribers */
209 libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
215 PrintColorMsg (&priv->i_verbose, type, &msg, format, ap);
217 PrintMsg (&priv->i_verbose, type, &msg, format, ap);
222 Win32DebugOutputMsg (&priv->i_verbose, type, &msg, format, ap);
226 vlc_rwlock_rdlock (&msg_lock);
227 for (msg_subscription_t *sub = msg_head; sub != NULL; sub = sub->next)
230 sub->func (sub->opaque, type, &msg, format, ap);
233 vlc_rwlock_unlock (&msg_lock);
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 };
248 static void PrintColorMsg (void *d, int type, const msg_item_t *p_item,
249 const char *format, va_list ap)
251 const signed char *pverbose = d;
252 FILE *stream = stderr;
254 if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
257 int canc = vlc_savecancel ();
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__)
270 funlockfile (stream);
271 vlc_restorecancel (canc);
274 static void PrintMsg (void *d, int type, const msg_item_t *p_item,
275 const char *format, va_list ap)
277 const signed char *pverbose = d;
278 FILE *stream = stderr;
280 if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
283 int canc = vlc_savecancel ();
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__)
296 funlockfile (stream);
297 vlc_restorecancel (canc);
301 static void Win32DebugOutputMsg (void* d, int type, const msg_item_t *p_item,
302 const char *format, va_list dol)
306 const signed char *pverbose = d;
307 if (pverbose && (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)))
312 int msg_len = vsnprintf(NULL, 0, format, dol2);
318 char *msg = malloc(msg_len + 1 + 1);
322 msg_len = vsnprintf(msg, msg_len+1, format, dol);
324 if(msg[msg_len-1] != '\n'){
326 msg[msg_len + 1] = '\0';
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);