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"
58 /*****************************************************************************
60 *****************************************************************************/
61 #if defined(HAVE_VA_COPY)
62 # define vlc_va_copy(dest,src) va_copy(dest,src)
63 #elif defined(HAVE___VA_COPY)
64 # define vlc_va_copy(dest,src) __va_copy(dest,src)
66 # define vlc_va_copy(dest,src) (dest)=(src)
69 #define QUEUE priv->msg_bank.queue
70 #define LOCK_BANK vlc_mutex_lock( &priv->msg_bank.lock );
71 #define UNLOCK_BANK vlc_mutex_unlock( &priv->msg_bank.lock );
73 /*****************************************************************************
75 *****************************************************************************/
76 static void QueueMsg ( vlc_object_t *, int, const char *,
77 const char *, va_list );
78 static void FlushMsg ( msg_queue_t * );
79 static void PrintMsg ( vlc_object_t *, msg_item_t * );
82 * Initialize messages queues
83 * This function initializes all message queues
85 void msg_Create (libvlc_int_t *p_libvlc)
87 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
88 vlc_mutex_init( &priv->msg_bank.lock );
89 vlc_mutex_init( &QUEUE.lock );
90 vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
91 priv->msg_all_objects_enabled = true;
93 QUEUE.b_overflow = false;
101 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
102 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
103 CREATE_ALWAYS, 0, NULL );
104 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
109 * Flush all message queues
111 void msg_Flush (libvlc_int_t *p_libvlc)
113 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
114 vlc_mutex_lock( &QUEUE.lock );
116 vlc_mutex_unlock( &QUEUE.lock );
121 * Object Printing selection
123 static void const * kObjectPrintingEnabled = (void *) 1;
124 static void const * kObjectPrintingDisabled = (void *) -1;
126 void __msg_EnableObjectPrinting (vlc_object_t *p_this, char * psz_object)
128 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
129 vlc_mutex_lock( &QUEUE.lock );
130 if( !strcmp(psz_object, "all") )
131 priv->msg_all_objects_enabled = true;
133 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, kObjectPrintingEnabled );
134 vlc_mutex_unlock( &QUEUE.lock );
137 void __msg_DisableObjectPrinting (vlc_object_t *p_this, char * psz_object)
139 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
140 vlc_mutex_lock( &QUEUE.lock );
141 if( !strcmp(psz_object, "all") )
142 priv->msg_all_objects_enabled = false;
144 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, kObjectPrintingDisabled );
145 vlc_mutex_unlock( &QUEUE.lock );
149 * Destroy the message queues
151 * This functions prints all messages remaining in the queues,
152 * then frees all the allocated ressources
153 * No other messages interface functions should be called after this one.
155 void msg_Destroy (libvlc_int_t *p_libvlc)
157 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
160 msg_Err( p_libvlc, "stale interface subscribers" );
165 CloseHandle( QUEUE.logfile );
168 vlc_dictionary_clear( &priv->msg_enabled_objects );
171 vlc_mutex_destroy( &QUEUE.lock );
172 vlc_mutex_destroy( &priv->msg_bank.lock);
176 * Subscribe to a message queue.
178 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
180 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
181 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
187 vlc_mutex_lock( &QUEUE.lock );
189 TAB_APPEND( QUEUE.i_sub, QUEUE.pp_sub, p_sub );
191 p_sub->i_start = QUEUE.i_start;
192 p_sub->pi_stop = &QUEUE.i_stop;
193 p_sub->p_msg = QUEUE.msg;
194 p_sub->p_lock = &QUEUE.lock;
196 vlc_mutex_unlock( &QUEUE.lock );
203 * Unsubscribe from a message queue.
205 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
207 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
210 vlc_mutex_lock( &QUEUE.lock );
211 for( int j = 0 ; j< QUEUE.i_sub ; j++ )
213 if( QUEUE.pp_sub[j] == p_sub )
215 REMOVE_ELEM( QUEUE.pp_sub, QUEUE.i_sub, j );
219 vlc_mutex_unlock( &QUEUE.lock );
223 /*****************************************************************************
224 * __msg_*: print a message
225 *****************************************************************************
226 * These functions queue a message for later printing.
227 *****************************************************************************/
228 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
229 const char *psz_format, ... )
233 va_start( args, psz_format );
234 QueueMsg( p_this, i_type, psz_module, psz_format, args );
238 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
239 const char *psz_format, va_list args )
241 QueueMsg( p_this, i_type, psz_module, psz_format, args );
244 /* Generic functions used when variadic macros are not available. */
245 #define DECLARE_MSG_FN( FN_NAME, FN_TYPE ) \
246 void FN_NAME( vlc_object_t *p_this, const char *psz_format, ... ) \
249 va_start( args, psz_format ); \
250 QueueMsg( p_this, FN_TYPE, "unknown", psz_format, args ); \
255 * Output an informational message.
256 * \note Do not use this for debug messages
259 DECLARE_MSG_FN( __msg_Info, VLC_MSG_INFO );
261 * Output an error message.
263 DECLARE_MSG_FN( __msg_Err, VLC_MSG_ERR );
265 * Output a waring message
267 DECLARE_MSG_FN( __msg_Warn, VLC_MSG_WARN );
269 * Output a debug message
271 DECLARE_MSG_FN( __msg_Dbg, VLC_MSG_DBG );
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;
291 msg_item_t * p_item = NULL; /* pointer to message */
292 msg_item_t item; /* message in case of a full queue */
293 msg_queue_t *p_queue;
295 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
296 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
299 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
300 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
304 /* Expand %m to strerror(errno) - only once */
305 char buf[strlen( psz_format ) + 2001], *ptr;
306 strcpy( buf, psz_format );
308 psz_format = (const char*) buf;
312 ptr = strchr( ptr, '%' );
322 strerror_r( errno, errbuf, 1001 );
324 int sockerr = WSAGetLastError( );
327 strncpy( errbuf, net_strerror( sockerr ), 1001 );
328 WSASetLastError( sockerr );
331 || (strcmp ("Unknown network stack error", errbuf) == 0))
332 strncpy( errbuf, strerror( errno ), 1001 );
336 /* Escape '%' from the error string */
337 for( char *percent = strchr( errbuf, '%' );
339 percent = strchr( percent + 2, '%' ) )
341 memmove( percent + 1, percent, strlen( percent ) + 1 );
344 errlen = strlen( errbuf );
345 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
346 memcpy( ptr, errbuf, errlen );
347 break; /* Only once, so we don't overflow */
350 /* Looks for conversion specifier... */
353 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
355 ptr++; /* ...and skip it */
359 /* Convert message to string */
360 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
361 vlc_va_copy( args, _args );
362 if( vasprintf( &psz_str, psz_format, args ) == -1 )
366 psz_str = (char*) malloc( i_size );
369 if( psz_str == NULL )
372 fprintf( stderr, "main warning: can't store message (%m): " );
376 /* we're not using GLIBC, so we are sure that the error description
377 * will be stored in the buffer we provide to strerror_r() */
378 strerror_r( errno, psz_err, 1001 );
380 strncpy( psz_err, strerror( errno ), 1001 );
382 psz_err[1000] = '\0';
383 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
385 vlc_va_copy( args, _args );
386 /* We should use utf8_vfprintf - but it calls malloc()... */
387 vfprintf( stderr, psz_format, args );
389 fputs( "\n", stderr );
395 while( p_obj != NULL )
397 char *psz_old = NULL;
398 if( p_obj->psz_header )
400 i_header_size += strlen( p_obj->psz_header ) + 4;
403 psz_old = strdup( psz_header );
404 psz_header = (char*)realloc( psz_header, i_header_size );
405 snprintf( psz_header, i_header_size , "[%s] %s",
406 p_obj->psz_header, psz_old );
410 psz_header = (char *)malloc( i_header_size );
411 snprintf( psz_header, i_header_size, "[%s]",
416 p_obj = p_obj->p_parent;
419 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
420 vlc_va_copy( args, _args );
421 vsnprintf( psz_str, i_size, psz_format, args );
423 psz_str[ i_size - 1 ] = 0; /* Just in case */
428 vlc_mutex_lock( &p_queue->lock );
430 /* Check there is room in the queue for our message */
431 if( p_queue->b_overflow )
435 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
437 /* Still in overflow mode, print from a dummy item */
442 /* Pheeew, at last, there is room in the queue! */
443 p_queue->b_overflow = false;
446 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
450 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
452 p_queue->b_overflow = true;
454 /* Put the overflow message in the queue */
455 p_item = p_queue->msg + p_queue->i_stop;
456 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
458 p_item->i_type = VLC_MSG_WARN;
459 p_item->i_object_id = p_this->i_object_id;
460 p_item->psz_object_type = p_this->psz_object_type;
461 p_item->psz_module = strdup( "message" );
462 p_item->psz_msg = strdup( "message queue overflowed" );
463 p_item->psz_header = NULL;
465 PrintMsg( p_this, p_item );
466 /* We print from a dummy item */
471 if( !p_queue->b_overflow )
473 /* Put the message in the queue */
474 p_item = p_queue->msg + p_queue->i_stop;
475 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
478 /* Fill message information fields */
479 p_item->i_type = i_type;
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( psz_module );
483 p_item->psz_msg = psz_str;
484 p_item->psz_header = psz_header;
486 PrintMsg( p_this, p_item );
488 if( p_queue->b_overflow )
490 free( p_item->psz_module );
491 free( p_item->psz_msg );
492 free( p_item->psz_header );
495 vlc_mutex_unlock ( &p_queue->lock );
499 /* following functions are local */
501 /*****************************************************************************
503 *****************************************************************************
504 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
505 * this function does not check the lock.
506 *****************************************************************************/
507 static void FlushMsg ( msg_queue_t *p_queue )
509 int i_index, i_start, i_stop;
511 /* Get the maximum message index that can be freed */
512 i_stop = p_queue->i_stop;
514 /* Check until which value we can free messages */
515 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
517 i_start = p_queue->pp_sub[ i_index ]->i_start;
519 /* If this subscriber is late, we don't free messages before
520 * his i_start value, otherwise he'll miss messages */
521 if( ( i_start < i_stop
522 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
523 || ( i_stop < i_start
524 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
530 /* Free message data */
531 for( i_index = p_queue->i_start;
533 i_index = (i_index+1) % VLC_MSG_QSIZE )
535 free( p_queue->msg[i_index].psz_msg );
536 free( p_queue->msg[i_index].psz_module );
537 free( p_queue->msg[i_index].psz_header );
540 /* Update the new start value */
541 p_queue->i_start = i_index;
544 /*****************************************************************************
545 * PrintMsg: output a standard message item to stderr
546 *****************************************************************************
547 * Print a message to stderr, with colour formatting if needed.
548 *****************************************************************************/
549 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
551 # define COL(x) "\033[" #x ";1m"
553 # define GREEN COL(32)
554 # define YELLOW COL(33)
555 # define WHITE COL(0)
556 # define GRAY "\033[0m"
561 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
562 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
563 const char *psz_object;
564 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
565 int i_type = p_item->i_type;
570 if( priv->i_verbose < 0 ) return;
573 if( priv->i_verbose < 0 ) return;
576 if( priv->i_verbose < 1 ) return;
579 if( priv->i_verbose < 2 ) return;
583 psz_object = p_item->psz_object_type;
584 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
586 if( val == kObjectPrintingDisabled )
588 if( val == kObjectPrintingEnabled )
590 else if( !priv->msg_all_objects_enabled )
594 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
595 str, strlen(str), &i_dummy, NULL );
596 CE_WRITE( p_item->psz_module );
598 CE_WRITE( psz_object );
599 CE_WRITE( ppsz_type[i_type] );
601 CE_WRITE( p_item->psz_msg );
603 FlushFileBuffers( QUEUE.logfile );
606 /* Send the message to stderr */
609 if( p_item->psz_header )
611 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s %s%s: %s%s" GRAY
613 p_item->i_object_id, p_item->psz_header,
614 p_item->psz_module, psz_object,
615 ppsz_type[i_type], ppsz_color[i_type],
620 utf8_fprintf( stderr, "[" GREEN "%.8i" GRAY "] %s %s%s: %s%s" GRAY "\n",
621 p_item->i_object_id, p_item->psz_module, psz_object,
622 ppsz_type[i_type], ppsz_color[i_type],
628 if( p_item->psz_header )
630 utf8_fprintf( stderr, "[%.8i] %s %s %s%s: %s\n", p_item->i_object_id,
631 p_item->psz_header, p_item->psz_module,
632 psz_object, ppsz_type[i_type], p_item->psz_msg );
636 utf8_fprintf( stderr, "[%.8i] %s %s%s: %s\n", p_item->i_object_id,
637 p_item->psz_module, psz_object, ppsz_type[i_type],
648 static msg_context_t* GetContext(void)
650 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context_global_key );
653 MALLOC_NULL( p_ctx, msg_context_t );
654 p_ctx->psz_message = NULL;
655 vlc_threadvar_set( &msg_context_global_key, p_ctx );
660 void msg_StackDestroy (void *data)
662 msg_context_t *p_ctx = data;
664 free (p_ctx->psz_message);
668 void msg_StackSet( int i_code, const char *psz_message, ... )
671 msg_context_t *p_ctx = GetContext();
675 free( p_ctx->psz_message );
677 va_start( ap, psz_message );
678 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
679 p_ctx->psz_message = NULL;
682 p_ctx->i_code = i_code;
685 void msg_StackAdd( const char *psz_message, ... )
689 msg_context_t *p_ctx = GetContext();
694 va_start( ap, psz_message );
695 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
699 if( !p_ctx->psz_message )
700 p_ctx->psz_message = psz_tmp;
704 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
707 free( p_ctx->psz_message );
708 p_ctx->psz_message = psz_new;
713 const char* msg_StackMsg( void )
715 msg_context_t *p_ctx = GetContext();
717 return p_ctx->psz_message;