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' */
50 # include <unistd.h> /* close(), write() */
55 #include <vlc_charset.h>
56 #include "../libvlc.h"
64 static void cleanup_msg_context (void *data)
66 msg_context_t *ctx = data;
67 free (ctx->psz_message);
71 static vlc_threadvar_t msg_context;
72 static uintptr_t banks = 0;
74 /*****************************************************************************
76 *****************************************************************************/
77 #if defined(HAVE_VA_COPY)
78 # define vlc_va_copy(dest,src) va_copy(dest,src)
79 #elif defined(HAVE___VA_COPY)
80 # define vlc_va_copy(dest,src) __va_copy(dest,src)
82 # define vlc_va_copy(dest,src) (dest)=(src)
85 static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
87 return (libvlc_priv (inst))->msg_bank;
90 /*****************************************************************************
92 *****************************************************************************/
93 static void PrintMsg ( vlc_object_t *, msg_item_t * );
95 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
98 * Store all data required by messages interfaces.
102 /** Message queue lock */
107 msg_subscription_t **pp_sub;
109 locale_t locale; /**< C locale for error messages */
110 vlc_dictionary_t enabled_objects; ///< Enabled objects
111 bool all_objects_enabled; ///< Should we print all objects?
115 * Initialize messages queues
116 * This function initializes all message queues
118 msg_bank_t *msg_Create (void)
120 msg_bank_t *bank = malloc (sizeof (*bank));
122 vlc_rwlock_init (&bank->lock);
123 vlc_dictionary_init (&bank->enabled_objects, 0);
124 bank->all_objects_enabled = true;
129 /* C locale to get error messages in English in the logs */
130 bank->locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t)0);
132 vlc_mutex_lock( &msg_stack_lock );
134 vlc_threadvar_create( &msg_context, cleanup_msg_context );
135 vlc_mutex_unlock( &msg_stack_lock );
140 * Object Printing selection
142 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
143 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
146 #undef msg_EnableObjectPrinting
147 void msg_EnableObjectPrinting (vlc_object_t *obj, const char * psz_object)
149 msg_bank_t *bank = libvlc_bank (obj->p_libvlc);
151 vlc_rwlock_wrlock (&bank->lock);
152 if( !strcmp(psz_object, "all") )
153 bank->all_objects_enabled = true;
155 vlc_dictionary_insert (&bank->enabled_objects, psz_object,
156 (void *)kObjectPrintingEnabled);
157 vlc_rwlock_unlock (&bank->lock);
160 #undef msg_DisableObjectPrinting
161 void msg_DisableObjectPrinting (vlc_object_t *obj, const char * psz_object)
163 msg_bank_t *bank = libvlc_bank (obj->p_libvlc);
165 vlc_rwlock_wrlock (&bank->lock);
166 if( !strcmp(psz_object, "all") )
167 bank->all_objects_enabled = false;
169 vlc_dictionary_insert (&bank->enabled_objects, psz_object,
170 (void *)kObjectPrintingDisabled);
171 vlc_rwlock_unlock (&bank->lock);
175 * Destroy the message queues
177 * This functions prints all messages remaining in the queues,
178 * then frees all the allocated resources
179 * No other messages interface functions should be called after this one.
181 void msg_Destroy (msg_bank_t *bank)
183 if (unlikely(bank->i_sub != 0))
184 fputs ("stale interface subscribers (LibVLC might crash)\n", stderr);
186 vlc_mutex_lock( &msg_stack_lock );
189 vlc_threadvar_delete( &msg_context );
190 vlc_mutex_unlock( &msg_stack_lock );
192 if (bank->locale != (locale_t)0)
193 freelocale (bank->locale);
195 vlc_dictionary_clear (&bank->enabled_objects, NULL, NULL);
197 vlc_rwlock_destroy (&bank->lock);
201 struct msg_subscription_t
203 libvlc_int_t *instance;
205 msg_cb_data_t *opaque;
210 * Subscribe to the message queue.
211 * Whenever a message is emitted, a callback will be called.
212 * Callback invocation are serialized within a subscription.
214 * @param instance LibVLC instance to get messages from
215 * @param cb callback function
216 * @param opaque data for the callback function
217 * @return a subscription pointer, or NULL in case of failure
219 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
220 msg_cb_data_t *opaque)
222 msg_subscription_t *sub = malloc (sizeof (*sub));
226 sub->instance = instance;
228 sub->opaque = opaque;
229 sub->verbosity = 2; /* by default, give all the messages */
231 msg_bank_t *bank = libvlc_bank (instance);
232 vlc_rwlock_wrlock (&bank->lock);
233 TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
234 vlc_rwlock_unlock (&bank->lock);
240 * Unsubscribe from the message queue.
241 * This function waits for the message callback to return if needed.
243 void msg_Unsubscribe (msg_subscription_t *sub)
245 msg_bank_t *bank = libvlc_bank (sub->instance);
247 vlc_rwlock_wrlock (&bank->lock);
248 TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
249 vlc_rwlock_unlock (&bank->lock);
253 void msg_SubscriptionSetVerbosity( msg_subscription_t *sub, const int i_verbosity )
255 if( i_verbosity < 0 || i_verbosity > 2 ) return;
257 msg_bank_t *bank = libvlc_bank ( sub->instance );
259 vlc_rwlock_wrlock (&bank->lock);
261 sub->verbosity = i_verbosity;
263 vlc_rwlock_unlock (&bank->lock);
265 /*****************************************************************************
266 * msg_*: print a message
267 *****************************************************************************
268 * These functions queue a message for later printing.
269 *****************************************************************************/
270 void msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
271 const char *psz_format, ... )
275 va_start( args, psz_format );
276 msg_GenericVa (p_this, i_type, psz_module, psz_format, args);
281 * Destroys a message.
283 static void msg_Free (gc_object_t *gc)
285 msg_item_t *msg = vlc_priv (gc, msg_item_t);
287 free (msg->psz_module);
289 free (msg->psz_header);
295 * Add a message to a queue
297 * This function provides basic functionnalities to other msg_* functions.
298 * It adds a message to a queue (after having printed all stored messages if it
299 * is full). If the message can't be converted to string in memory, it issues
302 void msg_GenericVa (vlc_object_t *p_this, int i_type,
303 const char *psz_module,
304 const char *psz_format, va_list _args)
306 size_t i_header_size; /* Size of the additionnal header */
308 char * psz_str = NULL; /* formatted message string */
309 char * psz_header = NULL;
314 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
315 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
318 msg_bank_t *bank = libvlc_bank (p_this->p_libvlc);
319 locale_t locale = uselocale (bank->locale);
322 /* Expand %m to strerror(errno) - only once */
323 char buf[strlen( psz_format ) + 2001], *ptr;
324 strcpy( buf, psz_format );
326 psz_format = (const char*) buf;
330 ptr = strchr( ptr, '%' );
340 strerror_r( errno, errbuf, 1001 );
342 int sockerr = WSAGetLastError( );
345 strncpy( errbuf, net_strerror( sockerr ), 1001 );
346 WSASetLastError( sockerr );
349 || (strcmp ("Unknown network stack error", errbuf) == 0))
350 strncpy( errbuf, strerror( errno ), 1001 );
354 /* Escape '%' from the error string */
355 for( char *percent = strchr( errbuf, '%' );
357 percent = strchr( percent + 2, '%' ) )
359 memmove( percent + 1, percent, strlen( percent ) + 1 );
362 errlen = strlen( errbuf );
363 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
364 memcpy( ptr, errbuf, errlen );
365 break; /* Only once, so we don't overflow */
368 /* Looks for conversion specifier... */
371 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
373 ptr++; /* ...and skip it */
377 /* Convert message to string */
378 vlc_va_copy( args, _args );
379 if( vasprintf( &psz_str, psz_format, args ) == -1 )
383 if( psz_str == NULL )
385 int canc = vlc_savecancel (); /* Do not print half of a message... */
387 fprintf( stderr, "main warning: can't store message (%m): " );
391 /* we're not using GLIBC, so we are sure that the error description
392 * will be stored in the buffer we provide to strerror_r() */
393 strerror_r( errno, psz_err, 1001 );
395 strncpy( psz_err, strerror( errno ), 1001 );
397 psz_err[1000] = '\0';
398 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
400 vlc_va_copy( args, _args );
401 /* We should use utf8_vfprintf - but it calls malloc()... */
402 vfprintf( stderr, psz_format, args );
404 fputs( "\n", stderr );
405 vlc_restorecancel (canc);
411 msg_item_t * p_item = malloc (sizeof (*p_item));
415 vlc_gc_init (p_item, msg_Free);
416 p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
422 while( p_obj != NULL )
424 char *psz_old = NULL;
425 if( p_obj->psz_header )
427 i_header_size += strlen( p_obj->psz_header ) + 4;
430 psz_old = strdup( psz_header );
431 psz_header = xrealloc( psz_header, i_header_size );
432 snprintf( psz_header, i_header_size , "[%s] %s",
433 p_obj->psz_header, psz_old );
437 psz_header = xmalloc( i_header_size );
438 snprintf( psz_header, i_header_size, "[%s]",
443 p_obj = p_obj->p_parent;
446 /* Fill message information fields */
447 p_item->i_type = i_type;
448 p_item->i_object_id = (uintptr_t)p_this;
449 p_item->psz_object_type = p_this->psz_object_type;
450 p_item->psz_module = strdup( psz_module );
451 p_item->psz_msg = psz_str;
452 p_item->psz_header = psz_header;
454 PrintMsg( p_this, p_item );
456 vlc_rwlock_rdlock (&bank->lock);
457 for (int i = 0; i < bank->i_sub; i++)
459 msg_subscription_t *sub = bank->pp_sub[i];
460 libvlc_priv_t *priv = libvlc_priv( sub->instance );
461 msg_bank_t *bank = priv->msg_bank;
462 void *val = vlc_dictionary_value_for_key( &bank->enabled_objects,
463 p_item->psz_module );
464 if( val == kObjectPrintingDisabled ) continue;
465 if( val != kObjectPrintingEnabled ) /*if not allowed */
467 val = vlc_dictionary_value_for_key( &bank->enabled_objects,
468 p_item->psz_object_type );
469 if( val == kObjectPrintingDisabled ) continue;
470 if( val == kObjectPrintingEnabled ); /* Allowed */
471 else if( !bank->all_objects_enabled ) continue;
473 switch( p_item->i_type )
477 if( sub->verbosity < 0 ) continue;
480 if( sub->verbosity < 1 ) continue;
483 if( sub->verbosity < 2 ) continue;
487 sub->func (sub->opaque, p_item, 0);
489 vlc_rwlock_unlock (&bank->lock);
490 msg_Release (p_item);
493 /*****************************************************************************
494 * PrintMsg: output a standard message item to stderr
495 *****************************************************************************
496 * Print a message to stderr, with colour formatting if needed.
497 *****************************************************************************/
498 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
500 # define COL(x,y) "\033[" #x ";" #y "m"
501 # define RED COL(31,1)
502 # define GREEN COL(32,1)
503 # define YELLOW COL(0,33)
504 # define WHITE COL(0,1)
505 # define GRAY "\033[0m"
507 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
508 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
509 const char *psz_object;
510 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
511 msg_bank_t *bank = priv->msg_bank;
512 int i_type = p_item->i_type;
517 if( priv->i_verbose < 0 ) return;
520 if( priv->i_verbose < 0 ) return;
523 if( priv->i_verbose < 1 ) return;
526 if( priv->i_verbose < 2 ) return;
530 psz_object = p_item->psz_object_type;
531 void * val = vlc_dictionary_value_for_key (&bank->enabled_objects,
533 if( val == kObjectPrintingDisabled )
535 if( val == kObjectPrintingEnabled )
539 val = vlc_dictionary_value_for_key (&bank->enabled_objects,
541 if( val == kObjectPrintingDisabled )
543 if( val == kObjectPrintingEnabled )
545 else if( !bank->all_objects_enabled )
549 int canc = vlc_savecancel ();
550 /* Send the message to stderr */
551 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
552 priv->b_color ? GREEN : "",
553 (void *)p_item->i_object_id,
554 priv->b_color ? GRAY : "",
555 p_item->psz_header ? p_item->psz_header : "",
556 p_item->psz_header ? " " : "",
557 p_item->psz_module, psz_object,
559 priv->b_color ? ppsz_color[i_type] : "",
561 priv->b_color ? GRAY : "" );
566 vlc_restorecancel (canc);
569 static msg_context_t* GetContext(void)
571 msg_context_t *p_ctx = vlc_threadvar_get( msg_context );
574 p_ctx = malloc( sizeof( msg_context_t ) );
577 p_ctx->psz_message = NULL;
578 vlc_threadvar_set( msg_context, p_ctx );
583 void msg_StackSet( int i_code, const char *psz_message, ... )
586 msg_context_t *p_ctx = GetContext();
590 free( p_ctx->psz_message );
592 va_start( ap, psz_message );
593 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
594 p_ctx->psz_message = NULL;
597 p_ctx->i_code = i_code;
600 void msg_StackAdd( const char *psz_message, ... )
604 msg_context_t *p_ctx = GetContext();
609 va_start( ap, psz_message );
610 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
614 if( !p_ctx->psz_message )
615 p_ctx->psz_message = psz_tmp;
619 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
622 free( p_ctx->psz_message );
623 p_ctx->psz_message = psz_new;
628 const char* msg_StackMsg( void )
630 msg_context_t *p_ctx = GetContext();
632 return p_ctx->psz_message;