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 QueueMsg ( vlc_object_t *, int, const char *,
91 const char *, va_list );
92 static void PrintMsg ( vlc_object_t *, msg_item_t * );
94 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
97 * Initialize messages queues
98 * This function initializes all message queues
100 void msg_Create (libvlc_int_t *p_libvlc)
102 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
103 msg_bank_t *bank = libvlc_bank (p_libvlc);
105 vlc_rwlock_init (&bank->lock);
106 vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
107 priv->msg_all_objects_enabled = true;
112 /* C locale to get error messages in English in the logs */
113 bank->locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t)0);
117 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
118 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
119 CREATE_ALWAYS, 0, NULL );
120 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
123 vlc_mutex_lock( &msg_stack_lock );
125 vlc_threadvar_create( &msg_context, cleanup_msg_context );
126 vlc_mutex_unlock( &msg_stack_lock );
130 * Object Printing selection
132 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
133 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
135 void __msg_EnableObjectPrinting (vlc_object_t *p_this, const char * psz_object)
137 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
138 vlc_rwlock_wrlock( &QUEUE.lock );
139 if( !strcmp(psz_object, "all") )
140 priv->msg_all_objects_enabled = true;
142 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
143 vlc_rwlock_unlock( &QUEUE.lock );
146 void __msg_DisableObjectPrinting (vlc_object_t *p_this, const char * psz_object)
148 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
149 vlc_rwlock_wrlock( &QUEUE.lock );
150 if( !strcmp(psz_object, "all") )
151 priv->msg_all_objects_enabled = false;
153 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
154 vlc_rwlock_unlock( &QUEUE.lock );
158 * Destroy the message queues
160 * This functions prints all messages remaining in the queues,
161 * then frees all the allocated resources
162 * No other messages interface functions should be called after this one.
164 void msg_Destroy (libvlc_int_t *p_libvlc)
166 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
167 msg_bank_t *bank = libvlc_bank (p_libvlc);
170 msg_Err( p_libvlc, "stale interface subscribers (VLC might crash)" );
172 vlc_mutex_lock( &msg_stack_lock );
175 vlc_threadvar_delete( &msg_context );
176 vlc_mutex_unlock( &msg_stack_lock );
179 CloseHandle( QUEUE.logfile );
181 if (bank->locale != (locale_t)0)
182 freelocale (bank->locale);
184 vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
186 vlc_rwlock_destroy (&bank->lock);
189 struct msg_subscription_t
191 libvlc_int_t *instance;
193 msg_cb_data_t *opaque;
197 * Subscribe to the message queue.
198 * Whenever a message is emitted, a callback will be called.
199 * Callback invocation are serialized within a subscription.
201 * @param instance LibVLC instance to get messages from
202 * @param cb callback function
203 * @param opaque data for the callback function
204 * @return a subscription pointer, or NULL in case of failure
206 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
207 msg_cb_data_t *opaque)
209 msg_subscription_t *sub = malloc (sizeof (*sub));
213 sub->instance = instance;
215 sub->opaque = opaque;
217 msg_bank_t *bank = libvlc_bank (instance);
218 vlc_rwlock_wrlock (&bank->lock);
219 TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
220 vlc_rwlock_unlock (&bank->lock);
226 * Unsubscribe from the message queue.
227 * This function waits for the message callback to return if needed.
229 void msg_Unsubscribe (msg_subscription_t *sub)
231 msg_bank_t *bank = libvlc_bank (sub->instance);
233 vlc_rwlock_wrlock (&bank->lock);
234 TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
235 vlc_rwlock_unlock (&bank->lock);
239 /*****************************************************************************
240 * __msg_*: print a message
241 *****************************************************************************
242 * These functions queue a message for later printing.
243 *****************************************************************************/
244 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
245 const char *psz_format, ... )
249 va_start( args, psz_format );
250 QueueMsg( p_this, i_type, psz_module, psz_format, args );
254 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
255 const char *psz_format, va_list args )
257 QueueMsg( p_this, i_type, psz_module, psz_format, args );
261 * Destroys a message.
263 static void msg_Free (gc_object_t *gc)
265 msg_item_t *msg = vlc_priv (gc, msg_item_t);
267 free (msg->psz_module);
269 free (msg->psz_header);
274 * Add a message to a queue
276 * This function provides basic functionnalities to other msg_* functions.
277 * It adds a message to a queue (after having printed all stored messages if it
278 * is full). If the message can't be converted to string in memory, it issues
281 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
282 const char *psz_format, va_list _args )
285 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
286 int i_header_size; /* Size of the additionnal header */
288 char * psz_str = NULL; /* formatted message string */
289 char * psz_header = NULL;
292 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
293 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
296 msg_bank_t *bank = &QUEUE;
297 locale_t locale = uselocale (bank->locale);
300 /* Expand %m to strerror(errno) - only once */
301 char buf[strlen( psz_format ) + 2001], *ptr;
302 strcpy( buf, psz_format );
304 psz_format = (const char*) buf;
308 ptr = strchr( ptr, '%' );
318 strerror_r( errno, errbuf, 1001 );
320 int sockerr = WSAGetLastError( );
323 strncpy( errbuf, net_strerror( sockerr ), 1001 );
324 WSASetLastError( sockerr );
327 || (strcmp ("Unknown network stack error", errbuf) == 0))
328 strncpy( errbuf, strerror( errno ), 1001 );
332 /* Escape '%' from the error string */
333 for( char *percent = strchr( errbuf, '%' );
335 percent = strchr( percent + 2, '%' ) )
337 memmove( percent + 1, percent, strlen( percent ) + 1 );
340 errlen = strlen( errbuf );
341 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
342 memcpy( ptr, errbuf, errlen );
343 break; /* Only once, so we don't overflow */
346 /* Looks for conversion specifier... */
349 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
351 ptr++; /* ...and skip it */
355 /* Convert message to string */
356 vlc_va_copy( args, _args );
357 if( vasprintf( &psz_str, psz_format, args ) == -1 )
361 if( psz_str == NULL )
363 int canc = vlc_savecancel (); /* Do not print half of a message... */
365 fprintf( stderr, "main warning: can't store message (%m): " );
369 /* we're not using GLIBC, so we are sure that the error description
370 * will be stored in the buffer we provide to strerror_r() */
371 strerror_r( errno, psz_err, 1001 );
373 strncpy( psz_err, strerror( errno ), 1001 );
375 psz_err[1000] = '\0';
376 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
378 vlc_va_copy( args, _args );
379 /* We should use utf8_vfprintf - but it calls malloc()... */
380 vfprintf( stderr, psz_format, args );
382 fputs( "\n", stderr );
383 vlc_restorecancel (canc);
389 msg_item_t * p_item = malloc (sizeof (*p_item));
393 vlc_gc_init (p_item, msg_Free);
394 p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
400 while( p_obj != NULL )
402 char *psz_old = NULL;
403 if( p_obj->psz_header )
405 i_header_size += strlen( p_obj->psz_header ) + 4;
408 psz_old = strdup( psz_header );
409 psz_header = xrealloc( psz_header, i_header_size );
410 snprintf( psz_header, i_header_size , "[%s] %s",
411 p_obj->psz_header, psz_old );
415 psz_header = xmalloc( i_header_size );
416 snprintf( psz_header, i_header_size, "[%s]",
421 p_obj = p_obj->p_parent;
424 /* Fill message information fields */
425 p_item->i_type = i_type;
426 p_item->i_object_id = (uintptr_t)p_this;
427 p_item->psz_object_type = p_this->psz_object_type;
428 p_item->psz_module = strdup( psz_module );
429 p_item->psz_msg = psz_str;
430 p_item->psz_header = psz_header;
432 PrintMsg( p_this, p_item );
434 vlc_rwlock_rdlock (&bank->lock);
435 for (int i = 0; i < bank->i_sub; i++)
437 msg_subscription_t *sub = bank->pp_sub[i];
438 sub->func (sub->opaque, p_item, 0);
440 vlc_rwlock_unlock (&bank->lock);
441 msg_Release (p_item);
444 /*****************************************************************************
445 * PrintMsg: output a standard message item to stderr
446 *****************************************************************************
447 * Print a message to stderr, with colour formatting if needed.
448 *****************************************************************************/
449 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
451 # define COL(x) "\033[" #x ";1m"
453 # define GREEN COL(32)
454 # define YELLOW COL(33)
455 # define WHITE COL(0)
456 # define GRAY "\033[0m"
461 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
462 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
463 const char *psz_object;
464 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
465 int i_type = p_item->i_type;
470 if( priv->i_verbose < 0 ) return;
473 if( priv->i_verbose < 0 ) return;
476 if( priv->i_verbose < 1 ) return;
479 if( priv->i_verbose < 2 ) return;
483 psz_object = p_item->psz_object_type;
484 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
485 p_item->psz_module );
486 if( val == kObjectPrintingDisabled )
488 if( val == kObjectPrintingEnabled )
492 val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
494 if( val == kObjectPrintingDisabled )
496 if( val == kObjectPrintingEnabled )
498 else if( !priv->msg_all_objects_enabled )
503 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
504 str, strlen(str), &i_dummy, NULL );
505 CE_WRITE( p_item->psz_module );
507 CE_WRITE( psz_object );
508 CE_WRITE( ppsz_type[i_type] );
510 CE_WRITE( p_item->psz_msg );
512 FlushFileBuffers( QUEUE.logfile );
515 int canc = vlc_savecancel ();
516 /* Send the message to stderr */
517 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
518 priv->b_color ? GREEN : "",
519 (void *)p_item->i_object_id,
520 priv->b_color ? GRAY : "",
521 p_item->psz_header ? p_item->psz_header : "",
522 p_item->psz_header ? " " : "",
523 p_item->psz_module, psz_object,
525 priv->b_color ? ppsz_color[i_type] : "",
527 priv->b_color ? GRAY : "" );
532 vlc_restorecancel (canc);
536 static msg_context_t* GetContext(void)
538 msg_context_t *p_ctx = vlc_threadvar_get( msg_context );
541 p_ctx = malloc( sizeof( msg_context_t ) );
544 p_ctx->psz_message = NULL;
545 vlc_threadvar_set( msg_context, p_ctx );
550 void msg_StackDestroy (void *data)
552 msg_context_t *p_ctx = data;
554 free (p_ctx->psz_message);
558 void msg_StackSet( int i_code, const char *psz_message, ... )
561 msg_context_t *p_ctx = GetContext();
565 free( p_ctx->psz_message );
567 va_start( ap, psz_message );
568 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
569 p_ctx->psz_message = NULL;
572 p_ctx->i_code = i_code;
575 void msg_StackAdd( const char *psz_message, ... )
579 msg_context_t *p_ctx = GetContext();
584 va_start( ap, psz_message );
585 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
589 if( !p_ctx->psz_message )
590 p_ctx->psz_message = psz_tmp;
594 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
597 free( p_ctx->psz_message );
598 p_ctx->psz_message = psz_new;
603 const char* msg_StackMsg( void )
605 msg_context_t *p_ctx = GetContext();
607 return p_ctx->psz_message;