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 # include <fcntl.h> /* O_CREAT, O_TRUNC, O_WRONLY, O_SYNC */
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 vlc_threadvar_t msg_context;
65 static uintptr_t banks = 0;
67 /*****************************************************************************
69 *****************************************************************************/
70 #if defined(HAVE_VA_COPY)
71 # define vlc_va_copy(dest,src) va_copy(dest,src)
72 #elif defined(HAVE___VA_COPY)
73 # define vlc_va_copy(dest,src) __va_copy(dest,src)
75 # define vlc_va_copy(dest,src) (dest)=(src)
78 #define QUEUE priv->msg_bank.queue
79 #define LOCK_BANK vlc_mutex_lock( &priv->msg_bank.lock );
80 #define UNLOCK_BANK vlc_mutex_unlock( &priv->msg_bank.lock );
82 /*****************************************************************************
84 *****************************************************************************/
85 static void QueueMsg ( vlc_object_t *, int, const char *,
86 const char *, va_list );
87 static void FlushMsg ( msg_queue_t * );
88 static void PrintMsg ( vlc_object_t *, msg_item_t * );
90 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
93 * Initialize messages queues
94 * This function initializes all message queues
96 void msg_Create (libvlc_int_t *p_libvlc)
98 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
99 vlc_mutex_init( &priv->msg_bank.lock );
100 vlc_mutex_init( &QUEUE.lock );
101 vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
102 priv->msg_all_objects_enabled = true;
104 QUEUE.b_overflow = false;
112 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
113 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
114 CREATE_ALWAYS, 0, NULL );
115 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
118 vlc_mutex_lock( &msg_stack_lock );
120 vlc_threadvar_create( &msg_context, NULL );
121 vlc_mutex_unlock( &msg_stack_lock );
125 * Flush all message queues
127 void msg_Flush (libvlc_int_t *p_libvlc)
129 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
130 vlc_mutex_lock( &QUEUE.lock );
132 vlc_mutex_unlock( &QUEUE.lock );
137 * Object Printing selection
139 static void const * kObjectPrintingEnabled = (void *) 1;
140 static void const * kObjectPrintingDisabled = (void *) -1;
142 void __msg_EnableObjectPrinting (vlc_object_t *p_this, char * psz_object)
144 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
145 vlc_mutex_lock( &QUEUE.lock );
146 if( !strcmp(psz_object, "all") )
147 priv->msg_all_objects_enabled = true;
149 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
150 vlc_mutex_unlock( &QUEUE.lock );
153 void __msg_DisableObjectPrinting (vlc_object_t *p_this, char * psz_object)
155 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
156 vlc_mutex_lock( &QUEUE.lock );
157 if( !strcmp(psz_object, "all") )
158 priv->msg_all_objects_enabled = false;
160 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
161 vlc_mutex_unlock( &QUEUE.lock );
165 * Destroy the message queues
167 * This functions prints all messages remaining in the queues,
168 * then frees all the allocated ressources
169 * No other messages interface functions should be called after this one.
171 void msg_Destroy (libvlc_int_t *p_libvlc)
173 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
176 msg_Err( p_libvlc, "stale interface subscribers" );
180 vlc_mutex_lock( &msg_stack_lock );
182 vlc_threadvar_delete( &msg_context );
183 vlc_mutex_unlock( &msg_stack_lock );
186 CloseHandle( QUEUE.logfile );
189 vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
192 vlc_mutex_destroy( &QUEUE.lock );
193 vlc_mutex_destroy( &priv->msg_bank.lock);
197 * Subscribe to a message queue.
199 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
201 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
202 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
208 vlc_mutex_lock( &QUEUE.lock );
210 TAB_APPEND( QUEUE.i_sub, QUEUE.pp_sub, p_sub );
212 p_sub->i_start = QUEUE.i_start;
213 p_sub->pi_stop = &QUEUE.i_stop;
214 p_sub->p_msg = QUEUE.msg;
215 p_sub->p_lock = &QUEUE.lock;
217 vlc_mutex_unlock( &QUEUE.lock );
224 * Unsubscribe from a message queue.
226 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
228 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
231 vlc_mutex_lock( &QUEUE.lock );
232 for( int j = 0 ; j< QUEUE.i_sub ; j++ )
234 if( QUEUE.pp_sub[j] == p_sub )
236 REMOVE_ELEM( QUEUE.pp_sub, QUEUE.i_sub, j );
240 vlc_mutex_unlock( &QUEUE.lock );
244 /*****************************************************************************
245 * __msg_*: print a message
246 *****************************************************************************
247 * These functions queue a message for later printing.
248 *****************************************************************************/
249 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
250 const char *psz_format, ... )
254 va_start( args, psz_format );
255 QueueMsg( p_this, i_type, psz_module, psz_format, args );
259 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
260 const char *psz_format, va_list args )
262 QueueMsg( p_this, i_type, psz_module, psz_format, args );
265 /* Generic functions used when variadic macros are not available. */
266 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
267 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
270 va_start( args, psz_format ); \
271 QueueMsg( p_this, FN_TYPE, "unknown", psz_format, args ); \
276 * Output an informational message.
277 * \note Do not use this for debug messages
280 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
282 * Output an error message.
284 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
286 * Output a waring message
288 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
290 * Output a debug message
292 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
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 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
303 const char *psz_format, va_list _args )
306 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
307 int i_header_size; /* Size of the additionnal header */
309 char * psz_str = NULL; /* formatted message string */
310 char * psz_header = NULL;
312 msg_item_t * p_item = NULL; /* pointer to message */
313 msg_item_t item; /* message in case of a full queue */
314 msg_queue_t *p_queue;
316 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
317 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
320 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
321 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
325 /* Expand %m to strerror(errno) - only once */
326 char buf[strlen( psz_format ) + 2001], *ptr;
327 strcpy( buf, psz_format );
329 psz_format = (const char*) buf;
333 ptr = strchr( ptr, '%' );
343 strerror_r( errno, errbuf, 1001 );
345 int sockerr = WSAGetLastError( );
348 strncpy( errbuf, net_strerror( sockerr ), 1001 );
349 WSASetLastError( sockerr );
352 || (strcmp ("Unknown network stack error", errbuf) == 0))
353 strncpy( errbuf, strerror( errno ), 1001 );
357 /* Escape '%' from the error string */
358 for( char *percent = strchr( errbuf, '%' );
360 percent = strchr( percent + 2, '%' ) )
362 memmove( percent + 1, percent, strlen( percent ) + 1 );
365 errlen = strlen( errbuf );
366 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
367 memcpy( ptr, errbuf, errlen );
368 break; /* Only once, so we don't overflow */
371 /* Looks for conversion specifier... */
374 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
376 ptr++; /* ...and skip it */
380 /* Convert message to string */
381 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
382 vlc_va_copy( args, _args );
383 if( vasprintf( &psz_str, psz_format, args ) == -1 )
387 psz_str = (char*) malloc( i_size );
390 if( psz_str == NULL )
392 int canc = vlc_savecancel (); /* Do not print half of a message... */
394 fprintf( stderr, "main warning: can't store message (%m): " );
398 /* we're not using GLIBC, so we are sure that the error description
399 * will be stored in the buffer we provide to strerror_r() */
400 strerror_r( errno, psz_err, 1001 );
402 strncpy( psz_err, strerror( errno ), 1001 );
404 psz_err[1000] = '\0';
405 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
407 vlc_va_copy( args, _args );
408 /* We should use utf8_vfprintf - but it calls malloc()... */
409 vfprintf( stderr, psz_format, args );
411 fputs( "\n", stderr );
412 vlc_restorecancel (canc);
418 while( p_obj != NULL )
420 char *psz_old = NULL;
421 if( p_obj->psz_header )
423 i_header_size += strlen( p_obj->psz_header ) + 4;
426 psz_old = strdup( psz_header );
427 psz_header = (char*)realloc( psz_header, i_header_size );
428 snprintf( psz_header, i_header_size , "[%s] %s",
429 p_obj->psz_header, psz_old );
433 psz_header = (char *)malloc( i_header_size );
434 snprintf( psz_header, i_header_size, "[%s]",
439 p_obj = p_obj->p_parent;
442 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
443 vlc_va_copy( args, _args );
444 vsnprintf( psz_str, i_size, psz_format, args );
446 psz_str[ i_size - 1 ] = 0; /* Just in case */
451 vlc_mutex_lock( &p_queue->lock );
453 /* Check there is room in the queue for our message */
454 if( p_queue->b_overflow )
458 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
460 /* Still in overflow mode, print from a dummy item */
465 /* Pheeew, at last, there is room in the queue! */
466 p_queue->b_overflow = false;
469 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
473 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
475 p_queue->b_overflow = true;
477 /* Put the overflow message in the queue */
478 p_item = p_queue->msg + p_queue->i_stop;
479 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
481 p_item->i_type = VLC_MSG_WARN;
482 p_item->i_object_id = (uintptr_t)p_this;
483 p_item->psz_object_type = p_this->psz_object_type;
484 p_item->psz_module = strdup( "message" );
485 p_item->psz_msg = strdup( "message queue overflowed" );
486 p_item->psz_header = NULL;
488 PrintMsg( p_this, p_item );
489 /* We print from a dummy item */
494 if( !p_queue->b_overflow )
496 /* Put the message in the queue */
497 p_item = p_queue->msg + p_queue->i_stop;
498 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
501 /* Fill message information fields */
502 p_item->i_type = i_type;
503 p_item->i_object_id = (uintptr_t)p_this;
504 p_item->psz_object_type = p_this->psz_object_type;
505 p_item->psz_module = strdup( psz_module );
506 p_item->psz_msg = psz_str;
507 p_item->psz_header = psz_header;
509 PrintMsg( p_this, p_item );
511 if( p_queue->b_overflow )
513 free( p_item->psz_module );
514 free( p_item->psz_msg );
515 free( p_item->psz_header );
518 vlc_mutex_unlock ( &p_queue->lock );
522 /* following functions are local */
524 /*****************************************************************************
526 *****************************************************************************
527 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
528 * this function does not check the lock.
529 *****************************************************************************/
530 static void FlushMsg ( msg_queue_t *p_queue )
532 int i_index, i_start, i_stop;
534 /* Get the maximum message index that can be freed */
535 i_stop = p_queue->i_stop;
537 /* Check until which value we can free messages */
538 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
540 i_start = p_queue->pp_sub[ i_index ]->i_start;
542 /* If this subscriber is late, we don't free messages before
543 * his i_start value, otherwise he'll miss messages */
544 if( ( i_start < i_stop
545 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
546 || ( i_stop < i_start
547 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
553 /* Free message data */
554 for( i_index = p_queue->i_start;
556 i_index = (i_index+1) % VLC_MSG_QSIZE )
558 free( p_queue->msg[i_index].psz_msg );
559 free( p_queue->msg[i_index].psz_module );
560 free( p_queue->msg[i_index].psz_header );
563 /* Update the new start value */
564 p_queue->i_start = i_index;
567 /*****************************************************************************
568 * PrintMsg: output a standard message item to stderr
569 *****************************************************************************
570 * Print a message to stderr, with colour formatting if needed.
571 *****************************************************************************/
572 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
574 # define COL(x) "\033[" #x ";1m"
576 # define GREEN COL(32)
577 # define YELLOW COL(33)
578 # define WHITE COL(0)
579 # define GRAY "\033[0m"
584 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
585 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
586 const char *psz_object;
587 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
588 int i_type = p_item->i_type;
593 if( priv->i_verbose < 0 ) return;
596 if( priv->i_verbose < 0 ) return;
599 if( priv->i_verbose < 1 ) return;
602 if( priv->i_verbose < 2 ) return;
606 psz_object = p_item->psz_object_type;
607 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
608 p_item->psz_module );
609 if( val == kObjectPrintingDisabled )
611 if( val == kObjectPrintingEnabled )
615 val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
617 if( val == kObjectPrintingDisabled )
619 if( val == kObjectPrintingEnabled )
621 else if( !priv->msg_all_objects_enabled )
625 int canc = vlc_savecancel ();
627 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
628 str, strlen(str), &i_dummy, NULL );
629 CE_WRITE( p_item->psz_module );
631 CE_WRITE( psz_object );
632 CE_WRITE( ppsz_type[i_type] );
634 CE_WRITE( p_item->psz_msg );
636 FlushFileBuffers( QUEUE.logfile );
639 /* Send the message to stderr */
640 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
641 priv->b_color ? GREEN : "",
642 (void *)p_item->i_object_id,
643 priv->b_color ? GRAY : "",
644 p_item->psz_header ? p_item->psz_header : "",
645 p_item->psz_header ? " " : "",
646 p_item->psz_module, psz_object,
648 priv->b_color ? ppsz_color[i_type] : "",
650 priv->b_color ? GRAY : "" );
655 vlc_restorecancel (canc);
659 static msg_context_t* GetContext(void)
661 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context );
664 MALLOC_NULL( p_ctx, msg_context_t );
665 p_ctx->psz_message = NULL;
666 vlc_threadvar_set( &msg_context, p_ctx );
671 void msg_StackDestroy (void *data)
673 msg_context_t *p_ctx = data;
675 free (p_ctx->psz_message);
679 void msg_StackSet( int i_code, const char *psz_message, ... )
682 msg_context_t *p_ctx = GetContext();
686 free( p_ctx->psz_message );
688 va_start( ap, psz_message );
689 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
690 p_ctx->psz_message = NULL;
693 p_ctx->i_code = i_code;
696 void msg_StackAdd( const char *psz_message, ... )
700 msg_context_t *p_ctx = GetContext();
705 va_start( ap, psz_message );
706 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
710 if( !p_ctx->psz_message )
711 p_ctx->psz_message = psz_tmp;
715 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
718 free( p_ctx->psz_message );
719 p_ctx->psz_message = psz_new;
724 const char* msg_StackMsg( void )
726 msg_context_t *p_ctx = GetContext();
728 return p_ctx->psz_message;