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
79 static inline msg_bank_t *libvlc_bank (libvlc_int_t *inst)
81 return &(libvlc_priv (inst))->msg_bank;
84 /*****************************************************************************
86 *****************************************************************************/
87 static void QueueMsg ( vlc_object_t *, int, const char *,
88 const char *, va_list );
89 static void FlushMsg ( msg_bank_t * );
90 static void PrintMsg ( vlc_object_t *, msg_item_t * );
92 static vlc_mutex_t msg_stack_lock = VLC_STATIC_MUTEX;
95 * Initialize messages queues
96 * This function initializes all message queues
98 void msg_Create (libvlc_int_t *p_libvlc)
100 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
101 msg_bank_t *bank = libvlc_bank (p_libvlc);
103 vlc_mutex_init (&bank->lock);
104 vlc_cond_init (&bank->wait);
105 vlc_dictionary_init( &priv->msg_enabled_objects, 0 );
106 priv->msg_all_objects_enabled = true;
108 QUEUE.b_overflow = false;
116 CreateFile( L"vlc-log.txt", GENERIC_WRITE,
117 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
118 CREATE_ALWAYS, 0, NULL );
119 SetFilePointer( QUEUE.logfile, 0, NULL, FILE_END );
122 vlc_mutex_lock( &msg_stack_lock );
124 vlc_threadvar_create( &msg_context, NULL );
125 vlc_mutex_unlock( &msg_stack_lock );
129 * Flush all message queues
131 void msg_Flush (libvlc_int_t *p_libvlc)
133 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
134 vlc_mutex_lock( &QUEUE.lock );
136 vlc_mutex_unlock( &QUEUE.lock );
141 * Object Printing selection
143 static void const * kObjectPrintingEnabled = &kObjectPrintingEnabled;
144 static void const * kObjectPrintingDisabled = &kObjectPrintingDisabled;
146 void __msg_EnableObjectPrinting (vlc_object_t *p_this, char * psz_object)
148 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
149 vlc_mutex_lock( &QUEUE.lock );
150 if( !strcmp(psz_object, "all") )
151 priv->msg_all_objects_enabled = true;
153 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingEnabled );
154 vlc_mutex_unlock( &QUEUE.lock );
157 void __msg_DisableObjectPrinting (vlc_object_t *p_this, char * psz_object)
159 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
160 vlc_mutex_lock( &QUEUE.lock );
161 if( !strcmp(psz_object, "all") )
162 priv->msg_all_objects_enabled = false;
164 vlc_dictionary_insert( &priv->msg_enabled_objects, psz_object, (void *)kObjectPrintingDisabled );
165 vlc_mutex_unlock( &QUEUE.lock );
169 * Destroy the message queues
171 * This functions prints all messages remaining in the queues,
172 * then frees all the allocated ressources
173 * No other messages interface functions should be called after this one.
175 void msg_Destroy (libvlc_int_t *p_libvlc)
177 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
178 msg_bank_t *bank = libvlc_bank (p_libvlc);
181 msg_Err( p_libvlc, "stale interface subscribers (VLC might crash)" );
185 vlc_mutex_lock( &msg_stack_lock );
187 vlc_threadvar_delete( &msg_context );
188 vlc_mutex_unlock( &msg_stack_lock );
191 CloseHandle( QUEUE.logfile );
194 vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
196 vlc_cond_destroy (&bank->wait);
197 vlc_mutex_destroy (&bank->lock);
200 struct msg_subscription_t
203 libvlc_int_t *instance;
205 msg_cb_data_t *opaque;
206 msg_item_t *items[VLC_MSG_QSIZE];
211 static void *msg_thread (void *data)
213 msg_subscription_t *sub = data;
214 msg_bank_t *bank = libvlc_bank (sub->instance);
216 /* TODO: finer-grained locking and/or msg_item_t refcount */
217 vlc_mutex_lock (&bank->lock);
218 mutex_cleanup_push (&bank->lock);
222 /* Wait for messages */
223 assert (sub->begin < VLC_MSG_QSIZE);
224 assert (sub->end < VLC_MSG_QSIZE);
225 while (sub->begin != sub->end)
227 sub->func (sub->opaque, sub->items[sub->begin], sub->overruns);
228 if (++sub->begin == VLC_MSG_QSIZE)
232 vlc_cond_wait (&bank->wait, &bank->lock);
240 * Subscribe to the message queue.
241 * Whenever a message is emitted, a callback will be called.
242 * Callback invocation are serialized within a subscription.
244 * @param instance LibVLC instance to get messages from
245 * @param cb callback function
246 * @param opaque data for the callback function
247 * @return a subscription pointer, or NULL in case of failure
249 msg_subscription_t *msg_Subscribe (libvlc_int_t *instance, msg_callback_t cb,
250 msg_cb_data_t *opaque)
252 msg_subscription_t *sub = malloc (sizeof (*sub));
256 sub->instance = instance;
258 sub->opaque = opaque;
259 sub->begin = sub->end = sub->overruns = 0;
261 if (vlc_clone (&sub->thread, msg_thread, sub, VLC_THREAD_PRIORITY_LOW))
267 msg_bank_t *bank = libvlc_bank (instance);
268 vlc_mutex_lock (&bank->lock);
269 TAB_APPEND (bank->i_sub, bank->pp_sub, sub);
270 vlc_mutex_unlock (&bank->lock);
276 * Unsubscribe from the message queue.
277 * This function waits for the message callback to return if needed.
279 void msg_Unsubscribe (msg_subscription_t *sub)
281 msg_bank_t *bank = libvlc_bank (sub->instance);
283 /* TODO: flush support? */
284 vlc_cancel (sub->thread);
285 vlc_mutex_lock (&bank->lock);
286 TAB_REMOVE (bank->i_sub, bank->pp_sub, sub);
287 vlc_mutex_unlock (&bank->lock);
289 vlc_join (sub->thread, NULL);
293 /*****************************************************************************
294 * __msg_*: print a message
295 *****************************************************************************
296 * These functions queue a message for later printing.
297 *****************************************************************************/
298 void __msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
299 const char *psz_format, ... )
303 va_start( args, psz_format );
304 QueueMsg( p_this, i_type, psz_module, psz_format, args );
308 void __msg_GenericVa( vlc_object_t *p_this, int i_type, const char *psz_module,
309 const char *psz_format, va_list args )
311 QueueMsg( p_this, i_type, psz_module, psz_format, args );
315 * Add a message to a queue
317 * This function provides basic functionnalities to other msg_* functions.
318 * It adds a message to a queue (after having printed all stored messages if it
319 * is full). If the message can't be converted to string in memory, it issues
322 static void QueueMsg( vlc_object_t *p_this, int i_type, const char *psz_module,
323 const char *psz_format, va_list _args )
326 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
327 int i_header_size; /* Size of the additionnal header */
329 char * psz_str = NULL; /* formatted message string */
330 char * psz_header = NULL;
332 msg_item_t * p_item = NULL; /* pointer to message */
333 msg_item_t item; /* message in case of a full queue */
336 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
337 int i_size = strlen(psz_format) + INTF_MAX_MSG_SIZE;
340 if( p_this->i_flags & OBJECT_FLAGS_QUIET ||
341 (p_this->i_flags & OBJECT_FLAGS_NODBG && i_type == VLC_MSG_DBG) )
345 /* Expand %m to strerror(errno) - only once */
346 char buf[strlen( psz_format ) + 2001], *ptr;
347 strcpy( buf, psz_format );
349 psz_format = (const char*) buf;
353 ptr = strchr( ptr, '%' );
363 strerror_r( errno, errbuf, 1001 );
365 int sockerr = WSAGetLastError( );
368 strncpy( errbuf, net_strerror( sockerr ), 1001 );
369 WSASetLastError( sockerr );
372 || (strcmp ("Unknown network stack error", errbuf) == 0))
373 strncpy( errbuf, strerror( errno ), 1001 );
377 /* Escape '%' from the error string */
378 for( char *percent = strchr( errbuf, '%' );
380 percent = strchr( percent + 2, '%' ) )
382 memmove( percent + 1, percent, strlen( percent ) + 1 );
385 errlen = strlen( errbuf );
386 memmove( ptr + errlen, ptr + 2, strlen( ptr + 2 ) + 1 );
387 memcpy( ptr, errbuf, errlen );
388 break; /* Only once, so we don't overflow */
391 /* Looks for conversion specifier... */
394 while( *ptr && ( strchr( "diouxXeEfFgGaAcspn%", *ptr ) == NULL ) );
396 ptr++; /* ...and skip it */
400 /* Convert message to string */
401 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined( SYS_BEOS )
402 vlc_va_copy( args, _args );
403 if( vasprintf( &psz_str, psz_format, args ) == -1 )
407 psz_str = (char*) malloc( i_size );
410 if( psz_str == NULL )
412 int canc = vlc_savecancel (); /* Do not print half of a message... */
414 fprintf( stderr, "main warning: can't store message (%m): " );
418 /* we're not using GLIBC, so we are sure that the error description
419 * will be stored in the buffer we provide to strerror_r() */
420 strerror_r( errno, psz_err, 1001 );
422 strncpy( psz_err, strerror( errno ), 1001 );
424 psz_err[1000] = '\0';
425 fprintf( stderr, "main warning: can't store message (%s): ", psz_err );
427 vlc_va_copy( args, _args );
428 /* We should use utf8_vfprintf - but it calls malloc()... */
429 vfprintf( stderr, psz_format, args );
431 fputs( "\n", stderr );
432 vlc_restorecancel (canc);
438 while( p_obj != NULL )
440 char *psz_old = NULL;
441 if( p_obj->psz_header )
443 i_header_size += strlen( p_obj->psz_header ) + 4;
446 psz_old = strdup( psz_header );
447 psz_header = (char*)realloc( psz_header, i_header_size );
448 snprintf( psz_header, i_header_size , "[%s] %s",
449 p_obj->psz_header, psz_old );
453 psz_header = (char *)malloc( i_header_size );
454 snprintf( psz_header, i_header_size, "[%s]",
459 p_obj = p_obj->p_parent;
462 #if !defined(HAVE_VASPRINTF) || defined(__APPLE__) || defined(SYS_BEOS)
463 vlc_va_copy( args, _args );
464 vsnprintf( psz_str, i_size, psz_format, args );
466 psz_str[ i_size - 1 ] = 0; /* Just in case */
470 vlc_mutex_lock( &p_queue->lock );
472 /* Check there is room in the queue for our message */
473 if( p_queue->b_overflow )
477 if( ((p_queue->i_stop - p_queue->i_start + 1) % VLC_MSG_QSIZE) == 0 )
479 /* Still in overflow mode, print from a dummy item */
484 /* Pheeew, at last, there is room in the queue! */
485 p_queue->b_overflow = false;
488 else if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
492 if( ((p_queue->i_stop - p_queue->i_start + 2) % VLC_MSG_QSIZE) == 0 )
494 p_queue->b_overflow = true;
496 /* Put the overflow 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;
500 p_item->i_type = VLC_MSG_WARN;
501 p_item->i_object_id = (uintptr_t)p_this;
502 p_item->psz_object_type = p_this->psz_object_type;
503 p_item->psz_module = strdup( "message" );
504 p_item->psz_msg = strdup( "message queue overflowed" );
505 p_item->psz_header = NULL;
507 PrintMsg( p_this, p_item );
508 /* We print from a dummy item */
513 if( !p_queue->b_overflow )
515 /* Put the message in the queue */
516 p_item = p_queue->msg + p_queue->i_stop;
517 p_queue->i_stop = (p_queue->i_stop + 1) % VLC_MSG_QSIZE;
520 /* Fill message information fields */
521 p_item->i_type = i_type;
522 p_item->i_object_id = (uintptr_t)p_this;
523 p_item->psz_object_type = p_this->psz_object_type;
524 p_item->psz_module = strdup( psz_module );
525 p_item->psz_msg = psz_str;
526 p_item->psz_header = psz_header;
528 PrintMsg( p_this, p_item );
530 if( p_item == &item )
532 free( p_item->psz_module );
533 free( p_item->psz_msg );
534 free( p_item->psz_header );
535 for (int i = 0; i < bank->i_sub; i++)
536 bank->pp_sub[i]->overruns++;
540 for (int i = 0; i < bank->i_sub; i++)
542 msg_subscription_t *sub = bank->pp_sub[i];
543 if ((sub->end + 1 - sub->begin) % VLC_MSG_QSIZE)
545 sub->items[sub->end++] = p_item;
546 if (sub->end == VLC_MSG_QSIZE)
552 vlc_cond_broadcast (&bank->wait);
554 vlc_mutex_unlock (&bank->lock);
557 /* following functions are local */
559 /*****************************************************************************
561 *****************************************************************************
562 * Print all messages remaining in queue. MESSAGE QUEUE MUST BE LOCKED, since
563 * this function does not check the lock.
564 *****************************************************************************/
565 static void FlushMsg ( msg_bank_t *p_queue )
567 int i_index, i_start, i_stop;
569 /* Get the maximum message index that can be freed */
570 i_stop = p_queue->i_stop;
572 /* Check until which value we can free messages */
573 for( i_index = 0; i_index < p_queue->i_sub; i_index++ )
575 i_start = p_queue->pp_sub[ i_index ]->begin;
577 /* If this subscriber is late, we don't free messages before
578 * his i_start value, otherwise he'll miss messages */
579 if( ( i_start < i_stop
580 && (p_queue->i_stop <= i_start || i_stop <= p_queue->i_stop) )
581 || ( i_stop < i_start
582 && (i_stop <= p_queue->i_stop && p_queue->i_stop <= i_start) ) )
588 /* Free message data */
589 for( i_index = p_queue->i_start;
591 i_index = (i_index+1) % VLC_MSG_QSIZE )
593 free( p_queue->msg[i_index].psz_msg );
594 free( p_queue->msg[i_index].psz_module );
595 free( p_queue->msg[i_index].psz_header );
598 /* Update the new start value */
599 p_queue->i_start = i_index;
602 /*****************************************************************************
603 * PrintMsg: output a standard message item to stderr
604 *****************************************************************************
605 * Print a message to stderr, with colour formatting if needed.
606 *****************************************************************************/
607 static void PrintMsg ( vlc_object_t * p_this, msg_item_t * p_item )
609 # define COL(x) "\033[" #x ";1m"
611 # define GREEN COL(32)
612 # define YELLOW COL(33)
613 # define WHITE COL(0)
614 # define GRAY "\033[0m"
619 static const char ppsz_type[4][9] = { "", " error", " warning", " debug" };
620 static const char ppsz_color[4][8] = { WHITE, RED, YELLOW, GRAY };
621 const char *psz_object;
622 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
623 int i_type = p_item->i_type;
628 if( priv->i_verbose < 0 ) return;
631 if( priv->i_verbose < 0 ) return;
634 if( priv->i_verbose < 1 ) return;
637 if( priv->i_verbose < 2 ) return;
641 psz_object = p_item->psz_object_type;
642 void * val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
643 p_item->psz_module );
644 if( val == kObjectPrintingDisabled )
646 if( val == kObjectPrintingEnabled )
650 val = vlc_dictionary_value_for_key( &priv->msg_enabled_objects,
652 if( val == kObjectPrintingDisabled )
654 if( val == kObjectPrintingEnabled )
656 else if( !priv->msg_all_objects_enabled )
660 int canc = vlc_savecancel ();
662 # define CE_WRITE(str) WriteFile( QUEUE.logfile, \
663 str, strlen(str), &i_dummy, NULL );
664 CE_WRITE( p_item->psz_module );
666 CE_WRITE( psz_object );
667 CE_WRITE( ppsz_type[i_type] );
669 CE_WRITE( p_item->psz_msg );
671 FlushFileBuffers( QUEUE.logfile );
674 /* Send the message to stderr */
675 utf8_fprintf( stderr, "[%s%p%s] %s%s%s %s%s: %s%s%s\n",
676 priv->b_color ? GREEN : "",
677 (void *)p_item->i_object_id,
678 priv->b_color ? GRAY : "",
679 p_item->psz_header ? p_item->psz_header : "",
680 p_item->psz_header ? " " : "",
681 p_item->psz_module, psz_object,
683 priv->b_color ? ppsz_color[i_type] : "",
685 priv->b_color ? GRAY : "" );
690 vlc_restorecancel (canc);
694 static msg_context_t* GetContext(void)
696 msg_context_t *p_ctx = vlc_threadvar_get( &msg_context );
699 MALLOC_NULL( p_ctx, msg_context_t );
700 p_ctx->psz_message = NULL;
701 vlc_threadvar_set( &msg_context, p_ctx );
706 void msg_StackDestroy (void *data)
708 msg_context_t *p_ctx = data;
710 free (p_ctx->psz_message);
714 void msg_StackSet( int i_code, const char *psz_message, ... )
717 msg_context_t *p_ctx = GetContext();
721 free( p_ctx->psz_message );
723 va_start( ap, psz_message );
724 if( vasprintf( &p_ctx->psz_message, psz_message, ap ) == -1 )
725 p_ctx->psz_message = NULL;
728 p_ctx->i_code = i_code;
731 void msg_StackAdd( const char *psz_message, ... )
735 msg_context_t *p_ctx = GetContext();
740 va_start( ap, psz_message );
741 if( vasprintf( &psz_tmp, psz_message, ap ) == -1 )
745 if( !p_ctx->psz_message )
746 p_ctx->psz_message = psz_tmp;
750 if( asprintf( &psz_new, "%s: %s", psz_tmp, p_ctx->psz_message ) == -1 )
753 free( p_ctx->psz_message );
754 p_ctx->psz_message = psz_new;
759 const char* msg_StackMsg( void )
761 msg_context_t *p_ctx = GetContext();
763 return p_ctx->psz_message;