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 */
39 #include <errno.h> /* errno */
42 # include <vlc_network.h> /* 'net_strerror' and 'WSAGetLastError' */
46 # include <unistd.h> /* close(), write() */
51 #include <vlc_charset.h>
52 #include "../libvlc.h"
60 static void cleanup_msg_context (void *data)
62 msg_context_t *ctx = data;
63 free (ctx->psz_message);
67 static vlc_threadvar_t msg_context;
68 static uintptr_t banks = 0;
70 /*****************************************************************************
72 *****************************************************************************/
73 #if defined(HAVE_VA_COPY)
74 # define vlc_va_copy(dest,src) va_copy(dest,src)
75 #elif defined(HAVE___VA_COPY)
76 # define vlc_va_copy(dest,src) __va_copy(dest,src)
78 # define vlc_va_copy(dest,src) (dest)=(src)
81 #define QUEUE priv->msg_bank
82 static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
84 return (libvlc_priv (inst))->msg_bank;
87 /*****************************************************************************
89 *****************************************************************************/
90 static void PrintMsg ( vlc_object_t *, msg_item_t * );
92 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
95 * Store all data required by messages interfaces.
99 /** Message queue lock */
104 msg_subscription_t **pp_sub;
106 /* Logfile for WinCE */
111 locale_t locale; /**< C locale for error messages */
112 vlc_dictionary_t enabled_objects; ///< Enabled objects
113 bool all_objects_enabled; ///< Should we print all objects?
117 * Initialize messages queues
118 * This function initializes all message queues
120 msg_bank_t *msg_Create (void)
122 msg_bank_t *bank = malloc (sizeof (*bank));
124 vlc_rwlock_init (&bank->lock);
125 vlc_dictionary_init (&bank->enabled_objects, 0);
126 bank->all_objects_enabled = true;
131 /* C locale to get error messages in English in the logs */
132 bank->locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t)0);
136 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
137 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
138 CREATE_ALWAYS, 0, NULL );
139 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
142 vlc_mutex_lock( &msg_stack_lock );
144 vlc_threadvar_create( &msg_context, cleanup_msg_context );
145 vlc_mutex_unlock( &msg_stack_lock );
150 * Object Printing selection
152 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
153 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
155 #undef msg_EnableObjectPrinting
156 void msg_EnableObjectPrinting (vlc_object_t *obj, const char * psz_object)
158 msg_bank_t *bank = libvlc_bank (obj->p_libvlc);
160 vlc_rwlock_wrlock (&bank->lock);
161 if( !strcmp(psz_object, "all") )
162 bank->all_objects_enabled = true;
164 vlc_dictionary_insert (&bank->enabled_objects, psz_object,
165 (void *)kObjectPrintingEnabled);
166 vlc_rwlock_unlock (&bank->lock);
169 #undef msg_DisableObjectPrinting
170 void msg_DisableObjectPrinting (vlc_object_t *obj, const char * psz_object)
172 msg_bank_t *bank = libvlc_bank (obj->p_libvlc);
174 vlc_rwlock_wrlock (&bank->lock);
175 if( !strcmp(psz_object, "all") )
176 bank->all_objects_enabled = false;
178 vlc_dictionary_insert (&bank->enabled_objects, psz_object,
179 (void *)kObjectPrintingDisabled);
180 vlc_rwlock_unlock (&bank->lock);
184 * Destroy the message queues
186 * This functions prints all messages remaining in the queues,
187 * then frees all the allocated resources
188 * No other messages interface functions should be called after this one.
190 void msg_Destroy (msg_bank_t *bank)
192 if (unlikely(bank->i_sub != 0))
193 fputs ("stale interface subscribers (LibVLC might crash)\n", stderr);
195 vlc_mutex_lock( &msg_stack_lock );
198 vlc_threadvar_delete( &msg_context );
199 vlc_mutex_unlock( &msg_stack_lock );
202 CloseHandle( QUEUE.logfile );
204 if (bank->locale != (locale_t)0)
205 freelocale (bank->locale);
207 vlc_dictionary_clear (&bank->enabled_objects, NULL, NULL);
209 vlc_rwlock_destroy (&bank->lock);
213 struct msg_subscription_t
215 libvlc_int_t *instance;
217 msg_cb_data_t *opaque;
221 * Subscribe to the message queue.
222 * Whenever a message is emitted, a callback will be called.
223 * Callback invocation are serialized within a subscription.
225 * @param instance LibVLC instance to get messages from
226 * @param cb callback function
227 * @param opaque data for the callback function
228 * @return a subscription pointer, or NULL in case of failure
230 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
231 msg_cb_data_t *opaque)
233 msg_subscription_t *sub = malloc (sizeof (*sub));
237 sub->instance = instance;
239 sub->opaque = opaque;
241 msg_bank_t *bank = libvlc_bank (instance);
242 vlc_rwlock_wrlock (&bank->lock);
243 TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
244 vlc_rwlock_unlock (&bank->lock);
250 * Unsubscribe from the message queue.
251 * This function waits for the message callback to return if needed.
253 void msg_Unsubscribe (msg_subscription_t *sub)
255 msg_bank_t *bank = libvlc_bank (sub->instance);
257 vlc_rwlock_wrlock (&bank->lock);
258 TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
259 vlc_rwlock_unlock (&bank->lock);
263 /*****************************************************************************
264 * msg_*: print a message
265 *****************************************************************************
266 * These functions queue a message for later printing.
267 *****************************************************************************/
268 void msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
269 const char *psz_format, ... )
273 va_start( args, psz_format );
274 msg_GenericVa (p_this, i_type, psz_module, psz_format, args);
279 * Destroys a message.
281 static void msg_Free (gc_object_t *gc)
283 msg_item_t *msg = vlc_priv (gc, msg_item_t);
285 free (msg->psz_module);
287 free (msg->psz_header);
293 * Add a message to a queue
295 * This function provides basic functionnalities to other msg_* functions.
296 * It adds a message to a queue (after having printed all stored messages if it
297 * is full). If the message can't be converted to string in memory, it issues
300 void msg_GenericVa (vlc_object_t *p_this, int i_type,
301 const char *psz_module,
302 const char *psz_format, va_list _args)
304 size_t i_header_size; /* Size of the additionnal header */
306 char * psz_str = NULL; /* formatted message string */
307 char * psz_header = NULL;
312 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
313 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
316 msg_bank_t *bank = libvlc_bank (p_this->p_libvlc);
317 locale_t locale = uselocale (bank->locale);
320 /* Expand %m to strerror(errno) - only once */
321 char buf[strlen( psz_format ) + 2001], *ptr;
322 strcpy( buf, psz_format );
324 psz_format = (const char*) buf;
328 ptr = strchr( ptr, '%' );
338 strerror_r( errno, errbuf, 1001 );
340 int sockerr = WSAGetLastError( );
343 strncpy( errbuf, net_strerror( sockerr ), 1001 );
344 WSASetLastError( sockerr );
347 || (strcmp ("Unknown network stack error", errbuf) == 0))
348 strncpy( errbuf, strerror( errno ), 1001 );
352 /* Escape '%' from the error string */
353 for( char *percent = strchr( errbuf, '%' );
355 percent = strchr( percent + 2, '%' ) )
357 memmove( percent + 1, percent, strlen( percent ) + 1 );
360 errlen = strlen( errbuf );
361 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
362 memcpy( ptr, errbuf, errlen );
363 break; /* Only once, so we don't overflow */
366 /* Looks for conversion specifier... */
369 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
371 ptr++; /* ...and skip it */
375 /* Convert message to string */
376 vlc_va_copy( args, _args );
377 if( vasprintf( &psz_str, psz_format, args ) == -1 )
381 if( psz_str == NULL )
383 int canc = vlc_savecancel (); /* Do not print half of a message... */
385 fprintf( stderr, "main warning: can't store message (%m): " );
389 /* we're not using GLIBC, so we are sure that the error description
390 * will be stored in the buffer we provide to strerror_r() */
391 strerror_r( errno, psz_err, 1001 );
393 strncpy( psz_err, strerror( errno ), 1001 );
395 psz_err[1000] = '\0';
396 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
398 vlc_va_copy( args, _args );
399 /* We should use utf8_vfprintf - but it calls malloc()... */
400 vfprintf( stderr, psz_format, args );
402 fputs( "\n", stderr );
403 vlc_restorecancel (canc);
409 msg_item_t * p_item = malloc (sizeof (*p_item));
413 vlc_gc_init (p_item, msg_Free);
414 p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
420 while( p_obj != NULL )
422 char *psz_old = NULL;
423 if( p_obj->psz_header )
425 i_header_size += strlen( p_obj->psz_header ) + 4;
428 psz_old = strdup( psz_header );
429 psz_header = xrealloc( psz_header, i_header_size );
430 snprintf( psz_header, i_header_size , "[%s] %s",
431 p_obj->psz_header, psz_old );
435 psz_header = xmalloc( i_header_size );
436 snprintf( psz_header, i_header_size, "[%s]",
441 p_obj = p_obj->p_parent;
444 /* Fill message information fields */
445 p_item->i_type = i_type;
446 p_item->i_object_id = (uintptr_t)p_this;
447 p_item->psz_object_type = p_this->psz_object_type;
448 p_item->psz_module = strdup( psz_module );
449 p_item->psz_msg = psz_str;
450 p_item->psz_header = psz_header;
452 PrintMsg( p_this, p_item );
454 vlc_rwlock_rdlock (&bank->lock);
455 for (int i = 0; i < bank->i_sub; i++)
457 msg_subscription_t *sub = bank->pp_sub[i];
458 sub->func (sub->opaque, p_item, 0);
460 vlc_rwlock_unlock (&bank->lock);
461 msg_Release (p_item);
464 /*****************************************************************************
465 * PrintMsg: output a standard message item to stderr
466 *****************************************************************************
467 * Print a message to stderr, with colour formatting if needed.
468 *****************************************************************************/
469 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
471 # define COL(x) "\033[" #x ";1m"
473 # define GREEN COL(32)
474 # define YELLOW COL(33)
475 # define WHITE COL(0)
476 # define GRAY "\033[0m"
481 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
482 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
483 const char *psz_object;
484 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
485 msg_bank_t *bank = priv->msg_bank;
486 int i_type = p_item->i_type;
491 if( priv->i_verbose < 0 ) return;
494 if( priv->i_verbose < 0 ) return;
497 if( priv->i_verbose < 1 ) return;
500 if( priv->i_verbose < 2 ) return;
504 psz_object = p_item->psz_object_type;
505 void * val = vlc_dictionary_value_for_key (&bank->enabled_objects,
507 if( val == kObjectPrintingDisabled )
509 if( val == kObjectPrintingEnabled )
513 val = vlc_dictionary_value_for_key (&bank->enabled_objects,
515 if( val == kObjectPrintingDisabled )
517 if( val == kObjectPrintingEnabled )
519 else if( !bank->all_objects_enabled )
524 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
525 str, strlen(str), &i_dummy, NULL );
526 CE_WRITE( p_item->psz_module );
528 CE_WRITE( psz_object );
529 CE_WRITE( ppsz_type[i_type] );
531 CE_WRITE( p_item->psz_msg );
533 FlushFileBuffers( QUEUE.logfile );
536 int canc = vlc_savecancel ();
537 /* Send the message to stderr */
538 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
539 priv->b_color ? GREEN : "",
540 (void *)p_item->i_object_id,
541 priv->b_color ? GRAY : "",
542 p_item->psz_header ? p_item->psz_header : "",
543 p_item->psz_header ? " " : "",
544 p_item->psz_module, psz_object,
546 priv->b_color ? ppsz_color[i_type] : "",
548 priv->b_color ? GRAY : "" );
553 vlc_restorecancel (canc);
557 static msg_context_t* GetContext(void)
559 msg_context_t *p_ctx = vlc_threadvar_get( msg_context );
562 p_ctx = malloc( sizeof( msg_context_t ) );
565 p_ctx->psz_message = NULL;
566 vlc_threadvar_set( msg_context, p_ctx );
571 void msg_StackDestroy (void *data)
573 msg_context_t *p_ctx = data;
575 free (p_ctx->psz_message);
579 void msg_StackSet( int i_code, const char *psz_message, ... )
582 msg_context_t *p_ctx = GetContext();
586 free( p_ctx->psz_message );
588 va_start( ap, psz_message );
589 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
590 p_ctx->psz_message = NULL;
593 p_ctx->i_code = i_code;
596 void msg_StackAdd( const char *psz_message, ... )
600 msg_context_t *p_ctx = GetContext();
605 va_start( ap, psz_message );
606 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
610 if( !p_ctx->psz_message )
611 p_ctx->psz_message = psz_tmp;
615 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
618 free( p_ctx->psz_message );
619 p_ctx->psz_message = psz_new;
624 const char* msg_StackMsg( void )
626 msg_context_t *p_ctx = GetContext();
628 return p_ctx->psz_message;