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 * );
91 * Initialize messages queues
92 * This function initializes all message queues
94 void msg_Create (libvlc_int_t *p_libvlc)
96 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
97 vlc_mutex_init( &priv->msg_bank.lock );
98 vlc_mutex_init( &QUEUE.lock );
99 vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
100 priv->msg_all_objects_enabled = true;
102 QUEUE.b_overflow = false;
110 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
111 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
112 CREATE_ALWAYS, 0, NULL );
113 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
116 vlc_mutex_t *lock = var_AcquireMutex( "msg-stack" );
118 vlc_threadvar_create( &msg_context, NULL );
119 vlc_mutex_unlock( lock );
123 * Flush all message queues
125 void msg_Flush (libvlc_int_t *p_libvlc)
127 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
128 vlc_mutex_lock( &QUEUE.lock );
130 vlc_mutex_unlock( &QUEUE.lock );
135 * Object Printing selection
137 static void const * kObjectPrintingEnabled = (void *) 1;
138 static void const * kObjectPrintingDisabled = (void *) -1;
140 void __msg_EnableObjectPrinting (vlc_object_t *p_this, char * psz_object)
142 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
143 vlc_mutex_lock( &QUEUE.lock );
144 if( !strcmp(psz_object, "all") )
145 priv->msg_all_objects_enabled = true;
147 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, kObjectPrintingEnabled );
148 vlc_mutex_unlock( &QUEUE.lock );
151 void __msg_DisableObjectPrinting (vlc_object_t *p_this, char * psz_object)
153 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
154 vlc_mutex_lock( &QUEUE.lock );
155 if( !strcmp(psz_object, "all") )
156 priv->msg_all_objects_enabled = false;
158 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, kObjectPrintingDisabled );
159 vlc_mutex_unlock( &QUEUE.lock );
163 * Destroy the message queues
165 * This functions prints all messages remaining in the queues,
166 * then frees all the allocated ressources
167 * No other messages interface functions should be called after this one.
169 void msg_Destroy (libvlc_int_t *p_libvlc)
171 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
174 msg_Err( p_libvlc, "stale interface subscribers" );
178 vlc_mutex_t *lock = var_AcquireMutex( "msg-stack" );
180 vlc_threadvar_delete( &msg_context );
181 vlc_mutex_unlock( lock );
184 CloseHandle( QUEUE.logfile );
187 vlc_dictionary_clear( &priv->msg_enabled_objects );
190 vlc_mutex_destroy( &QUEUE.lock );
191 vlc_mutex_destroy( &priv->msg_bank.lock);
195 * Subscribe to a message queue.
197 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
199 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
200 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
206 vlc_mutex_lock( &QUEUE.lock );
208 TAB_APPEND( QUEUE.i_sub, QUEUE.pp_sub, p_sub );
210 p_sub->i_start = QUEUE.i_start;
211 p_sub->pi_stop = &QUEUE.i_stop;
212 p_sub->p_msg = QUEUE.msg;
213 p_sub->p_lock = &QUEUE.lock;
215 vlc_mutex_unlock( &QUEUE.lock );
222 * Unsubscribe from a message queue.
224 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
226 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
229 vlc_mutex_lock( &QUEUE.lock );
230 for( int j = 0 ; j< QUEUE.i_sub ; j++ )
232 if( QUEUE.pp_sub[j] == p_sub )
234 REMOVE_ELEM( QUEUE.pp_sub, QUEUE.i_sub, j );
238 vlc_mutex_unlock( &QUEUE.lock );
242 /*****************************************************************************
243 * __msg_*: print a message
244 *****************************************************************************
245 * These functions queue a message for later printing.
246 *****************************************************************************/
247 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
248 const char *psz_format, ... )
252 va_start( args, psz_format );
253 QueueMsg( p_this, i_type, psz_module, psz_format, args );
257 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
258 const char *psz_format, va_list args )
260 QueueMsg( p_this, i_type, psz_module, psz_format, args );
263 /* Generic functions used when variadic macros are not available. */
264 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
265 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
268 va_start( args, psz_format ); \
269 QueueMsg( p_this, FN_TYPE, "unknown", psz_format, args ); \
274 * Output an informational message.
275 * \note Do not use this for debug messages
278 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
280 * Output an error message.
282 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
284 * Output a waring message
286 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
288 * Output a debug message
290 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
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 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
301 const char *psz_format, va_list _args )
304 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
305 int i_header_size; /* Size of the additionnal header */
307 char * psz_str = NULL; /* formatted message string */
308 char * psz_header = NULL;
310 msg_item_t * p_item = NULL; /* pointer to message */
311 msg_item_t item; /* message in case of a full queue */
312 msg_queue_t *p_queue;
314 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
315 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
318 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
319 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
323 /* Expand %m to strerror(errno) - only once */
324 char buf[strlen( psz_format ) + 2001], *ptr;
325 strcpy( buf, psz_format );
327 psz_format = (const char*) buf;
331 ptr = strchr( ptr, '%' );
341 strerror_r( errno, errbuf, 1001 );
343 int sockerr = WSAGetLastError( );
346 strncpy( errbuf, net_strerror( sockerr ), 1001 );
347 WSASetLastError( sockerr );
350 || (strcmp ("Unknown network stack error", errbuf) == 0))
351 strncpy( errbuf, strerror( errno ), 1001 );
355 /* Escape '%' from the error string */
356 for( char *percent = strchr( errbuf, '%' );
358 percent = strchr( percent + 2, '%' ) )
360 memmove( percent + 1, percent, strlen( percent ) + 1 );
363 errlen = strlen( errbuf );
364 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
365 memcpy( ptr, errbuf, errlen );
366 break; /* Only once, so we don't overflow */
369 /* Looks for conversion specifier... */
372 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
374 ptr++; /* ...and skip it */
378 /* Convert message to string */
379 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
380 vlc_va_copy( args, _args );
381 if( vasprintf( &psz_str, psz_format, args ) == -1 )
385 psz_str = (char*) malloc( i_size );
388 if( psz_str == NULL )
390 int canc = vlc_savecancel (); /* Do not print half of a message... */
392 fprintf( stderr, "main warning: can't store message (%m): " );
396 /* we're not using GLIBC, so we are sure that the error description
397 * will be stored in the buffer we provide to strerror_r() */
398 strerror_r( errno, psz_err, 1001 );
400 strncpy( psz_err, strerror( errno ), 1001 );
402 psz_err[1000] = '\0';
403 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
405 vlc_va_copy( args, _args );
406 /* We should use utf8_vfprintf - but it calls malloc()... */
407 vfprintf( stderr, psz_format, args );
409 fputs( "\n", stderr );
410 vlc_restorecancel (canc);
416 while( p_obj != NULL )
418 char *psz_old = NULL;
419 if( p_obj->psz_header )
421 i_header_size += strlen( p_obj->psz_header ) + 4;
424 psz_old = strdup( psz_header );
425 psz_header = (char*)realloc( psz_header, i_header_size );
426 snprintf( psz_header, i_header_size , "[%s] %s",
427 p_obj->psz_header, psz_old );
431 psz_header = (char *)malloc( i_header_size );
432 snprintf( psz_header, i_header_size, "[%s]",
437 p_obj = p_obj->p_parent;
440 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
441 vlc_va_copy( args, _args );
442 vsnprintf( psz_str, i_size, psz_format, args );
444 psz_str[ i_size - 1 ] = 0; /* Just in case */
449 vlc_mutex_lock( &p_queue->lock );
451 /* Check there is room in the queue for our message */
452 if( p_queue->b_overflow )
456 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
458 /* Still in overflow mode, print from a dummy item */
463 /* Pheeew, at last, there is room in the queue! */
464 p_queue->b_overflow = false;
467 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
471 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
473 p_queue->b_overflow = true;
475 /* Put the overflow message in the queue */
476 p_item = p_queue->msg + p_queue->i_stop;
477 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
479 p_item->i_type = VLC_MSG_WARN;
480 p_item->i_object_id = p_this->i_object_id;
481 p_item->psz_object_type = p_this->psz_object_type;
482 p_item->psz_module = strdup( "message" );
483 p_item->psz_msg = strdup( "message queue overflowed" );
484 p_item->psz_header = NULL;
486 PrintMsg( p_this, p_item );
487 /* We print from a dummy item */
492 if( !p_queue->b_overflow )
494 /* Put the message in the queue */
495 p_item = p_queue->msg + p_queue->i_stop;
496 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
499 /* Fill message information fields */
500 p_item->i_type = i_type;
501 p_item->i_object_id = p_this->i_object_id;
502 p_item->psz_object_type = p_this->psz_object_type;
503 p_item->psz_module = strdup( psz_module );
504 p_item->psz_msg = psz_str;
505 p_item->psz_header = psz_header;
507 PrintMsg( p_this, p_item );
509 if( p_queue->b_overflow )
511 free( p_item->psz_module );
512 free( p_item->psz_msg );
513 free( p_item->psz_header );
516 vlc_mutex_unlock ( &p_queue->lock );
520 /* following functions are local */
522 /*****************************************************************************
524 *****************************************************************************
525 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
526 * this function does not check the lock.
527 *****************************************************************************/
528 static void FlushMsg ( msg_queue_t *p_queue )
530 int i_index, i_start, i_stop;
532 /* Get the maximum message index that can be freed */
533 i_stop = p_queue->i_stop;
535 /* Check until which value we can free messages */
536 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
538 i_start = p_queue->pp_sub[ i_index ]->i_start;
540 /* If this subscriber is late, we don't free messages before
541 * his i_start value, otherwise he'll miss messages */
542 if( ( i_start < i_stop
543 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
544 || ( i_stop < i_start
545 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
551 /* Free message data */
552 for( i_index = p_queue->i_start;
554 i_index = (i_index+1) % VLC_MSG_QSIZE )
556 free( p_queue->msg[i_index].psz_msg );
557 free( p_queue->msg[i_index].psz_module );
558 free( p_queue->msg[i_index].psz_header );
561 /* Update the new start value */
562 p_queue->i_start = i_index;
565 /*****************************************************************************
566 * PrintMsg: output a standard message item to stderr
567 *****************************************************************************
568 * Print a message to stderr, with colour formatting if needed.
569 *****************************************************************************/
570 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
572 # define COL(x) "\033[" #x ";1m"
574 # define GREEN COL(32)
575 # define YELLOW COL(33)
576 # define WHITE COL(0)
577 # define GRAY "\033[0m"
582 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
583 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
584 const char *psz_object;
585 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
586 int i_type = p_item->i_type;
591 if( priv->i_verbose < 0 ) return;
594 if( priv->i_verbose < 0 ) return;
597 if( priv->i_verbose < 1 ) return;
600 if( priv->i_verbose < 2 ) return;
604 psz_object = p_item->psz_object_type;
605 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
607 if( val == kObjectPrintingDisabled )
609 if( val == kObjectPrintingEnabled )
611 else if( !priv->msg_all_objects_enabled )
614 int canc = vlc_savecancel ();
616 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
617 str, strlen(str), &i_dummy, NULL );
618 CE_WRITE( p_item->psz_module );
620 CE_WRITE( psz_object );
621 CE_WRITE( ppsz_type[i_type] );
623 CE_WRITE( p_item->psz_msg );
625 FlushFileBuffers( QUEUE.logfile );
628 /* Send the message to stderr */
631 if( p_item->psz_header )
633 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
635 p_item->i_object_id, p_item->psz_header,
636 p_item->psz_module, psz_object,
637 ppsz_type[i_type], ppsz_color[i_type],
642 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
643 p_item->i_object_id, p_item->psz_module, psz_object,
644 ppsz_type[i_type], ppsz_color[i_type],
650 if( p_item->psz_header )
652 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
653 p_item->psz_header, p_item->psz_module,
654 psz_object, ppsz_type[i_type], p_item->psz_msg );
658 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
659 p_item->psz_module, psz_object, ppsz_type[i_type],
667 vlc_restorecancel (canc);
671 static msg_context_t* GetContext(void)
673 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context );
676 MALLOC_NULL( p_ctx, msg_context_t );
677 p_ctx->psz_message = NULL;
678 vlc_threadvar_set( &msg_context, p_ctx );
683 void msg_StackDestroy (void *data)
685 msg_context_t *p_ctx = data;
687 free (p_ctx->psz_message);
691 void msg_StackSet( int i_code, const char *psz_message, ... )
694 msg_context_t *p_ctx = GetContext();
698 free( p_ctx->psz_message );
700 va_start( ap, psz_message );
701 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
702 p_ctx->psz_message = NULL;
705 p_ctx->i_code = i_code;
708 void msg_StackAdd( const char *psz_message, ... )
712 msg_context_t *p_ctx = GetContext();
717 va_start( ap, psz_message );
718 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
722 if( !p_ctx->psz_message )
723 p_ctx->psz_message = psz_tmp;
727 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
730 free( p_ctx->psz_message );
731 p_ctx->psz_message = psz_new;
736 const char* msg_StackMsg( void )
738 msg_context_t *p_ctx = GetContext();
740 return p_ctx->psz_message;