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
80 /*****************************************************************************
82 *****************************************************************************/
83 static void QueueMsg ( vlc_object_t *, int, const char *,
84 const char *, va_list );
85 static void FlushMsg ( msg_bank_t * );
86 static void PrintMsg ( vlc_object_t *, msg_item_t * );
88 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
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( &QUEUE.lock );
98 vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
99 priv->msg_all_objects_enabled = true;
101 QUEUE.b_overflow = false;
109 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
110 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
111 CREATE_ALWAYS, 0, NULL );
112 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
115 vlc_mutex_lock( &msg_stack_lock );
117 vlc_threadvar_create( &msg_context, NULL );
118 vlc_mutex_unlock( &msg_stack_lock );
122 * Flush all message queues
124 void msg_Flush (libvlc_int_t *p_libvlc)
126 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
127 vlc_mutex_lock( &QUEUE.lock );
129 vlc_mutex_unlock( &QUEUE.lock );
134 * Object Printing selection
136 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
137 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
139 void __msg_EnableObjectPrinting (vlc_object_t *p_this, char * psz_object)
141 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
142 vlc_mutex_lock( &QUEUE.lock );
143 if( !strcmp(psz_object, "all") )
144 priv->msg_all_objects_enabled = true;
146 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
147 vlc_mutex_unlock( &QUEUE.lock );
150 void __msg_DisableObjectPrinting (vlc_object_t *p_this, char * psz_object)
152 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
153 vlc_mutex_lock( &QUEUE.lock );
154 if( !strcmp(psz_object, "all") )
155 priv->msg_all_objects_enabled = false;
157 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
158 vlc_mutex_unlock( &QUEUE.lock );
162 * Destroy the message queues
164 * This functions prints all messages remaining in the queues,
165 * then frees all the allocated ressources
166 * No other messages interface functions should be called after this one.
168 void msg_Destroy (libvlc_int_t *p_libvlc)
170 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
173 msg_Err( p_libvlc, "stale interface subscribers" );
177 vlc_mutex_lock( &msg_stack_lock );
179 vlc_threadvar_delete( &msg_context );
180 vlc_mutex_unlock( &msg_stack_lock );
183 CloseHandle( QUEUE.logfile );
186 vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
189 vlc_mutex_destroy( &QUEUE.lock );
193 * Subscribe to a message queue.
195 msg_subscription_t *__msg_Subscribe( vlc_object_t *p_this )
197 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
198 msg_subscription_t *p_sub = malloc( sizeof( msg_subscription_t ) );
203 vlc_mutex_lock( &QUEUE.lock );
205 TAB_APPEND( QUEUE.i_sub, QUEUE.pp_sub, p_sub );
207 p_sub->i_start = QUEUE.i_start;
208 p_sub->pi_stop = &QUEUE.i_stop;
209 p_sub->p_msg = QUEUE.msg;
210 p_sub->p_lock = &QUEUE.lock;
212 vlc_mutex_unlock( &QUEUE.lock );
218 * Unsubscribe from a message queue.
220 void __msg_Unsubscribe( vlc_object_t *p_this, msg_subscription_t *p_sub )
222 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
224 vlc_mutex_lock( &QUEUE.lock );
225 for( int j = 0 ; j< QUEUE.i_sub ; j++ )
227 if( QUEUE.pp_sub[j] == p_sub )
229 REMOVE_ELEM( QUEUE.pp_sub, QUEUE.i_sub, j );
233 vlc_mutex_unlock( &QUEUE.lock );
236 /*****************************************************************************
237 * __msg_*: print a message
238 *****************************************************************************
239 * These functions queue a message for later printing.
240 *****************************************************************************/
241 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
242 const char *psz_format, ... )
246 va_start( args, psz_format );
247 QueueMsg( p_this, i_type, psz_module, psz_format, args );
251 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
252 const char *psz_format, va_list args )
254 QueueMsg( p_this, i_type, psz_module, psz_format, args );
258 * Add a message to a queue
260 * This function provides basic functionnalities to other msg_* functions.
261 * It adds a message to a queue (after having printed all stored messages if it
262 * is full). If the message can't be converted to string in memory, it issues
265 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
266 const char *psz_format, va_list _args )
269 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
270 int i_header_size; /* Size of the additionnal header */
272 char * psz_str = NULL; /* formatted message string */
273 char * psz_header = NULL;
275 msg_item_t * p_item = NULL; /* pointer to message */
276 msg_item_t item; /* message in case of a full queue */
279 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
280 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
283 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
284 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
288 /* Expand %m to strerror(errno) - only once */
289 char buf[strlen( psz_format ) + 2001], *ptr;
290 strcpy( buf, psz_format );
292 psz_format = (const char*) buf;
296 ptr = strchr( ptr, '%' );
306 strerror_r( errno, errbuf, 1001 );
308 int sockerr = WSAGetLastError( );
311 strncpy( errbuf, net_strerror( sockerr ), 1001 );
312 WSASetLastError( sockerr );
315 || (strcmp ("Unknown network stack error", errbuf) == 0))
316 strncpy( errbuf, strerror( errno ), 1001 );
320 /* Escape '%' from the error string */
321 for( char *percent = strchr( errbuf, '%' );
323 percent = strchr( percent + 2, '%' ) )
325 memmove( percent + 1, percent, strlen( percent ) + 1 );
328 errlen = strlen( errbuf );
329 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
330 memcpy( ptr, errbuf, errlen );
331 break; /* Only once, so we don't overflow */
334 /* Looks for conversion specifier... */
337 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
339 ptr++; /* ...and skip it */
343 /* Convert message to string */
344 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
345 vlc_va_copy( args, _args );
346 if( vasprintf( &psz_str, psz_format, args ) == -1 )
350 psz_str = (char*) malloc( i_size );
353 if( psz_str == NULL )
355 int canc = vlc_savecancel (); /* Do not print half of a message... */
357 fprintf( stderr, "main warning: can't store message (%m): " );
361 /* we're not using GLIBC, so we are sure that the error description
362 * will be stored in the buffer we provide to strerror_r() */
363 strerror_r( errno, psz_err, 1001 );
365 strncpy( psz_err, strerror( errno ), 1001 );
367 psz_err[1000] = '\0';
368 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
370 vlc_va_copy( args, _args );
371 /* We should use utf8_vfprintf - but it calls malloc()... */
372 vfprintf( stderr, psz_format, args );
374 fputs( "\n", stderr );
375 vlc_restorecancel (canc);
381 while( p_obj != NULL )
383 char *psz_old = NULL;
384 if( p_obj->psz_header )
386 i_header_size += strlen( p_obj->psz_header ) + 4;
389 psz_old = strdup( psz_header );
390 psz_header = (char*)realloc( psz_header, i_header_size );
391 snprintf( psz_header, i_header_size , "[%s] %s",
392 p_obj->psz_header, psz_old );
396 psz_header = (char *)malloc( i_header_size );
397 snprintf( psz_header, i_header_size, "[%s]",
402 p_obj = p_obj->p_parent;
405 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
406 vlc_va_copy( args, _args );
407 vsnprintf( psz_str, i_size, psz_format, args );
409 psz_str[ i_size - 1 ] = 0; /* Just in case */
413 vlc_mutex_lock( &p_queue->lock );
415 /* Check there is room in the queue for our message */
416 if( p_queue->b_overflow )
420 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
422 /* Still in overflow mode, print from a dummy item */
427 /* Pheeew, at last, there is room in the queue! */
428 p_queue->b_overflow = false;
431 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
435 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
437 p_queue->b_overflow = true;
439 /* Put the overflow message in the queue */
440 p_item = p_queue->msg + p_queue->i_stop;
441 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
443 p_item->i_type = VLC_MSG_WARN;
444 p_item->i_object_id = (uintptr_t)p_this;
445 p_item->psz_object_type = p_this->psz_object_type;
446 p_item->psz_module = strdup( "message" );
447 p_item->psz_msg = strdup( "message queue overflowed" );
448 p_item->psz_header = NULL;
450 PrintMsg( p_this, p_item );
451 /* We print from a dummy item */
456 if( !p_queue->b_overflow )
458 /* Put the message in the queue */
459 p_item = p_queue->msg + p_queue->i_stop;
460 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
463 /* Fill message information fields */
464 p_item->i_type = i_type;
465 p_item->i_object_id = (uintptr_t)p_this;
466 p_item->psz_object_type = p_this->psz_object_type;
467 p_item->psz_module = strdup( psz_module );
468 p_item->psz_msg = psz_str;
469 p_item->psz_header = psz_header;
471 PrintMsg( p_this, p_item );
473 if( p_queue->b_overflow )
475 free( p_item->psz_module );
476 free( p_item->psz_msg );
477 free( p_item->psz_header );
480 vlc_mutex_unlock ( &p_queue->lock );
483 /* following functions are local */
485 /*****************************************************************************
487 *****************************************************************************
488 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
489 * this function does not check the lock.
490 *****************************************************************************/
491 static void FlushMsg ( msg_bank_t *p_queue )
493 int i_index, i_start, i_stop;
495 /* Get the maximum message index that can be freed */
496 i_stop = p_queue->i_stop;
498 /* Check until which value we can free messages */
499 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
501 i_start = p_queue->pp_sub[ i_index ]->i_start;
503 /* If this subscriber is late, we don't free messages before
504 * his i_start value, otherwise he'll miss messages */
505 if( ( i_start < i_stop
506 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
507 || ( i_stop < i_start
508 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
514 /* Free message data */
515 for( i_index = p_queue->i_start;
517 i_index = (i_index+1) % VLC_MSG_QSIZE )
519 free( p_queue->msg[i_index].psz_msg );
520 free( p_queue->msg[i_index].psz_module );
521 free( p_queue->msg[i_index].psz_header );
524 /* Update the new start value */
525 p_queue->i_start = i_index;
528 /*****************************************************************************
529 * PrintMsg: output a standard message item to stderr
530 *****************************************************************************
531 * Print a message to stderr, with colour formatting if needed.
532 *****************************************************************************/
533 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
535 # define COL(x) "\033[" #x ";1m"
537 # define GREEN COL(32)
538 # define YELLOW COL(33)
539 # define WHITE COL(0)
540 # define GRAY "\033[0m"
545 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
546 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
547 const char *psz_object;
548 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
549 int i_type = p_item->i_type;
554 if( priv->i_verbose < 0 ) return;
557 if( priv->i_verbose < 0 ) return;
560 if( priv->i_verbose < 1 ) return;
563 if( priv->i_verbose < 2 ) return;
567 psz_object = p_item->psz_object_type;
568 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
569 p_item->psz_module );
570 if( val == kObjectPrintingDisabled )
572 if( val == kObjectPrintingEnabled )
576 val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
578 if( val == kObjectPrintingDisabled )
580 if( val == kObjectPrintingEnabled )
582 else if( !priv->msg_all_objects_enabled )
586 int canc = vlc_savecancel ();
588 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
589 str, strlen(str), &i_dummy, NULL );
590 CE_WRITE( p_item->psz_module );
592 CE_WRITE( psz_object );
593 CE_WRITE( ppsz_type[i_type] );
595 CE_WRITE( p_item->psz_msg );
597 FlushFileBuffers( QUEUE.logfile );
600 /* Send the message to stderr */
601 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
602 priv->b_color ? GREEN : "",
603 (void *)p_item->i_object_id,
604 priv->b_color ? GRAY : "",
605 p_item->psz_header ? p_item->psz_header : "",
606 p_item->psz_header ? " " : "",
607 p_item->psz_module, psz_object,
609 priv->b_color ? ppsz_color[i_type] : "",
611 priv->b_color ? GRAY : "" );
616 vlc_restorecancel (canc);
620 static msg_context_t* GetContext(void)
622 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context );
625 MALLOC_NULL( p_ctx, msg_context_t );
626 p_ctx->psz_message = NULL;
627 vlc_threadvar_set( &msg_context, p_ctx );
632 void msg_StackDestroy (void *data)
634 msg_context_t *p_ctx = data;
636 free (p_ctx->psz_message);
640 void msg_StackSet( int i_code, const char *psz_message, ... )
643 msg_context_t *p_ctx = GetContext();
647 free( p_ctx->psz_message );
649 va_start( ap, psz_message );
650 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
651 p_ctx->psz_message = NULL;
654 p_ctx->i_code = i_code;
657 void msg_StackAdd( const char *psz_message, ... )
661 msg_context_t *p_ctx = GetContext();
666 va_start( ap, psz_message );
667 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
671 if( !p_ctx->psz_message )
672 p_ctx->psz_message = psz_tmp;
676 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
679 free( p_ctx->psz_message );
680 p_ctx->psz_message = psz_new;
685 const char* msg_StackMsg( void )
687 msg_context_t *p_ctx = GetContext();
689 return p_ctx->psz_message;